I have this working:

% cat read.sh


while read line
    echo "$line"
cut -d' ' -f27 | sed -n '$p' > file2
done < "$file"

% cat list.txt 
sftp> #!/bin/sh
sftp> cd u/aaa
sftp> ls -lrt x_*.csv
-rwxr-xr-x    0 1001     1001     12274972 May 13 21:07 x_20150501.csv
-rw-r--r--    0 1001     1001            0 May 13 21:44 x_20150601.csv
-rw-r--r--    0 1001     1001            0 May 13 21:44 x_20150701.csv
-rw-r--r--    0 1001     1001            0 May 13 21:44 x_20150801.csv
-rw-r--r--    0 1001     1001            0 May 13 21:44 x_20150901.csv
-rw-r--r--    0 1001     1001            0 May 13 21:45 x_20151001.csv
-rw-r--r--    0 1001     1001            0 May 13 21:45 x_20151101.csv
-rw-r--r--    0 1001     1001            0 May 13 21:45 x_20151201.csv

% cat file2 

First question: Is there something more glamorous to read just the very last item on the very last line? Would you use cut and sed? This is a redirect of a sftp directoy listing.

Second question: Whatever is in file2, I want to have it read from a sftp batch file to get that exact file.

% cat fetch.sh 

cd u/aaa
!sh read.sh
!< file2 

As you can imagine, sftp doesn't like get provided without any file, so how can I read in file2 to get that file from the sftp server?

 % sftp -b fetch.sh user@pulse
    sftp> #!/bin/sh
    sftp> cd u/aaa        
    sftp> !sh read.sh
    sftp> #!/bin/sh
    sftp> !< file2 
    sftp> get
    You must specify at least one path after a get command.

You can combine all the actions in one command:

sftp user@host:/path/to/file/$(tail -1 file1.txt |tr -s ' ' |cut -d ' ' -f 9)

This will fetch the file into the current working directory. If you need to fetch the file into another directory specify the destination directory as a next argument to the sftp command.

  • This works well! Throwing that in a 'script' means I'll be able to make a cronjob and get the file automatically. thanks! I can't upvote due to reputation so I hope others will in my honor. – Registered User May 15 '15 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.