我编写了这个小脚本,用于从远程服务器获取多个文件到我的主机:
#! /usr/bin/expect -f
spawn scp \
user@remote:/home/user/{A.txt,B.txt} \
/home/user_local/Documents
expect "password: "
send "somesecretpwd\r"
interact
这很好用,但是当我想在这些文件之间创建新的行:
user@remote:/home/user/{A.txt,\
B.txt} \
我收到以下错误:
scp: /home/user/{A.txt,: No such file or directory
scp: B.txt}: No such file or directory
我试过了:
user@remote:"/home/user/{A.txt,\
B.txt}" \
得到:
bash: -c: line 0: unexpected EOF while looking for matching `"'
bash: -c: line 1: syntax error: unexpected end of file
cp: cannot stat 'B.txt}"': No such file or directory
或者这个:
"user@remote:/home/user/{A.txt,\
B.txt}" \
在开头就得到同样的错误。
如何以多行编写文件,以便程序正常运行?我需要这个以便更好地阅读所选文件。
编辑: 仅将本地用户名更改为user_local
答案 0 :(得分:1)
在Tcl中(因此Expect)\<NEWLINE><SPACEs>
将被转换为单个<SPACE>
,因此您不能将不包含空格的字符串写入多行。
% puts "abc\
def"
abc def
% puts {abc\
def}
abc def
%
答案 1 :(得分:1)
假设文件名真的比较长(否则不多)你可以使用这样的几个变量:
<filter>
答案 2 :(得分:0)
对于想要仅使用expect来解决类似问题的人:
您可以编写文件列表,然后将所有文件连接到一个字符串。
以下是代码:
#! /usr/bin/expect -f
set files {\ # a list of files
A.txt\
B.txt\
C.txt\
}
# will return the concatenated string with all files
# in this example it would be: A.txt,B.txt,C.txt
set concat [join $files ,]
# self made version of concat
# set concat [lindex $files 0] # get the first file
# set last_idx [expr {[llength $files]-1}] # calc the last index from the list
# set rest_files [lrange $files 1 $last_idx] # get other files
# foreach file $rest_files {
# set concat $concat,$file # append the concat varibale with a comma and the other file
# }
# # puts "$concat" # only for testing the output
spawn scp \
user@remote:/home/doublepmcl/{$concat} \
/home/user_local/Documents
expect "password: "
send "somesecretpwd\r"
interact