For Programmers: Free Programming Magazines  


Home > Archive > AWK > July 2004 > SSH/(g)awk









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author SSH/(g)awk
Rob

2004-07-21, 3:55 pm

Good afternoon,

This is a bit of a joint ssh/awk question.

The following command returns the correct process id:
ps -ef | gawk '$8 ~ /X/ { print $2 }'
However, if I try to execute it remotely as:

ssh node "ps -ef | gawk '$8 ~ /X/ { print $2 }'"
The command fails with error:
gawk: cmd. line:1: ~ /X/ { print }
gawk: cmd. line:1: ^ parse error


Also of note
ssh node "ps -ef | gawk '/X/ { print $2 }'"
works (though it matches more than I want). So I am unsure whether
it is the "$8" or the "~" part that causes the problem. A brief glance
at the ssh manual mentions that "~" is an escape character. I tried
using "~~" instead but had the same problem.

Any advice is appreciated.
Rob
Kenny McCormack

2004-07-21, 3:55 pm

In article <6eb3235e.0407210754.7b2bb0b0@posting.google.com>,
Rob <rkane@travel-net.com> wrote:
....
> Also of note
> ssh node "ps -ef | gawk '/X/ { print $2 }'"
> works (though it matches more than I want). So I am unsure whether
>it is the "$8" or the "~" part that causes the problem. A brief glance
>at the ssh manual mentions that "~" is an escape character. I tried
>using "~~" instead but had the same problem.


Actually, your question has nothing to do with either ssh or gawk.
It is a shell question.

You need to escape the $ in $8 somehow.

Dimitri Maziuk

2004-07-21, 8:55 pm

Kenny McCormack sez:
> In article <6eb3235e.0407210754.7b2bb0b0@posting.google.com>,
> Rob <rkane@travel-net.com> wrote:
> ...
>
> Actually, your question has nothing to do with either ssh or gawk.
> It is a shell question.
>
> You need to escape the $ in $8 somehow.


Care to explain why? (Specifically, why it works on local machine
without those escapes.)

Dima
--
Sufficiently advanced incompetence is indistinguishable from malice.
Richard E. Silverman

2004-07-21, 8:55 pm

>>>>> "DM" == Dimitri Maziuk <dima@127.0.0.1> writes:
[color=darkred]

DM> Care to explain why? (Specifically, why it works on local machine
DM> without those escapes.)

Because when you execute it remotely via SSH, the command text is
intepreted by two shells in succession -- once locally before it's passed
to ssh, and once on the server since sshd handles "exec" requests by
passing the command to the target account's shell with "<shell> -c ...".

--
Richard Silverman
res@qoxp.net

Patrick TJ McPhee

2004-07-21, 8:55 pm

In article <slrncftvjs.q5h.dima@localhost.localdomain>,
Dimitri Maziuk <abuse@127.0.0.1> wrote:
% Kenny McCormack sez:

[...]

% > You need to escape the $ in $8 somehow.
%
% Care to explain why? (Specifically, why it works on local machine
% without those escapes.)

Actually, it doesn't work on the local machine without those escapes.
What you execute on the local machine is completely different from
the above command. You're typing

ps -ef | gawk ' $8 ~ /X/ { print $2 }' # 1

but what you're giving to ssh is

"ps -ef | gawk ' $8 ~ /X/ { print $2 }'" #2

in 1, the awk program is contained in single quotes, which have the
effect of protecting it from shell expansion. In 2, the entire
command is contained in double quotes, which has the effect of protecting
most things from shell expansion, but not everything. In particular,
$8 and $2 are expanded within the double quotes. You can fix it
by escaping them with \

"ps -ef | gawk ' \$8 ~ /X/ { print \$2 }'" #2

You'll have similar problems if you try to pass a string in a
similarly executed script.
--

Patrick TJ McPhee
East York Canada
ptjm@interlog.com
William Park

2004-07-22, 3:55 am

In <comp.lang.awk> Dimitri Maziuk <dima@127.0.0.1> wrote:
> Kenny McCormack sez:
>
> Care to explain why? (Specifically, why it works on local machine
> without those escapes.)


Hint:
echo $8
echo '$8'
echo "'$8'"

--
William Park, Open Geometry Consulting, <opengeometry@yahoo.ca>
Toronto, Ontario, Canada
Dimitri Maziuk

2004-07-22, 3:55 pm

Patrick TJ McPhee sez:
....
> in 1, the awk program is contained in single quotes, which have the
> effect of protecting it from shell expansion. In 2, the entire
> command is contained in double quotes, which has the effect of protecting
> most things from shell expansion, but not everything.


Unbelieveable. I've been doing this crap for years and I never
noticed it does that. Or maybe I did and forgot all about it --
there must be a reason why I never do "foo 'bar'"...

Dima
--
I'm going to exit now since you don't want me to replace the printcap. If you
change your mind later, run -- magicfilter config script
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com