3

I'm trying to write a code that will check a specific date's file and if it exists it'll get it to my local home path and if it doesn't exists then will retrieve other file of different date.

#!/bin/ksh

FILE1="CAS_20170604.txt"
FILE2="CAS_20170603.txt"

/usr/bin/ftp -n 93.45.148.9 << EOF
    user usr passwd
    cd "/abc/def"
    bin
    get $FILE1

if [ ! -f $FILE1 ];
then
    cd "/abc/def"
    bin
    get $FILE2
fi
    bye
    !EOF! 

While on executing the script, I'm getting both the files, which is not required, and the below error:

?Invalid command
?Invalid command

Please someone could help me here.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
User123
  • 1,498
  • 2
  • 12
  • 26
  • Do you have any command line clients like `wget`, `curl`, `fetch`? Rather than scripting things to use the `ftp` client directly, it seems to me it would be easier to make FTP protocol requests using something that can provide return codes your shell script can interpret. Happy to demonstrate in an answer if you think this might be an option. – ghoti Jun 05 '17 at 07:16

1 Answers1

2

You cannot use shell commands in ftp script.

You have to split the script into two:

/usr/bin/ftp -n 93.45.148.9 << EOF
user usr passwd
cd "/abc/def"
bin
get $FILE1
bye
!EOF! 

if [ ! -f $FILE1 ];
then
    /usr/bin/ftp -n 93.45.148.9 << EOF
    user usr passwd
    cd "/abc/def"
    bin
    get $FILE2
    bye
    !EOF! 
fi
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 1
    Thanks :-) It's working, additionally I've used redirection while doing the first ftp to prevent the output "Failed to open File" to be displayed on the screen , in case the current date's file is not found. Like : /usr/bin/ftp -n 93.179.136.9 << !EOF! > /dev/null 2>&1 – User123 Jun 05 '17 at 06:39