我使用基于 linux 的操作系统。比方说,我有一个服务器包含 10 个文件,如 tmp1 .... tmp10,我想将三个特定文件复制到我的本地机器,比如 tmp3、tmp7 和 tmp10。在这种情况下,我必须使用“scp-command”三次,其中我还必须输入密码三次。问题是,我必须经常做这个过程,所以我“写”了下面的脚本。我的问题是,脚本运行时没有任何错误消息,但文件没有被复制。
echo "insert path of source:" #prompt to enter the path of files you want to copy
read SOURCE # saving the path in the variable SOURCE
echo "insert path of target:" #prompt to enter the path, where you want to past the copied files
read TARGET # saving the path in the variable SOURCE
echo "Insert the port" # prompt to enter the port of the server
read port # saving the port in the variable PORT
echo "Password?" # asking for password
read -s -a PASSWORD # saving the password in the variable PASSWORD
x=(tmp1 tmp2) # An array contains the files i want to copy.
for i in "${x[@]}" # A for-loop to copy each of the files in the array (x) from the SOURCE to the TARGET
do
echo "the file $i" # just to check if the array has been read.
#!/usr/bin/expect -f # to read the expect-programm
expect -c "
spawn /usr/bin/scp -P $prot $SOURCE/$i $TARGET
expect {
"Password:" { send $PASSWORD\r\n; interact }
eof { exit }
}
exit
"
done # End of the for-loop
PASSWORD=0 # To delete the variable PASSWORD
先感谢您!!