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).
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"
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.
#!/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.