Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

exchanging data between shell script and perl -e

by Sinister (Friar)
on Jul 25, 2001 at 15:52 UTC ( [id://99632]=perlquestion: print w/replies, xml ) Need Help??

Sinister has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I'm trying to build a little shell script which will use a perl -e line for parsing purposes. It will return 1 or 0 if the parsed succeeded or failed.
It sorta boils down to the following script.
... a=`big old | lump of | piped | apps` b=`big old | lump of | piped apps | doing something | different` c=`perl -e 'if ( $ENV{a} < $ENV{b} ) { print "1\n"; } else { print "0\ +n"; }'` ...
Unfortunatly this doesn't seem to work...
Any suggestions ?!

Sinister greetings.
perldoc -q $_

Replies are listed 'Best First'.
Re: exchanging data between shell script and perl -e
by kschwab (Vicar) on Jul 25, 2001 at 16:32 UTC
    I suspect that this doesn't work because the "a" and "b" variables aren't in the environment. Try adding "export a;export b" in between the "b=" and "c=" lines.

    Or you could just pass them on the command line:

    c=`perl -e 'if ( $ARGV[0] < $ARGV[1]} ) { print "1\n"; } else { print +"0\"; }' $a $b`
    Or, since you are using perl anyway, replace this toplevel shell script with a perl script, and open the pipes from perl.

      The export works just fine! Thank you!

      <update>
      setenv works even beter!
      </update>


      Sinister greetings.
      perldoc -q $_
        export and setenv are both doing the same thing: adding the variable to your environment, making them accessible to other processes running under the same environment.

        You use one or the other depending on what shell you are using. The Bourne shell, Korn Shell, bash etc. support 'export'. The C shell and its variants (tcsh etc.) support 'setenv'.

        I am not sure if any of the shells support both of them, though.

Re: exchanging data between shell script and perl -e
by Jouke (Curate) on Jul 25, 2001 at 15:57 UTC
    First off, you can pass data to perl using normal piping like:
    ls -l |perl -ne "print if /bla/"
    Second, if you want to let Perl exit with a status code, you can use exit.

    I hope this answers your question.

    Jouke Visser, Perl 'Adept'
    Using Perl to help the disabled: pVoice and pStory
      This is one option, but I sort of have my mind set on the option I mentioned...

      If, for what reason what so ever this can't be done I will certainly use this option. But I figured the first to be more readable...

      Sinister greetings.
      perldoc -q $_
Re: exchanging data between shell script and perl -e
by converter (Priest) on Jul 25, 2001 at 18:46 UTC

    The "proper" way to do this, from the bash-scripting presepective, is probably to use exit() so that you my test the exit status just as you would with other commands:

    $ a=foo b=bar; export a b; perl -le 'exists $ENV{a} && exists $ENV{b} +? exit 0 : exit 1' && echo 'success' success

    You can also find the exit status in the shell's $? variable:

    $ a=foo b=bar; export a b; perl -le 'exists $ENV{a} && exists $ENV{b} +? exit 0 : exit 1'; echo "exit status: $?" exit status: 0

    conv

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://99632]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-23 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found