Need Help on SFTP login from another server

Hi Experts,

I am writing one shell script. Below is the requirement

  1. need to login to the SFTP server, go to the particular folder
  2. take the file count in that folder and assign it to variable
  3. came out of the SFTP server and check the condition, if the file count is less than the required count exit the script other wise further processing
  4. then we need to move the files from SFTP to another server.
  5. Need to check the file count in another server if all the files are not copied print the error msg and exit the script.

please help me with the sudo code. I am trying the below code it is getting error.

sftp ${SftpPort} ${userid}@${host name} "cd ${SFTP_SRC_DIR} <<sftp_test_EOF
count_src=`ls -ltr *.csv} | wc -l`
sftp_test_EOF"

echo $count_src

if [ $count_src -lt 3 ]
then
        echo "All CSV files not present in sftp"
        exit 1

fi

You can follow below steps:

  1. Create a SFTP batch file:-
 
cat > sftp.cntrl
cd <destination dir>
ls -l *.csv 
  1. Run the SFTP batch file and get the count:-
 
count_src=`sftp -b sftp.cntrl $user@$host | awk '!/sftp/ { print; } ' | wc -l` 
1 Like