I have two Perl scripts at a predefined location in Unix. This predefined location is set in path variable ($PATH). Now when i try to run first script (from any location) the script runs successfully while for second script (at same path as that of previous script) throws an error saying

ScriptB: Command not found

My shell is tcsh and permissions given to both scripts are the same.

What can be the probable reason for such erroneous behaviour?

  • 2
    What does sed l\;q /path/to/ScriptB tell you? Is ScriptB executable (ls -ld /path/to/ScriptB)? – Stéphane Chazelas May 17 '13 at 14:23
  • Is ScriptB executable? Does it have a shebang at the top which points to the right place? – Flup May 17 '13 at 14:25
  • @StephaneChazelas - Script is at path : /path/to sed l\;q /path/to/ScriptB returns #!/path/to/bin/perl\r$ #!/path/to/bin/perl – Mayank Jain May 17 '13 at 14:26
  • @Flup - Answer to both questions YES... – Mayank Jain May 17 '13 at 14:31
  • @StephaneChazelas @cjm - Can you please explain what sed l\;q /path/to/ScriptB is trying to do – Mayank Jain May 17 '13 at 15:01

ScriptB has CRLF line endings. Since the kernel doesn't support CRLF, it's trying to execute a program named "/path/to/bin/perl\r" (where \r indicates a CR). That program doesn't exist. Convert ScriptB to LF line endings. (Thank Stephane Chazelas for asking the right question. The \r in the sed output indicates the problem.)

sed l\;q FILE runs 2 sed commands (separated by a semicolon, which needs to be escaped because it's also a shell metacharacter). From man sed:

l  List out the current line in a "visually unambiguous" form
q  Immediately quit the sed script without processing any more input,
   except that if auto-print is not disabled the current pattern space will be printed.

So l prints the first line in a format that makes normally hidden characters like CR visible, and then q quits after the first line.

  • But how two lines are returned by sed command in discussion. As per your explanation it should just return one line i.e. the first line of first – Mayank Jain May 17 '13 at 15:08
  • Also if the problem is with script why the error thrown is Command not found instead error should be thrown by perl – Mayank Jain May 17 '13 at 15:11
  • I abbreviated the explanation of q because it's only the first line of output that's significant. – cjm May 17 '13 at 15:11
  • The problem is that the kernel doesn't start perl because you told it to look for perl\r. Perl can handle scripts with CRLF line endings; it's the kernel that has an issue with them. – cjm May 17 '13 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.