添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to use SFTP to copy some files from one server to another, this task should run everyweek. The script I use :

    HOST='sftp://my.server.com'
    USER='user1'
    PASSWORD='passwd'
    DIR=$HOSTNAME
    REMOTE_DIR='/home/remote' 
    LOCAL_DIR='/home/local'     
    # LFTP via SFTP connexion
    lftp -u "$USER","$PASSWORD" $HOST <<EOF
    # changing directory
    cd "$REMOTE_DIR"
    $(if [ ! -d "$DIR" ]; then
     mkdir  $DIR
    put -O "$REMOTE_DIR"/$DIR  "$LOCAL_DIR"/uploaded.txt

My issue is that put is executed without taking in consideration the result of if statment.

PS : The error message I got is the following :

put: Access failed: No such file (/home/backups/myhost/upload.txt)

I think it's a little bit unclear what you want. Should put only be executed when the code in the if statement is executed? – Rene Knop Aug 10, 2018 at 8:54 The if statement, verify if a folder with the sender hostname exists or no, if yes it will send the file to that specific folder, otherwise, it will create one. The idea is to create a folder for each host sending files. – Aziz Azizos Aug 10, 2018 at 8:58

You call a sub command in a here document. The sub command is executed locally before lftp is started and its output is pasted in the here document, which gets passed to lftp. This works just, because mkdir has no output. You do not call mkdir on the ftp server. You call the mkdir of your local shell. Effectively it is the same as if you put the if statement before the lftp execution.

if [ ! -d "$DIR" ]; then
  mkdir $DIR
lftp -u "$USER","$PASSWORD" $HOST <<EOF
cd "$REMOTE_DIR"
put -O "$REMOTE_DIR"/$DIR  "$LOCAL_DIR"/uploaded.txt

What you are trying to do, does not work. You have to think about a different solution.

Right now I have no FTP server to test it, but it might be possible to use the -f option of LFTP's mkdir. I assume that it may work like the -f option of the Unix rm command. Try this:

lftp -u "$USER","$PASSWORD" $HOST <<EOF
cd "$REMOTE_DIR"
mkdir -f "$DIR"
put -O "$REMOTE_DIR"/$DIR  "$LOCAL_DIR"/uploaded.txt

Update: It works as supposed. The creation of a directory, which exist already, throws no error, if you use the option -f:

lftp anonymous@localhost:/pub> mkdir -f dir
mkdir ok, `dir' created
lftp anonymous@localhost:/pub> mkdir -f dir
lftp anonymous@localhost:/pub> ls
drwx------    2 116      122          4096 Aug 10 12:04 dir

Maybe you lftp client is outdated. I tested it with Debian 9.

Thanks for the clarification, I tried mkdir -f but it doesn't work, **mkdir: invalid option -- 'f' ** Will look for another way to do it. – Aziz Azizos Aug 10, 2018 at 9:47

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.