Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quote a shell variable in a TCL-expect string

Tags:

awk

expect

I'm using the following awk command in an expect script to get the gateway for a particular destination

route | grep $dest | awk '{print $2}'

However the expect script does not like the $2 in the above statement.

Does anyone know of an alternative to awk to perform the same function as above? ie. output 2nd column.

like image 371
jmac Avatar asked Feb 14 '11 16:02

jmac


2 Answers

You can use cut:

route | grep $dest | cut -d \  -f 2

That uses spaces as the field delimiter and pulls out the second field

like image 149
Alex Avatar answered Sep 27 '22 16:09

Alex


To answer your Expect question, single quotes have no special meaning to the Tcl parser. You need to use braces to protect the body of the awk script:

route | grep $dest | awk {{print $2}}

And as awk can do what grep does, you can get away with one less process:

route | awk -v d=$dest {$0 ~ d {print $2}}
like image 45
glenn jackman Avatar answered Sep 27 '22 16:09

glenn jackman