Sftp file for multiple servers

Dear Sirs,
i wish to sftp file , after sftp, empty the directory.

#!/usr/bin/bash
Date='date +%Y%m%d'
source_dir='/tmp/test/output'
server2=$(cat server.txt)
while read -r server2
do 
sftp wasadmin@"$server2" << "_EORS_"
#Remote Script
cd $source_dir
for i in 'ls -ltr *.out |awk '{print $9}'
do 
echo $i
mput *.out\n bye'
mkdir -p $backup/$Date
cp $i $backup/$Date
done

_EORS_
done < server.txt

im getting this error:

[Line 2:](javascript:setPosition(2, 1))
Date='date +%Y%m%d'
^-- [SC2034](https://www.shellcheck.net/wiki/SC2034) (warning): Date appears unused. Verify use (or export if used externally).

thanks youss sirs

did you examine the value of Date ? , here it is as you have defined it

Date='date +%Y%m%d'
echo $Date
date +%Y%m%d

presume you meant

Date=$(date +'%Y%m%d')
echo $Date
20240707

so , replace the defintion as shown
fyi: you can do this INSIDE the <<EORS here document

example

cat <<EORS
Date=$(date +'%Y%m%d')
EOF
Date=20240707

source_dir is not used .... remove it or fix it.

learn how to use the markdown menu option on the dialog box and stop posting everything as text.

image

put your scripts through the shellcheck utility BEFORE posting here
shellcheck /tmp/apple08.bash, if not on your system do it online at shellcheck.net

In /tmp/apple08.bash line 2:
Date='date +%Y%m%d'
^--^ SC2034: Date appears unused. Verify use (or export if used externally).

In /tmp/apple08.bash line 3:
source_dir='/tmp/test/output'
^--------^ SC2034: source_dir appears unused. Verify use (or export if used externally).

For more information:
ShellCheck: SC2034 – foo appears unused. Verify it or export it. -- Date appears unused. Verify use (...

This is completely useless assignment statement. Whenever below you do

while read -r server2
  # rest of your code here
done < server.txt

the server2 variable is ALWAYS overwitten by subsequnt values read from a file.

More like "you should" than "you can" :wink:

sftp is not ssh, thus I'm pretty sure it doesn't interpret for loops nor pipelines of shell commands (also incorrectly used ' ' instead of ` ` - learn the best practices, and NEVER use backticks again for command substitution, always use $( ) instead, as shellcheck suggests).
And for an ordinary bash loop, it should be simply:
for i in *.out;

You are attempting to run mput *.out instead of just put $i - are you sure you'd want to put ALL FILES within EVERY iteration? Seems like an awful waste of bandwidth and time (and it won't work anyway with sftp - not when mixing sftp commands with "normal" shell commands)

backup variable doesn't seem to be defined neither locally nor remotely, so it cannot be referred to as $backup anywhere in this script.

Dear Sirs,

many thanks for g8 advise.

#!/usr/bin/bash
while read -r server2
do
sftp wasadmin@"$server2" << "_EORS_"
cd /tmp/test/output
lcd /tmp/test/output
get *.out
bye
Date=$(date +'%Y%m%d')
mkdir -p /tmp/test/backup/$Date
for i in *.out
do
cp $i /tmp/test/backup/$Date
done
_EORS_
done < server2.txt

sirs, after sftp bye, it seem did not run the date and subsequent commands.
would like to seek g8 advise .
thank youss sirs.

... because the bye has ended the sftp session, but you continue to send (shell-)commands to it.

For multiple files you must use the mget in sftp.

#!/usr/bin/bash
Date=$(date +'%Y%m%d')
mkdir -p "/tmp/test/backup/$Date"
cd "/tmp/test/backup/$Date" || exit

while read -r server2
do

  sftp wasadmin@"$server2" << "_EOSFTP_"
cd /tmp/test/output
mget *.out
bye
_EOSFTP_

done < server2.txt

All files are directly copied to the common /tmp/test/backup/$Date; identical file names will overwrite.
If you want individual /tmp/test/backup/$Date-$server2
Then put this to the server2 loop.

#!/usr/bin/bash
Date=$(date +'%Y%m%d')

while read -r server2
do
  mkdir -p "/tmp/test/backup/${Date}-${server2}"
  cd "/tmp/test/backup/${Date}-${server2}" || continue

  sftp wasadmin@"$server2" << "_EOSFTP_"
cd /tmp/test/output
mget *.out
bye
_EOSFTP_

done < server2.txt

The || exit or || continue are quick failure actions: if the cd command should fail then the sftp command is not run, and files don't land in a wrong directory.

dear Sirs,
Thank yousss so much sirs for great expertise. truly appreciate it.

thank youusss sirs