Help on expanding variable from sh

J

jdoe987

This may sound like a simple question - but I am a newbie at perl.
Most of my scripting is in sh. Anyway - I am having problems with the
command below. specifically in the perl command.

for HOST in `ypcat netgroup| grep foobar;; do ping $HOST 2 >> /dev/
null; if [ $? -eq 0 ]; then perl -e 'alarm(3); exec qw/rsh -n \{$HOST}
\ uname -a /'; fi;done

The $HOST variable is only being expanded on the first host. After
that it loops stating:
{$HOST}: unknown host

Any help and an explanation on what the correct syntax is would be
appreciated.
 
M

Michele Dondi

This may sound like a simple question - but I am a newbie at perl.

You welcome!
Most of my scripting is in sh. Anyway - I am having problems with the

You will switch gradually, if needed.
command below. specifically in the perl command.

for HOST in `ypcat netgroup| grep foobar;; do ping $HOST 2 >> /dev/
null; if [ $? -eq 0 ]; then perl -e 'alarm(3); exec qw/rsh -n \{$HOST}
\ uname -a /'; fi;done

Perl usage is really minimal here.
The $HOST variable is only being expanded on the first host. After

So far I think you have a shell quoting problem. To go on topic,
however, you can access the $HOST environment variable directly IN
Perl by means of the %ENV variable. $ENV{HOST} should do the trick.


Michele
 
J

jdoe987

Michele,
Thanks for the advice. Unfortunately, I had already tries the env
option and it does not seem to work. I have tried escaping it as
well. Any other ideas? Thanks again.

perl -e 'alarm(3); exec qw/rsh -n $ENV{4HOST} uname/' - error
generates
$ENV{HOST}: unknown host

perl -e 'alarm(3); exec qw/rsh -n $ENV{$HOST} uname/' - error
generates
$ENV{$HOST}: unknown host

perl -e 'alarm(3); exec qw/rsh -n \$env{'HOST'}\ uname/' - for some
reason this is the only one that works. It reports on the first host
(incorrectly I may add) then spews out
\$env{HOST}\: unknown host

This may sound like a simple question - but I am a newbie at perl.

You welcome!
Most of my scripting is in sh. Anyway - I am having problems with the

You will switch gradually, if needed.
command below. specifically in the perl command.
for HOST in `ypcat netgroup| grep foobar;; do ping $HOST 2 >> /dev/
null; if [ $? -eq 0 ]; then perl -e 'alarm(3); exec qw/rsh -n \{$HOST}
\ uname -a /'; fi;done

Perl usage is really minimal here.
The $HOST variable is only being expanded on the first host. After

So far I think you have a shell quoting problem. To go on topic,
however, you can access the $HOST environment variable directly IN
Perl by means of the %ENV variable. $ENV{HOST} should do the trick.

Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
P

Paul Lalli

Michele,
Thanks for the advice. Unfortunately, I had already tries the env
option and it does not seem to work. I have tried escaping it as
well. Any other ideas? Thanks again.

perl -e 'alarm(3); exec qw/rsh -n $ENV{4HOST} uname/' - error
generates
$ENV{HOST}: unknown host

perl -e 'alarm(3); exec qw/rsh -n $ENV{$HOST} uname/' - error
generates
$ENV{$HOST}: unknown host

perl -e 'alarm(3); exec qw/rsh -n \$env{'HOST'}\ uname/' - for some
reason this is the only one that works. It reports on the first host
(incorrectly I may add) then spews out
\$env{HOST}\: unknown host

Please don't multipost. Please see the answer to this question in the
perl.beginners list/group. Basically, stop using qw// when you mean
qq//. If you really want to avoid the shell (which is the only reason
I know of to pass a list rather than a string), you'll have to build
your list manually:
exec('rsh', '-n', $ENV{HOST}, 'uname');

Paul Lalli
 
J

jdoe987

Paul,
I was simply replying back to Michele and asking for any help on
understanding and addressing the issue. The posting simply an ongoing
thread to the original post. That being said - your suggestion of
using qq// seemed to do the trick. Thanks.
 
P

Paul Lalli

Paul,
I was simply replying back to Michele and asking for any help on
understanding and addressing the issue. The posting simply an ongoing
thread to the original post.

Yes, and because you multi-posted your original question, I wasted
time answering you in perl.beginners without being able to know that
you'd also asked the question here and had started another thread on
it.

Don't multipost. It's rude.

Paul Lalli
 
M

Michele Dondi

Please don't multipost. Please see the answer to this question in the
perl.beginners list/group. Basically, stop using qw// when you mean
qq//. If you really want to avoid the shell (which is the only reason

D'Oh! Hadn't even noticed...


Michele
 
X

Xicheng Jia

This may sound like a simple question - but I am a newbie at perl.
Most of my scripting is in sh. Anyway - I am having problems with the
command below. specifically in the perl command.

for HOST in `ypcat netgroup| grep foobar;; do ping $HOST 2 >> /dev/
null; if [ $? -eq 0 ]; then perl -e 'alarm(3); exec qw/rsh -n \{$HOST}
\ uname -a /'; fi;done

The $HOST variable is only being expanded on the first host. After
that it loops stating:
{$HOST}: unknown host

Any help and an explanation on what the correct syntax is would be
appreciated.

Hi,

You might also want to try Perl 's -s switch on the command line to
pass shell varaibles into Perl:

perl -se 'alarm(3); exec("rsh -n $host uname -a")' -- -
host="$HOST"

(Note: '--' is needed to tell Perl that all switches finished there),

Regards,
Xicheng
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top