英文:
why does this sed command only fail from inside an expect script?
问题
我正尝试使用expect脚本来编辑远程机器上的文件。
当从expect脚本中发出sed命令时,它会失败,但当手动登录并运行它时(不使用expect脚本),它可以正常工作。
以下是expect脚本中命令的样子
send "sed -i -E 's/value=[0-9]+/value=9999/' target_file\r"
返回的错误信息是
无效的命令名称"0-9"
但当我手动登录并像这样运行命令时,它可以正常工作
sed -i -E 's/value=[0-9]+/value=9999/' target_file
为什么它在从expect脚本中发出时失败,而其他情况下不会失败,如何使其正常工作?
英文:
I am trying to use an expect script to edit a file on a remote machine.
The sed command fails when issued from within the expect script, yet it works when log on and run it manually (without expect script)
Here is what the command looks like inside the expect script
send "sed -i -E 's/value=[0-9]+/value=9999/' target_file\r"
The error that is returned
invalid command name "0-9"
Yet when I manually log on and run the command like this it works fine
sed -i -E 's/value=[0-9]+/value=9999/' target_file
Why does it fail when issued from the expect script but not otherwise, and how can I make it work?
答案1
得分: 1
方括号需要转义。
这个方法有效:
发送 "sed -i -E 's/value=\[0-9\]+/value=9999/' target_file\r"
英文:
The square brackets need to be escaped.
This works:
send "sed -i -E 's/value=\[0-9\]+/value=9999/' target_file\r"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论