Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

perl script inside a shell script

by Anish (Novice)
on Dec 12, 2000 at 02:55 UTC ( [id://46168]=perlquestion: print w/replies, xml ) Need Help??

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

This one is weird question,but if answer to this question is YES, then it will help me out a lot.my question is, Is it posiible to call perl script inside a shell script i.e. can I write both script in one file for e.g.
#!/bin/ksh #some shell script code ... .... #perl script begains here #!/usr/bin/perl #some perl script code .. .. .. close(file) exit 0; #end of perl script #continuation of original shell script #this part of shell script will use output from above perl script ... ... ... exit 0; #end of shell script
or I have to write a perl script and shell script in different file and call perl script inside shell script by the flle name(That's what I do right now)

Replies are listed 'Best First'.
Re: perl script inside a shell script
by eg (Friar) on Dec 12, 2000 at 03:05 UTC

    It's strange and terrible and I'm not sure how to get something out of the perl part, but this sort of works (with bash, at least):

    #!/bin/sh echo This is bash i=12 echo $i perl <<__HERE__ print "This is perl\n"; my \$i = $i; print ++\$i . "\n"; __HERE__ echo This is bash again echo $i

    Good luck with it (although I'm not quite sure what you hope to gain.)

      Very nice! As another way of doing it, using quotes around the here-doc terminator saves you from having to backslash all those special characters.
      #!/bin/sh echo This is bash i=12 echo $i perl - $i <<'__HERE__' my $i = shift; print "This is perl\n"; print ++$i . "\n"; __HERE__ echo This is bash again echo $i
      Because << is a redirection to STDIN, in order to add $i to the command line I also had to add - so that perl would know to read the script from STDIN.
      Ewwww.

      I was thinking that it would be pretty trivial to embed Perl one liners in the shell script. This, on the other hand is just ... just .. well, I don't know. Not right. Not even wrong. It just is.

      Hmmm. Barring something like this, or a perl one-liner, I can't think of any easy way to do it in one script that starts as a shell. The shebang (#!) is a one-time-deal, no changing type mid way though.

      BTW, just for curiosity, could you have the base be a Perl program that called shell script stuff? The requirement to have shell and Perl in the same file is somewhat unusual.

      =Blue
      ...you might be eaten by a grue...

Re: perl script inside a shell script
by dchetlin (Friar) on Dec 12, 2000 at 03:44 UTC
    Ah, tailor-made for the little-used `-x' option for perl. Observe:

    [~] $ cat test.sh #!/bin/sh echo "In shell part" FOO=`/usr/bin/perl -x $0` echo <<'__END__' > /dev/null #!/usr/bin/perl -wl print "In perl"; __END__ echo "Perl said: $FOO" echo "Back in shell" [~] $ sh test.sh In shell part Perl said: In perl Back in shell [~] $

    Ain't that fun? See perlrun for details on `-x'.

    -dlc

      I get error testing the script...have not been able to figure out y...any idea? this is waht I get:
      [gii]:/checker>sh -x toy.sh + echo in shell part in shell part + /usr/bin/perl -x toy.sh Semicolon seems to be missing at toy.sh line 4. Unquoted string "echo" may clash with future reserved word at toy.sh l +ine 5. syntax error at toy.sh line 5, near "echo " String found where operator expected at toy.sh line 5, near "echo "bac +k in shell "" (Do you need to predeclare echo?) Execution of toy.sh aborted due to compilation errors. FOO= + echo + echo perl said perl said + echo back in shell back in shell
      I need to call a long perl script from a shell program..it needs to be like that, since the shell program is very big. any ideas? i'm new to this stuff,help!!
        (I realize I'm responding to an old post) #! /bin/sh # the shell script can be arbitrarily long echo $SHELL echo line filtered by pearl | perl -x $0 exit # the previous line must not be removed (this is a void in the script as the shell nor perl will execute) # the following must not be removed #! perl # it can also be the full path of perl - the -x only looks for a #! and perl on the line though. # the following perl script must be come last but can be arbitrarily long while (<>) { s/pearl/perl/; }

        (sorry, I didn' know this would line-wrapt)
        (I realize I'm responding to an old post)
        #! /bin/sh

        # the shell script can be arbitrarily long

        echo $SHELL
        echo line filtered by pearl | perl -x $0

        exit
        # the previous line must not be removed

        (this is a void in the script as the shell nor perl will execute)

        # the following must not be removed
        #! perl
        # it can also be the full path of perl - the -x only looks for a #! and perl on the line though.

        # the following perl script must be come last but can be arbitrarily long

        while (<>) {
        s/pearl/perl/;
        }
(Ovid) Re: perl script inside a shell script
by Ovid (Cardinal) on Dec 12, 2000 at 02:59 UTC
    As far as I am aware, you can't do it that way. The first line of the file, if it begins with #!, is a special command that shells understand to mean "run this script with the program I specify". The #!/usr/bin/perl line later on is just treated as a comment.

    Cheers,
    Ovid

    Update: eg and chipmunk have come up with very neat solutions around the problem. But this does beg the question of why embed Perl in a shell script? Shell scripts are fast, but the overhead of loading the Perl interpreter is going to kill any performance gain of the shell script.

    The only reason I could think of is if you needed a quick hack to a huge shell script. But if it's a huge shell script, it's probably better implemented as a Perl or [insert other language here] program, huh?

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: perl script inside a shell script
by Desdinova (Friar) on Dec 12, 2000 at 10:28 UTC
    Personally I have found that there is nothing that i can do in a shell in script that I cant do in a perl script, even if i have to resort to a system() call. If you are going to have to load perl into memory might as well do the whole thing that way. I also work in an envrioment where there are a couple of different shells in use and this way the scripts work the same for everyone. Just my 2 cents.

      Of course you can do anything in Perl that you can do in a shell script when you can use system. Like so:

      #!/usr/bin/perl system('sh arbitraryshellscript');

      </humor>

Re: perl script inside a shell script
by HamNRye (Monk) on Dec 12, 2000 at 23:32 UTC

    I do this alot as part of maintenance and reporting, and I know that there are basically two ways:
    call "perl -e" with your script inside.
    put an exec call to your perl script.

    I really must say that for any perl script more than 5-6 lines long the first way is no good. The second way works great, but you wind up with the need to have a number of files to run the shell script.

    Also, why not write all of it in perl?? I have not found too many thing that a shell can do that perl cannot... (Although around here we use perl scripts like libraries and allow our shell scripters to call them....) If nothing else, having all of your info in perl saves you the effort of sending a command line and parsing the output.

    Finally, if you can make the shell scripty parts of your file look like comments to perl you could try this:

    exec perl -e "$0" ${1+"$@"}

    Hope this helps....

    ~Hammy

Log In?
Username:
Password:

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

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

    No recent polls found