Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

calling perl script from shell script

by shilpam (Sexton)
on Mar 26, 2004 at 12:56 UTC ( #339996=perlquestion: print w/ replies, xml ) Need Help??
shilpam has asked for the wisdom of the Perl Monks concerning the following question:

I need to execute a perl script from a shell script. If it is success, I need to do a few more things else on error it should exit the shell script with an error code. My problem is not with running the perl script or getting the status code but the if-then-else part of the script. No matter what, it goes into the else part and prints "failure". Is the syntax of if-then-else ok? I am pasting the code below.
#!/usr/bin/sh cd go_to_dir_where_perl_program_exists /usr/bin/perl perlscript.pl echo "Status String is $?" if( $? -eq 0) then echo "success" # do some more stuff else echo "failure" # do some more stuff

Comment on calling perl script from shell script
Download Code
Re: calling perl script from shell script
by pelagic (Curate) on Mar 26, 2004 at 13:05 UTC
    Is this code complete or did you just forget the fi?
    Do a simple test like
    /usr/bin/perl perlscript.pl ret=$? echo "Status String is $?" if [ $ret -eq 0 ] then echo "case A" else echo "case B" fi

    pelagic
    update
    Your are testing the $? of the echo stmt ... ;))

    -------------------------------------
    I can resist anything but temptation.
      I tried the piece of code you have posted, but it gives me error:
      ./new_script.sh: ret: not found
      Status String is 1
      ./new_script.sh: test: argument expected



      Then I declared ret as $ret=$?, then I got another error:
      ./new_script.sh: =0: not found
      Status String is 1
      ./new_script.sh: test: argument expected


        Probably because you've got ret = $? or some other variant with spaces around the =. And the second is just plain wrong, as the $ is for interpolating in shell not a universal variable prefix. At any rate, it's not a perl problem; you need to look into something like Learning the bash Shell (ISBN 1565923472) or the Z shell intro.

        Exactly what shell are you using? I run it on solaris, having used following code
        #!/usr/bin/sh echo /usr/bin/perl perlscript.pl ret=$? echo "Status String is $?" if [ $ret -eq 0 ] then echo "case A" else echo "case B" fi
        and it gave me
        Status String is 0 case A

        pelagic

        -------------------------------------
        I can resist anything but temptation.
Re: calling perl script from shell script
by redlemon (Hermit) on Mar 26, 2004 at 14:31 UTC
    apart from forgetting a fi, your using the wrong braces: (something) calls "something" in a subshell, which always returns true. The syntax you're looking for is:
    if test $? -eq 0 then else fi
    or alternatively
    if [ $? -eq 0 ] then else fi
    Use square braces in stead of round ones. finally, it might be easier to use:
    cd go_to_dir_where_perl_program_exists && if /usr/bin/perl perlscript.pl then echo "success" # do some more stuff else echo "failure" # do some more stuff fi
Re: calling perl script from shell script
by Plankton (Priest) on Mar 26, 2004 at 16:32 UTC
    shilpam,

    You might find this node helpful.

    Plankton: 1% Evil, 99% Hot Gas.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-09-15 04:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (145 votes), past polls