Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Difference between a perl script & shell script

by Anonymous Monk
on Dec 19, 2005 at 12:39 UTC ( [id://517700]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Difference between a perl script & shell script

Replies are listed 'Best First'.
Re: Difference between a perl script & shell script
by tirwhan (Abbot) on Dec 19, 2005 at 13:14 UTC

    I don't know what exactly you are looking for, the basic difference is that shell script will be interpreted by the appropriate shell binary (for example /bin/bash, /bin/ksh), whereas the perl script will be compiled and interpreted by the perl binary (e.g. /usr/bin/perl).

    One practical difference is that the shell comes in a variety of flavours and you can never know which shell binary will be executed on a system your shell script gets copied to. Usually shell scripts call /bin/sh , which on many systems is just a symlink to the systems preferred shell. While it is usually not extraordinarily difficult to write shell scripts which are portable between different shell flavours, this does require some knowledge and diligence on the part of the script author. A perl script will always run in perl, which is mostly the same across *NIX systems, so you do not need to worry about incompatibilities between different systems as much (as long as you take care that your script works under the commonly installed version of perl, and not just the most recent one). Thus, for certain usage scenarios, Perl scripts are more portable between systems than shell scripts.

    Another practical difference is that the shell on it's own doesn't do very much, shell scripts are usually used to call external programs (mv,find,ls,sed to name a few). So again you are somewhat dependant on the environment that your script find, because different systems will have different variants of these utilities installed which may exhibit different behaviour. A lot of Perl scripts OTOH do useful work without depending on external processes, so again you have a better chance of easy portability between disparate systems and, depending on what you do, better performance.

    A good answer to your question really depends on why you're asking it :-)


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: Difference between a perl script & shell script
by Hue-Bond (Priest) on Dec 19, 2005 at 12:41 UTC

    In few words, a Perl script is one written in the Perl programming language, while a shell script is one written using commands from some shell, like bash or ksh.

    --
    David Serrano

Re: Difference between a perl script & shell script
by matija (Priest) on Dec 19, 2005 at 13:23 UTC
    It's easier to port a perl program then a shell script.

    (Or as Larry Wall said, "it's easier to port a shell then a shell script")...

      Actualy, one of the first "I love perl" moments, for me came when I was moving programs between an SGI and a SunOs machine, back in the time when Perl was at version 4.19 and dinosaurs roamed the earth.

      For some reason, the user shell on the SunOS (not in general, just on that particular machine) was mandated to be tcsh, and on the SGI it was bash. Or maybe it was the other way around.

      Either way, porting shell scripts was a pain, porting C programs was a pain, but Perl scripts simply copied over. Nifty...

        For some reason, the user shell on the SunOS (not in general, just on that particular machine) was mandated to be tcsh, and on the SGI it was bash. Or maybe it was the other way around.
        I don't get this. Just because a users shell is tcsh doesn't mean that every shell program needs to be in tcsh - if the top line says #!/usr/bin/sh, it will be run in sh, regardless of the users shell.

        The only exception being shells that need to be "sourced" - but you can't write those in Perl anyway (unless your shell is Perl).

        Perl --((8:>*
      It's easier to port a perl program then a shell script.
      OTOH, there's less need to port a shell script than a Perl program. I've never had the need to port a shell script written in the Bourne shell to the C-shell, or from ksh to zsh. And I cannot recall ever to have written a shell program (and I have written many) that didn't work on a version of the shell five years old.

      However, every now and then, I do need to adapt a Perl program because it needs to run on an older version, or because it no longer runs (or no longer runs warnings-free) on a newer version of Perl.

      Furthermore, any Unix will have a shell that's Bourne shell compatible. Not every Unix will have Perl installed - specially not during OS-installation phase.

      Perl --((8:>*
Re: Difference between a perl script & shell script
by JohnMG (Beadle) on Dec 19, 2005 at 15:34 UTC
    The stuff you type into the terminal window is getting interpreted by the shell interactively (as you type it, after you hit the enter key). You may not've ever tried it, but you can do loops in terminal window. Try:
    $ for i in `ls` > do > echo "found this: $i" > done
    (You don't type the '>' characters -- they'll show up automatically.)

    So, one difference is, a shell script could be typed in one line at a time in a terminal window, and it would work. Python has this same sort of interactive "shell". Perl's interpreter isn't really interactive like that (though it does offer the -e option for making "one-liners").

      I usually type my loops on a single line:
      for i in `ls`; do echo "found this: $i"; done
      Perl --((8:>*
      Perl's interpreter isn't really interactive like that
      $ perl print "Look ma, I'm interactive!\n"; __END__ Look ma, I'm interactive! $ _

      --
      David Serrano

        That's not interactive, that's taking the source code via STDIN. There's a difference, namely in that interactive means you run one command, get the return value. Run another, get another return value. This is commonly known as REPL, or a Read Eval Print Loop. You can make one in Perl:

        perl -le '@ret = eval and print "Returned [@ret]" while <STDIN>'

        But it's not quite as fully featured as most interactive shells.

        Interesting. Didn't know you could do that. Thanks.

        A quick comparison of Perl, Python, and shell interactive modes:

        • Perl -- You type in the whole script, line by line, and you get the output only after all lines have been entered and after you've typed __END__ as the last line. A syntax error will dump you back to the command line.
        • shell -- you always get the output (if there is any) right after each line/command. View $? to see the return value of the last command that was run.
        • Python -- You get the output of statements as you run them, just like the shell. Also, it automatically prints out the value of the expression evaluated on the line you just typed.

        IMO, the Python shell is pretty darn interactive. The Perl shell might be nice for one-liners that take up more than one line ;), but it doesn't seem all that interactive to me.

Re: Difference between a perl script & shell script
by Perl Mouse (Chaplain) on Dec 19, 2005 at 13:04 UTC
    • What's the difference between a C program and a Java program?
    • What's the difference between a French novel and an English novel?
    • What's the difference between an Italian opera and a German opera?
    • What's the difference between a Chinese poem and a Japanese poem?
    • What's the difference between a Danish song and a Swedish song?
    • What's the difference between a Perl script and a shell script?
    Perl --((8:>*

Log In?
Username:
Password:

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

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

    No recent polls found