Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to escape a dollar sign in eval

412 views
Skip to first unread message

C. Ulrich

unread,
Mar 28, 2002, 5:31:49 AM3/28/02
to
Hi, I've got a quick question. I've got a line that should execute a
particular command. Problem is, the command has a dollar sign in it
that eval is trying to treat as a Tcl variable and I cannot figure out
how to escape it so that everything within the double quotes arrives
at the commandline untouched. Any hints? The errant line is as
follows.

eval exec "ifconfig | grep $eth_if | awk \'{print $1}\'"

--C. Ulrich

Giovanni Cristelli

unread,
Mar 28, 2002, 6:20:59 AM3/28/02
to
"C. Ulrich" wrote:

Instead of using awk, try to use the command output:

set values {}
set out [eval exec "ifconfig | grep $eth_if"]
foreach line [split $out \n] {
lappend values [lindex $line 0]
}

Giovanni

Arjen Markus

unread,
Mar 28, 2002, 7:15:40 AM3/28/02
to

Use a backslash before $1 as well. The braces { } loose their
special meaning inside "" (or that is what I have noticed):

eval exec "ifconfig | grep $eth_if | awk \'{print \$1}\'"

Regards,

Arjen

Richard Suchenwirth

unread,
Mar 28, 2002, 12:19:12 PM3/28/02
to
Giovanni Cristelli <cris...@sodalia.it> wrote:
>> eval exec "ifconfig | grep $eth_if | awk \'{print $1}\'"
>[...]

>Instead of using awk, try to use the command output:
>
>set values {}
>set out [eval exec "ifconfig | grep $eth_if"]
>foreach line [split $out \n] {
> lappend values [lindex $line 0]
>}

Then again, a simple grep can also be handled by Tcl built-in:

foreach line [split [exec ifconfig] \n] {
if {[regexp $eth_if $line]} {lappend values [lindex $line 0]}
}
--
Best regards, Richard Suchenwirth


C. Ulrich

unread,
Mar 29, 2002, 2:51:32 AM3/29/02
to
Thanks to all that replied, I think I've got an idea of what to do
now.

(And I should probably quit trying to use external commands for stuff
that Tcl can do... :P)

--C. Ulrich

Kevin Rodgers

unread,
Apr 2, 2002, 7:58:21 PM4/2/02
to
Arjen Markus wrote:
>
> Use a backslash before $1 as well. The braces { } loose their
> special meaning inside "" (or that is what I have noticed):
>
> eval exec "ifconfig | grep $eth_if | awk \'{print \$1}\'"

And (as the good folks in comp.unix.shell would point out) you can do
without the grep process:

eval exec "ifconfig | awk \'/$eth_if/ {print \$1}\'"

--
Kevin Rodgers <kev...@ihs.com>

Dan Smart

unread,
Apr 2, 2002, 11:43:15 PM4/2/02
to
Kevin Rodgers <kev...@ihs.com> wrote in news:3CAA53AD...@ihs.com:

And as this denizen of comp.lang.tcl will point out, you don't need the awk
either:

set f [open "|ifconfig" "r"]
while {[gets $f line] >= 0} {
if {[string match $eth_if $line] > 0} {
puts [lindex $line 0]
}
}
close $f

Note that this code was typed straight into the newsreader without benefit
of a tclsh or a unix box, and thus is worth almost as much as the paper it
is printed on.

Dan "Disk Lamer" Smart
--
Dan Smart. C++ Programming and Mentoring.
cpp...@dansmart.com
ADDvantaged

Donal K. Fellows

unread,
Apr 8, 2002, 6:36:21 AM4/8/02
to
Dan Smart wrote:
> And as this denizen of comp.lang.tcl will point out, you don't need the awk
> either:
>
> set f [open "|ifconfig" "r"]
> while {[gets $f line] >= 0} {
> if {[string match $eth_if $line] > 0} {

Surely that'd be [regexp $eth_if $line] or [string first $eth_if $line]?

> puts [lindex $line 0]

Is ifconfig output regular enough to do that, or is a [split] needed?

> }
> }
> close $f

Donal.
--
Donal K. Fellows http://www.cs.man.ac.uk/~fellowsd/ fell...@cs.man.ac.uk
make: *** No rule to make target `war'. Stop.

Dan Smart

unread,
Apr 8, 2002, 10:53:31 PM4/8/02
to
"Donal K. Fellows" <fell...@cs.man.ac.uk> wrote in
news:3CB172A5...@cs.man.ac.uk:
> Dan Smart wrote:
>> And as this denizen of comp.lang.tcl will point out, you don't need
>> the awk either:
>>
>> set f [open "|ifconfig" "r"]
>> while {[gets $f line] >= 0} {
>> if {[string match $eth_if $line] > 0} {
>
> Surely that'd be [regexp $eth_if $line] or [string first $eth_if
> $line]?
>
Well yes, but only if you wanted it to work :-(
I'm fairly sure I was thinking 'first', I just seem to have typed 'match'.
Lets face it, "[string thing $foo $bar] > 0" makes litle sense when "thing"
is "match".

>> puts [lindex $line 0]
>
> Is ifconfig output regular enough to do that, or is a [split] needed?
>
I think so, but my Linux box was at the time a pile of interesting bits.
Things have moved on however, its now vaguely PC shaped, and most of the
tab "A"s are inserted into there corresponding slot "B"s. All I need to do
now is decide which Linux variant to install.
>> }
>> }
>> close $f
>
> Donal.

Dan "Excuse me, do you have a light, I've run out of firsts" Smart

0 new messages