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

Someone asked me to write them a shell script (.sh). I don't know if I understand this. Can I just rename a .pl to a .sh and have it work? Or is a shell script something totally different?

Second question is, to determine the number of lines in ANY type of file..is this the best idea?

my $count = 0; open(FILE, "image.gif") or die "we had a problem $!"; while(<FILE>) { $count++; }
Thanks everyone

Replies are listed 'Best First'.
Re: Shell scripts?
by eXile (Priest) on Apr 30, 2004 at 04:19 UTC
    Hi, the difference between a shell script and a perl script is that the first script will/should be interpreted by a shell (sh, bash, ksh, zsh, csh, tcsh ..) and the second one will be interpreted by perl. Perl and Shell-scripting are different languages, and to complicate things further there are different 'dialects' in Shell-scripting. As pointed out, the first line of your script (on UNIX at least) normally contains the interpreter that is used.

    For counting the number of lines, your idea is good. On UNIXes (and in cygwin) there is a special utility for counting words/lines etc. thats called wc (abbrev. for word count). wc -l<filename> should give you the number of lines.
Re: Shell scripts?
by Zaxo (Archbishop) on Apr 30, 2004 at 04:26 UTC

    To the second question, your while loop's $count will depend on the value of $/. That is what perl looks for to determine that a "line" has been read.

    For many binary file types, there is no meaningful notion of a "line". For graphics files, the number of pixel rows might serve, but that must be deduced from information in the file header. Image::Size will help with that.

    The short answer is no, there is no universal line marker for all file types.

    For text files, your idea is fine so long as $/ is correctly set.

    After Compline,
    Zaxo

Re: Shell scripts?
by Plankton (Vicar) on Apr 30, 2004 at 04:03 UTC
    Techically yes. But when some says "write a shell script" they usally mean the first line of the script will be ...
    #!/bin/sh
    ... if some says write a korn shell script the first line is ...
    #!/bin/ksh
    ... if some ask you to write a perl script then ...
    #!/usr/bin/perl
    does that help? Maybe you should search on #! or the phrase she-bang.

    Plankton: 1% Evil, 99% Hot Gas.
Re: Shell scripts?
by perlinux (Deacon) on Apr 30, 2004 at 08:21 UTC
    These are two very different things!
    The Shell Scripting is a great language, I love it.
    See this site for your learning..
    From Learning Perl:
    So, the best you can do is stare at a shell script, figure out what it does, and start from scratch with Perl. Of course, you can do a quick-and-dirty transliteration, by putting major portions of the original script inside system() calls or backquotes. You might be able to replace some of the operations with native Perl: for example, replace system(rm fred) with unlink(fred), or a shell for loop with a Perl for loop. But generally you'll find it's a bit like converting a COBOL program into C (with about the same reduction in the number of characters and increase in illegibility).

    Second answer:
    cat filename | wc -l
    on the Unix shell
    UPDATE:
    or better:
    wc -l filename
    thanks pelagic
      wc -l filenamewill do.

      pelagic

        And if yu really want to do it in Perl that you can always use the Shell module that is standard with Perl:

        use Shell; + my $foo = wc('-l', '.bash_profile'); + print $foo;
        ;-}

        /J\

        Careful, tiger. wc's behavior differs depending on whether it read from stdin or you passed it a file. Observe:
        thulben@bravo:~
        7$ wc -l file
        10 file
        thulben@bravo:~
        8$ cat file | wc -l
        10
        thulben@bravo:~
        9$
        
        So, to get just the line count, you'd either have to do the cat-pipe thing, or pipe the results to awk.

        thor