Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

perl callable from bash?

by hsmyers (Canon)
on Apr 13, 2009 at 00:50 UTC ( #757140=perlquestion: print w/ replies, xml ) Need Help??
hsmyers has asked for the wisdom of the Perl Monks concerning the following question:

I've just been thrown into the unix pool with little to defend myself except perl. The problem I have is that I need to call two perl scripts from a bash script. Something like:
#!/bin/sh filename=$1 firstscript $filename secondscript $filename
Problem is while I've figured out how to make both firstscript and secondscript executable (chmod 777 and mv to /bin), when I run this script (./bl filename) it tells me that it has no idea of what I'm talking about (comforting, why should it just be me...) The error messages are:
': No such file or directory .links ': No such file or directory
Which I at first thought had to do with not getting the variable right--- but a little detective work convinced me that what it couldn't find were the two scripts.

So does anyone have a recipe for installing perl scripts for shell use?

Update: Great thanks to all advisors! For those who come after with same question, here is what worked.
  1. magic chmod number is 755. Apply to all scripts to be called/used.
  2. move to /usr/bin (typically without extension.)
  3. insure that any reference to the called perl scripts is path-complete.

--hsm

"Never try to teach a pig to sing...it wastes your time and it annoys the pig."

Comment on perl callable from bash?
Select or Download Code
Re: perl callable from bash?
by ig (Vicar) on Apr 13, 2009 at 01:20 UTC

    Maybe your scripts (firstscript and secondscript) have <CR><LF> line terminations instead of the usual (on unix) <LF> line termination. That might explain the first of your error messages. The second error message, beginning without an opening single quote and with the text ".links" is a bit more puzzling.

    One way to see what your line terminations are is to use the "od" command. For example, in the following, each line ends with "\r\n".

    $ od -c test.html 0000000 < h t m l > \r \n < h e a d > \r \ +n 0000020 < t i t l e > t e s t < / t i +t 0000040 l e > \r \n < / h e a d > \r \n < +b 0000060 o d y > \r \n T h i s i s s +o 0000100 m e " t e x t " i n t h +e 0000120 b o d y . \r \n < / b o d y > \ +r 0000140 \n < / h t m l > \r \n 0000152

    After converting the line endings, it appears as follows:

    $ od -c test.html 0000000 < h t m l > \n < h e a d > \n < +t 0000020 i t l e > t e s t < / t i t l +e 0000040 > \n < / h e a d > \n < b o d y +> 0000060 \n T h i s i s s o m e " +t 0000100 e x t " i n t h e b o d +y 0000120 . \n < / b o d y > \n < / h t m +l 0000140 > \n 0000142

    Now the lines end with "\n" only.

    Depending on your unix installation, you may have a command "dos2unix". I used the vim editor. You might also be able to use the "tr" command as "tr -d '\r' <file.old >file.new"

    How did you create your perl script files?

      Normally I'd use Emacs, but since the target is down I'm using EditPlus2 which can switch hit. Also using Cygwin to emulate the real environment. Don't think I mentioned it, but both scripts run fine from the command line. Just not in the shell script.

      --hsm

      "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re: perl callable from bash?
by irah (Pilgrim) on Apr 13, 2009 at 04:00 UTC

    how to make both firstscript and secondscript executable (chmod 777 and mv to /bin)

    If you are using Linux, you can create a symbolic link for that file, and put it in /bin directory.

    For execute a Perl script, give the exact path of Perl script in the bash script and check it.

Re: perl callable from bash?
by ikegami (Pope) on Apr 13, 2009 at 05:11 UTC
    Those error messages aren't generated by sh or by Perl, but by user code. Why don't you show us the code that generates those error messages.

    Wouldn't hurt if you added the following line to your sh script and gave us the output.

    echo "[$firname]"
Re: perl callable from bash?
by eye (Chaplain) on Apr 13, 2009 at 06:15 UTC
    Two points to be made here.

    First, you rarely, if ever, want to set a script to have permissions (mode) 777. That give write access to your script to anyone with access to the machine. A mode of 755 should be sufficient to allow anyone to run the script. If it is just for you, you could use 700.

    Second, I'm going to take a wild stab at your actual question and suggest that you:

    • change the script to include the absolute paths of the scripts (e.g., /home/hsmyers/bin/firstscript
    • enclose the name of your file in single quotes on the command line

    Good luck!

Re: perl callable from bash?
by FunkyMonk (Canon) on Apr 13, 2009 at 09:12 UTC
    If you've copied your file to /bin you should be able to run it by just typing bl filename. Using ./bl tells the shell to run the file called bl that's in the curent directory.

    The way I do it is to make my own bin in my home directory with mkdir $HOME/bin and add it to my path by adding export PATH=$HOME/bin:$PATH at the end of $HOME/.bashrc assuming you're using bash as your shell. You still need to make the executable using chmod as previously mentioned.

      In line with best/recommended practice, I provide session-wide definitions e.g. $PATH etc., in my .profile, the shell rc file (.bashrc, kshrc, etc.) is left to define session specifics...

      A user level that continues to overstate my experience :-))

        While .profile and $HOME/bin are quite appropriate for an individual developer, a sysadmin faces a different situation. If the script is intended to for use by all users of the system, the traditional location is /usr/local/bin or maybe even /usr/local/sbin. If the file is required for boot, then /bin or /sbin might be called for. The caveat with .profile is that it's only invoked by an interactive shell.

        When I have a script that I only want root to run, I'll usually toss the script in /root/bin unless the system uses / as root's home directory.

        I'm not really disagreeing, except with the idea that there is a single best practice for all situations.

