KSH shell script won't execute and returns 127 (not found)

Chris Knight picture Chris Knight · Apr 14, 2010 · Viewed 18.2k times · Source

Can anyone enlighten me why the following won't work?

$ groups
  staff btgroup
$ ls -l
  total 64
  -rw-rw----    1 sld248   btgroup       26840 Apr 02 13:39 padaddwip.jks
  -rwxrwx---    1 sld248   btgroup        1324 Apr 02 13:39 padaddwip.ksh
$ ./padaddwip.ksh
  ksh: ./padaddwip.ksh:  not found.
$ echo $?
  127

This is nearly identical to another script which works just fine. I can't see any differences between the two in terms of permissions or ownership.

Answer

DVK picture DVK · Apr 14, 2010

There may be 2 problems:

  • Shebang line is wrong (as ghostdog alluded to)

  • The script was saved from Windows and has DOS line endings.

For the latter, do

head padaddwip.ksh | cat -vet | head -1

The command should produce the shebang line NOT ending with ^M. If it does end with ^M that's a DOS-encoded file, and the fix is:

cp padaddwip.ksh padaddwip.ksh.bak
dos2unix padaddwip.ksh.bak > padaddwip.ksh
./padaddwip.ksh

On systems without dos2unix, you can use

cat padaddwip.ksh.bak | tr -d "\r" > padaddwip.ksh