这个sed命令为什么只在expect脚本内部失败?

huangapple go评论53阅读模式
英文:

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"

huangapple
  • 本文由 发表于 2023年6月8日 16:40:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定