Re: perl callable from bash?
by Bloodnok (Vicar) on Apr 13, 2009 at 10:21 UTC
    Apart from the advice already proffered elsewhere on this thread, I'm intrigued as to how you can ...mv to /bin and then expect the script to run by entering ./bl filename without first cd(1)'ing to /bin aka having just moved the script from your current working directory, you expect to still be able to invoke the script in there &91;your current working directory&93;

    Moreover, the 2 scripts, called by the shell script, are no longer in the same directory as the shell script - hence it should come as no surprise that the shell script can no longer find them, so assuming all 3 scripts were previously in your home directory, I would modify the shell script (now /bin/bl) to read...

    #!/bin/sh filename=$HOME/$1 firstscript $filename secondscript $filename

    A user level that continues to overstate my experience :-))
Re: perl callable from bash?
by targetsmart (Curate) on Apr 13, 2009 at 13:45 UTC
    Which I at first thought had to do with not getting the variable right--- but a little detective work convinced me that what it couldn't find were the two scripts.
    IMO you are wrong, if you don't have script in place, the shell script will say
    ./check.sh: line 3: firstscript: command not found ./check.sh: line 4: secondscript: command not found
    IMO you have problem with arguments only, and might be some problems with the absolute path of the first and second script(as other monks mentioned).
    generally if you are dealing with filenames in shell scripts, just check it existence before using it for further processing.
    you can use something like
    -e file True if file exists. -f file True if file exists and is a regular file. -r file True if file exists and is readable. -s file True if file exists and has a size greater than zero. -O file True if file exists and is owned by the effective user i +d. -G file True if file exists and is owned by the effective group +id.
    and more of this can be seen in your shell manual(the above operators are from bash manual), use those effectively to figure out whether the file exists and continue only in such case. For the script use
    -x file True if file exists and is executable.
    before calling it from your shell script.

    if I write your script (shown in OP), in bash, it will be like

    #!/bin/bash echo -e "received arguments >$*<" filename=$1; script1='fristscript'; # use absolute path script2='firstscript'; # use absolute path if [ -f $filename ] ; then echo -e "executing scripts $script1, $script2"; if [ -x $script1 ]; then echo -e "executing $script1..." $script1 $filename else echo -e "$script1 not found"; fi if [ -x $script2 ]; then echo -e "executing $script2..." $script2 $filename else echo -e "$script2 not found"; fi else echo -e "$filename is not readable or doesn't exist"; fi
    Hope this helps.
    (again the above bash code can be written even more effectively, but that is not the point of discussion)

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2014-09-01 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (299 votes), past polls