Bash Expect、sed和变量替换
我从bash脚本调用Bash Expect、sed和变量替换,bash,sed,expect,Bash,Sed,Expect,我从bash脚本调用expect脚本,并传递一个用于sed字符串替换的参数 但是它在sed语句中错误地删除了变量的b/c。有没有关于如何解决这个问题的建议?我试过逃跑,但没有成功 参数已成功传递(cde) 守则: #!/usr/bin/expect -f # ./sshlogin.exp uptime set hosts {myhost.com} set user root set password xxxx set mount [lindex $argv 0] foreach vm $host
expect
脚本,并传递一个用于sed
字符串替换的参数
但是它在sed
语句中错误地删除了变量的b/c。有没有关于如何解决这个问题的建议?我试过逃跑,但没有成功
参数已成功传递(cde)
守则:
#!/usr/bin/expect -f
# ./sshlogin.exp uptime
set hosts {myhost.com}
set user root
set password xxxx
set mount [lindex $argv 0]
foreach vm $hosts {
set timeout -1
# now ssh
spawn ssh $user@$vm -o StrictHostKeyChecking=no
match_max 100000 # Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
# send blank line (\r) to make sure we get back to gui
expect "]# "
send "sed -e -i 's/abc/${mount}/g' /my/files.new\r"
expect "]# "
sleep 1
send -- "exit\r"
expect eof }
错误:
# sed -e 's/abc/cde
> /g' /my/files.new
sed: -e expression #1, char 37: unterminated `s' command
什么坏了
以下是无效的调用:
sed -e -i 's/abc/${mount}/g'
它是无效的,因为:
-e
标志不关联${mount}
可能包含换行符或前导/尾随空格字符-i
标志提供了一个参数(例如空字符串),以便它可以与非GNU sed一起使用string trim
从mount变量中去除任何换行符或前导/尾随空格set mount [string trim [lindex $argv 0]]
# ...
send "sed -i'' -e 's/abc/${mount}/g' /my/files.new\r"
@CodeGenome感谢您的回答,但我得到以下错误:
#sed-I'-e's/abc/cde>#'/my/files.new sed:-e expression#1,char 37:unterminated
s'命令`@CodeGenome运行时${mount}
的值是正确的,因为错误显示了这一点“cde
@heemayl感谢您的评论。我收到以下错误:#sed-e-I's#abc#cde>#g'/my/files.new sed:-e expression#1,char 1:未知命令:
-'`