Help on expanding variable from sh

Discussion in 'Perl Misc' started by jdoe987@gmail.com, May 2, 2007.

  1. Guest

    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.
    , May 2, 2007
    #1
    1. Advertising

  2. On 2 May 2007 12:36:56 -0700, wrote:

    >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,
    Michele Dondi, May 2, 2007
    #2
    1. Advertising

  3. Guest

    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

    On May 2, 3:48 pm, Michele Dondi <> wrote:
    > On 2 May 2007 12:36:56 -0700, wrote:
    >
    > >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,
    , May 3, 2007
    #3
  4. Paul Lalli Guest

    On May 3, 10:07 am, wrote:
    > 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
    Paul Lalli, May 3, 2007
    #4
  5. Guest

    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.


    On May 3, 10:36 am, Paul Lalli <> wrote:
    > On May 3, 10:07 am, wrote:
    >
    >
    >
    > > 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
    , May 3, 2007
    #5
  6. Paul Lalli Guest

    On May 3, 10:48 am, wrote:
    > 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
    Paul Lalli, May 3, 2007
    #6
  7. On 3 May 2007 07:36:59 -0700, Paul Lalli <> wrote:

    >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
    --
    {$_=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,
    Michele Dondi, May 3, 2007
    #7
  8. Xicheng Jia Guest

    On May 2, 3:36 pm, wrote:
    > 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
    Xicheng Jia, May 3, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Menolo

    Expanding table help

    Menolo, Feb 5, 2005, in forum: HTML
    Replies:
    9
    Views:
    1,763
    Menolo
    Feb 8, 2005
  2. idle
    Replies:
    8
    Views:
    283
    Max M
    Apr 9, 2008
  3. Daniel Schoch

    expanding environment variable

    Daniel Schoch, Mar 3, 2009, in forum: Ruby
    Replies:
    8
    Views:
    164
    Peña, Botp
    Mar 4, 2009
  4. Tyson Marchuk
    Replies:
    7
    Views:
    122
    Brian McCauley
    Feb 24, 2004
  5. Greg
    Replies:
    2
    Views:
    112
Loading...

Share This Page