问题
I have created one script to copy the local files to the remote folder, the script is working fine outside of if condition but when I enclosed inside the if condition the put command is not working and logged into the remote server using sftp protocol and when exist it's showing the error: put command not found
see what is happening after executing the script
Connected to 10.42.255.209.
sftp> bye
sftp.sh: line 23: put: command not found
Please find the below script.
echo -e;
echo -e "This script is used to copy the files";
sleep 2;
localpath=/home/localpath/sftp
remotepath=/home/destination/sftp/
if [ -d $localpath ]
then
echo -e "Source Path found"
echo -e "Reading source path"
echo -e "Uploading the files"
sleep 2;
sftp username@10.42.255.209
put $localpath/* $remotepath
else
回答1:
In a simple case such as this, you could use scp
instad of sftp
and specify the files to copy on the command line:
scp $localpath/* username@10.42.255.209:/$remotepath/
But if you would rather want to issue sftp commands, then sftp can read commands from its stdin, so you can do:
echo "put $localpath/* $remotepath" | sftp username@10.42.255.209
Or you can use a here document to pass data as stdin to sftp, which might be easier if you want to run several sftp commands:
sftp username@10.42.255.209 << EOF
put $localpath/fileA $remotepath/
put $localpath/fileB $remotepath/
EOF
Finally, you could place the sftp commands in a separate file, say sftp_commands.txt
, and have sftp execute those commands using its -b
flag:
sftp -b ./sftp_commands.txt username@10.42.255.209
来源:https://stackoverflow.com/questions/53761918/sftp-bash-shell-script-to-copy-the-file-from-source-to-destination