0

I need to use if function to filter out required files only when using SFTP to copy files to my server from a remote server. Here is my try to get the all data inside /filesnew.

#!/bin/bash


files=`sshpass -p 'XXX' sftp -P 2222 [email protected]<<EOF
cd /filesnew
ls
EOF`

files=`echo $files|sed "s/.*sftp> ls//"`



(
  echo cd /filesnew
  for file in $files; do
    echo get $file /data/processedfiles/$file
  done
) |sshpass -p 'XXX' sftp -P 2222 [email protected]

I need to filter out the files which are starting with "USER".

ex:
If($files==*USER*) then
echo get $file /data/processedfiles/$file

Can someone show me how to do this?

0

1 Answer 1

3

Using sftp to get all files that match the pattern /filesnew/USER* (filenames starting with the literal string USER in the directory /filesnew) and store these locally in the directory /data/processedfiles:

mkdir -p /data/processedfiles || exit

sftp -P 2222 [email protected] <<'END_SFTP'
get /filesnew/USER* /data/processedfiles
END_SFTP

Note that there is no reason to list the files. You also don't have to mention the destination filename on the local host, just the destination directory, and only if it's different from the current directory.

You may skip the initial mkdir command if you are confident that the local destination directory always exists.

As an additional note, it may be worth mentioning that if is a "reserved word", part of "an if statement", and not a function in the shell.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.