1

I'm implementing agent script bash to pull files from the remote server with SFTP service. The script must:

  1. connect SFTP
  2. file listing
  3. cycling on files found
  4. get every file and copy agent side
  5. after that files copied must be deleted

The script is followed:

 #!/bin/bash

SFTP_CONNECTION="sftp -oIdentityFile=/home/account_xxx/.ssh/service_ssh user@host"

DEST_DATA=/tmp/test/data/

# GET list file by ls command ###############

$SFTP_CONNECTION
$LIST_FILES_DATA_OSM1 = $("ls fromvan/test/data/test_1")
echo $LIST_FILES_DATA_OSM1


for file in "${LIST_FILES_DATA_OSM1[@]}"
       do
                $SFTP_CONNECTION get $file $DEST_DATA
                $SFTP_CONNECTION rm $file
       done

I tried the script but it seems that the connection and command execution (ls) are distinct on thread separated. How can I provide command sequential as described above ?

Screenshoot:

enter image description here

Invalid find command

enter image description here

SSH it seem not available

enter image description here

RSYNC result to take the files is the followed:

enter image description here

Thanks

Stefano
  • 1,439
  • 4
  • 23
  • 38

2 Answers2

1

First of all, I would recommend the following syntax changes:

#!/bin/bash

sftp_connection() { 
    sftp -oIdentityFile=/home/account_xxx/.ssh/service_ssh user@host "$@"; 
}

Dest_Data=/tmp/test/data/

# GET list file by ls command ###############

sftp_connection
List_Files_D_OSM1=$("ls fromvan/test/data/test_1")
echo "$LIST_FILES_DATA_OSM1"


for file in "${LIST_FILES_DATA_OSM1[@]}"
    do
        sftp_connection get "$file" $Dest_Data
        sftp_connection rm "$file"
    done
  • Quoting $file and $List_Files_D_OSM1 to prevent globbing and word splitting.
  • Assignments can't start with a $, otherwise bash will try to execute List_Files_D_OSM1 and will complain with a command not found
  • No white spaces in assignments like List_Files_D_OSM1 = $("ls fromvan/test/data/test_1")

You can use ShellCheck to catch this kind of errors.


Having said that, it is in general not a good idea to use ls in such way.

What you can use instead is something like find. For example:

find . -type d -exec echo '{}' \;

M. Becerra
  • 639
  • 6
  • 21
  • @Stefano what do you mean? How does your code look exactly? You could edit it to your question – M. Becerra Nov 21 '17 at 15:38
  • it mean that sftp protocol can't perform the command because it not available, My code is the same as described. If I perform the script it try the connetion ad show to screen sftp> . it's waiting the command. I update the post with screenshoot – Stefano Nov 21 '17 at 15:47
  • This is still really unfortunate from a syntax and best-practices perspective. See [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050) re: why commands shouldn't be stored in strings, and [Why you shouldn't parse the output of `ls`](http://mywiki.wooledge.org/ParsingLs), and [the pertinent POSIX standards re: variable names](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html) -- all-caps names are reserved for variables with meaning to POSIX-specified tools, whereas names with at least one lower-case character are guaranteed safe for application use. – Charles Duffy Nov 21 '17 at 16:30
  • If you want to shorten things, define a function: `sftp_connection() { sftp -oIdentityFile=/home/account_xxx/.ssh/service_ssh user@host "$@"; }`, and then call `sftp_connection rm "$file"` – Charles Duffy Nov 21 '17 at 16:32
0

Use a different client. lftp supports sftp as a transport, and has a subcommand for mirroring which will do the work of listing the remote directory and iterating over files for you.

Assuming your ~/.ssh/config contains an entry like:

Host myhost
    IdentityFile /home/account_xxx/.ssh/service_ssh

...you can run:

lftp -e 'mirror -R fromvan/test/data/test_1 /tmp/test/data' sftp://user@myhost
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Original answer didn't tell lftp to use the sftp transport. That's a rather critical piece -- be sure you note the edit. – Charles Duffy Nov 21 '17 at 16:46