简体   繁体   中英

entering the last created directory via sftp connection

i have opened an SFTP connection to a remote server via Linux shell script, now i need to enter to the last created directory in directory /distribution and download the files from it, the code looks like:

lftp -u ${sourceEnv},${sourcePass} sftp://${sourceHost}<<EOF
cd $sourceBuildDir/distribution
variable=$(ls -t -r | tail -n 1)
cd $variable
mget *
bye
EOF

but it doesn't work via SFTP connection neither the command ls -t -r | tail -n 1 by itself nor the variable creation. Any ideas ? Thanks in advance

in the same spirit of Marc B's answer, I don't think you can do it all inside of lftp. In particular I don't think it supports user variables... (I should check...)

So, for some purposes you can call the shell from which you start lftp inside of a lftp script. For example:

# This is a lftp script (like yours)
ls -tr| ! "tail -1|awk '{print $NF}'"

Everything after the '!' is passed to the unix shell. The 'tail' that is called is the system tail and not some lftp command. The same goes for awk, which I use to get only the name of the dir (ls of lftp gives all file permissions etc...)

For more complex things (as your requested) I believe you're better off with a shell script that uses lftp just as any other shell command. Your example would become something like:

    # Call lftp to log in and do 'ls', capture the ouput and process it.
    sourceBuildDir=$(lftp -c "open ${sourceEnv}:${sourcePass}@${sourceHost}; ls" \
                     |tail -1|awk '{print $NF}');
    echo "I am about to download $sourceBuildDir";
    # Call lftp with the processed dir name and do the rest 
    # (btw. did you consider using the 'mirror' command?)
    lftp -c "open ${sourceEnv}:${sourcePass}@${sourceHost}; \
             cd $sourceBuildDir/distribution; mget *";

PS Sometimes lftp commands produce extra stuff like "[FEAT negotiation...]" that may break the script. You can probably solve by repeating lftp commands twice, so that the second time they succeed without further negotiation by lftp.

Hope this helps getting on the right track! Cheers.

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