问题描述
我现在正在尝试使用过程调用"将值从 TK 传递到 cshell 脚本......如下所示.
I am trying pass value from TK to cshell script using "procedure call" now.... as follow.
proc Run {} {
global passedvalue
## to see what value it has for passedvalue
puts $passedvalue
exec sed -i {s/ABC/$passedvalue/g} runme.sh
exec /bin/csh -c ./runme.sh >@stdout 2>@stderr
}
我正在通过新的传递值更改具有 ABC 值的行.puts"工作并正确打印passedvalue的值.但它不适用于 sed 并且它给出了
I am changing a line which has value ABC by new passedvalue."puts" works and prints the value of passedvalue properly.But it does not work for sed and it gives
错误:程序未定义变量
请告诉我我哪里做错了.
Please let me know how where I am doing wrong.
我也尝试过使用字符串映射,但也确实有效……我可能做错了什么.
I have tried using string map as well but did work either...I might be doing something wrong.
推荐答案
大括号禁止变量替换.如果您希望在调用 exec
之前扩展 $passedvalue
,则需要使用其他一些引用机制.
Curly braces inhibit variable substitution. If you want $passedvalue
to be expanded before calling exec
, you'll need to use some other quoting mechanism.
例如,您可以使用双引号:
For example, you could use double quotes:
exec sed -i "s/ABC/$passedvalue/g" runme.sh
但是,您需要添加一些额外的防弹功能.例如,如果 $passedvalue
中有一个 /
,您将向 sed
发送一个格式错误的表达式.
You will need to add some extra bullet-proofing, however. For example, if $passedvalue
Has a /
in it, you will send a mal-formed expression to sed
.
这篇关于TCL/TK 中的 SED 和 TCL 中的任何其他等效命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!