简体   繁体   中英

FTP Shell Script does not working

I want to download file from server using FTP.

Here's my script code. (myscript.sh)

#!/bin/ksh
ftp -n $1 << END
user $2 $3
lcd $4
get $5
bye
EOF

After writing script and run, It does not working.

sh myscript.sh 111.222.333.444 testuser test /myfolder/src/data/ DATA20140419.txt

shell display this message : usage lcd local-directory

What's the problem in my script code?

Try to start the command via : ./myscript.sh and not via sh ./myscript.sh : You are using a shebang specifying ksh . Invoking sh directly will most certainly "bypass" the shebang (it will, unless sh reads it and starts ksh - and that depends on your sh implementation), and will not invoke ksh as wanted.

Also, you should not end your here document with EOF if it expects END . Another point is that you probably want to put all your arguments ( $1 , $2 , etc.) between quotes, so you are sure that their value is not separated by spaces (as in "$1" , "$2" , and so on).

However, I doubt that this is causing the error message. The point is, I cannot replicate your error locally, even if I copy/paste your script. Then, I cannot tell what is causing this error message to appear, since an empty lcd argument does output the current local directory, on my local FTP client (Debian).

The only answer I can give you is that prior to the get , you should cd to the correct directory (the get command should only contain the path of the file in the current directory). Then your script shall be:

#!/bin/ksh
ftp -n $1 << END
user $2 $3
lcd $4
cd $(dirname $5)
get $(basename $5)
bye
END

and you should use it like this:

$ ./myscript <server> <user> <passwd> <localdir> <remotedir/file>

Example:

$ ./myscript 1.2.3.4 7heo secret . /path/to/remote/file

I solved this problem.

The reason is, encoding.

I wrote script code using windows, run in unix.

I missed that difference of "ENTER".

UNIX's enter is \\n

but, Windows enter is \\r\\n

so, Without encoding script code, error must be occured.

Most of windows editer supply encoding option. (UNIX, DOS, WINDOWS style.)

If you use unix editor(ex, vi) you don't need to encoding.

Addition.

  1. If you didn't set remote directory(cd) or local directory(lcd), the source and destnation is default folder.

  2. END, EOF don't effect script code. you can use any string.

  3. To run shell script, you must use "sh" keyword. "./" can't run shell script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM