how to set an expect variable with output of shell command

user2769274 picture user2769274 · Mar 13, 2016 · Viewed 15.4k times · Source

I want to set a variable b in expect file,here initially i did ssh to a machine through this script,in that machine I want to do fetch a value and set the expect variable using following command:

set b [exec `cat /home/a |grep "work"|awk -F '=' '{print $2}'`]


send_user "$b"

file /home/a have following structure:

home=10.10.10.1

work=10.20.10.1

I am trying to use variable b after printing it but after doing ssh script it is giving:

can't read "2": no such variable

while executing

If I put this output in a file name temp in that machine and try to do:

set b [exec cat ./temp]

then also it gives:

cat: ./temp: No such file or directory

If I do send "cat ./temp" it prints the correct output.

Please let me know where I am going wrong.

Answer

Dinesh picture Dinesh · Mar 13, 2016

Single quotes are not quoting mechanism for Tcl, so brace your awk expressions.

% set b [exec cat /home/a | grep "work" | awk -F {=} {{print $2}}]
10.20.10.1

Reference : Frequently Made Mistakes in Tcl