-1

I am trying to run a perl script from within a *.csh script, but I get the following error:

Can't open perl script "checkLength.perl": No such file or directory

I think it has something to do with the path of the perl script. I can run a perl script fine when it's not being called within the shell script, but a perl script won't run when I have it being called in the cshell script.

The shebang I use for the perl script is : #!/usr/bin/perl. I checked, and perl is located there (not in /usr/bin/env perl). I made sure it's executable.

Do I need to edit the $PATH to get this to work? I'm confused because if I run a perl script without calling it within another csh script, it runs just fine.

ruakh
  • 175,680
  • 26
  • 273
  • 307
geeb.24
  • 527
  • 2
  • 7
  • 25
  • Check your current working directory in the moment you call your script. – nlu Jan 25 '15 at 20:59
  • what is the path of `checkLength.perl` in relation to your .csh script? – Fredrik Pihl Jan 25 '15 at 21:01
  • Hmm, I'm not sure. How would I find that out? – geeb.24 Jan 25 '15 at 21:02
  • Ahh, I checked the working directory the moment before I called the script, and it's searching for the perl script in one directory ahead of where the perl script actually is. If i edit the path of the perl script to the actual path, that should fix the issue, correct? – geeb.24 Jan 25 '15 at 21:11
  • Yes, that's it, suppose you found out about pwd. And Perlscripts usually have the suffix .pl. – nlu Jan 25 '15 at 21:16
  • Yeah I knew about pwd from before, just never thought to use it right before the perl script ran just to check to see if I'm in the right directory. Thanks a lot mate! – geeb.24 Jan 25 '15 at 21:21
  • @nlu: Please write a solution so that the OP can accept it – Borodin Jan 26 '15 at 01:49

2 Answers2

1

Of course one obvious solution to your problem would be to simply use the full path to the script. But this is often not preferable, as the script would brake on installations that do not mirror exactly the original structure.

So there are two possibilities: Either you put your perl script into a well defined location that is contained in your PATH variable so any program that runs under the respective users environment finds it.

Or you have some well defined location relative to the location of your calling script. Say your bash script is located in somedir and you perl script is in somedir/subdir, then your call would be subdir/perlscript.pl. Then, to become independent of possible changes in the working directory of the caller, you could determine the current full path of the perl script to be called on start of the bash script.

A template for this would be:

#!/bin/bash
FULLPATH=$(pwd)/subdir/somescript.pl

# do something else, cd ...

$FULLPATH
nlu
  • 1,903
  • 14
  • 18
0

I am assuming your Perl script is kept in a fixed path relative to your bash script. Assuming your directory structure to be like so:

  • (BASEDIR)/bin/example.sh

  • (BASEDIR)/perl/example.pl

To allow you to run your bash script from anywhere in your system you must specify the relative path to your Perl script by getting the BASEDIR.

#!/bin/bash

BASEDIR=$(dirname $0)
perl $BASEDIR/../perl/example.pl

What we are doing above is finding the location of your bash script(BASEDIR/bin) that you call and then finding the relative location of the Perl script using the location of the bash script as reference. Now you will be able to call the bash script from anywhere and run your Perl script normally.

Shantanu Bhadoria
  • 14,202
  • 1
  • 12
  • 9