0

I've been trying to run a script using chmod.

Here's how I tried to execute the script:

$ chmod u+x tester
$ ./tester
./tester: Command not found.

I always get the Command not found error and I'm struggling to figure out why. Tried the full path fix too and run chmod from there but still it won't solve the problem.

I used ls -l tester and here's my access permission:

-rwxr-xr-x 1 kplus user123 983 Jan 28 15:00 tester

Here's the complete version of the Linux that I am using:

Linux KODRL58IRA02 2.6.18-308.e15 #1 SMP Fri Jan 27 17:17:51 EST 2012 x86_64 

Here's the script that I am trying to run:

#!/bin/ksh

echo ""
echo " = K+WA =";
WA_PORT="`cat /home/kptp4/testdirectory2/kondor.active |grep PORTAL_PORT|cut -d '&' -f 2`"

#== Checking Tomcat of WebAccess
printf "%25s" Tomcat
touch /home/kptp4/processiddirectory/tomcat-*.pid
PID=`cat /home/kptp4/processiddirectory/tomcat-*.pid`
if  [[ $PID = "" ]];then
echo " x 0"
else
PSPID=`/bin/ps -eo pid | grep -w $PID`
if [[ $PSPID -eq $PID ]];then
/bin/ps -ef | grep $PID | grep -v grep | awk 'NR==1 {print " = "$8 $9 $10 $11 $12 $13 $14 $15}'
else
echo " x 0"
fi
fi

PORT_STATUS=`netstat -an | grep ${WA_PORT} | grep LISTEN | perl -pe "s/^.+\n/LISTENING/g;"`
PORT_STATUS=${PORT_STATUS:="NOT LISTENING!!!"}
echo " Port ${WA_PORT}/TCP = ${PORT_STATUS}"

cd /home/kptp4/WebAccessFolder
(PATH=/home/kptp4/testdirectory/checkall.sh;) | perl -pe "s,^, ,g;"
echo ""

I've also tried to run it using ksh, bash, sh but no luck...

$ bash tester
: Permission denied

 = K+WA =
: Permission denied
: Permission denied
tester: line 29: syntax error: unexpected end of file
$ sh tester
: Permission denied

 = K+WA =
: Permission denied
: Permission denied
tester: line 29: syntax error: unexpected end of file
$ ksh tester
tester[2]: ^M: cannot execute [Permission denied]

 = K+WA =
tester[4]: ^M: cannot execute [Permission denied]
tester[6]: ^M: cannot execute [Permission denied]
tester: line 10: syntax error at line 29: `if' unmatched

Right now I'm wondering why I cannot run this script as I am able to run other scripts using the permission commands I have used above(chmod u+x filename). Any idea how I can solve this? This script is working fine in my SunOS version of Linux by the way, and I am wondering why it doesn't execute properly in this system.

Any help would be much appreciated, thank you!

Thomas Dickey
  • 76,765

1 Answers1

1

It looks like your script file has DOS/Windows line endings, e.g. Carriage Return (^M, chr(13)) in addition to the Line Feed character. The output tester[2]: ^M: cannot execute [Permission denied] certainly looks like that. This would also explain why the file cannot be executed by the shell directly: It is looking for an executable named /bin/ksh^M extracted from the shebang line, which most probably does not exist.¹

You should first try to find out if this is the case, e.g. by opening the file in an editor that shows the line endings or using file tester or even od -ah tester. Many editors can remove the CRs, as can the utility dos2unix. Under Windows, the Notepad++ editor is capable to convert line endings using the menu entry Edit -> EOL Conversion -> UNIX/OSX Format.

See also What is ^M and how do I get rid of it?.


¹ SuSE Linux used to have a funny symlink from /usr/bin/perl^M to /usr/bin/perl to allow all those random Perl scripts retrieved from the Web to work even without converting them to Unix line endings.

Dubu
  • 3,723
  • Hi I used notepad++ to see if the file has DOS/Windows line endings, what showed up after every line in the my script is the CR LF.. does this confirm that my file has DOS/Windows line endings? thank you – Francis Feb 11 '16 at 06:02
  • @Francis Yes, CR LF (Carriage Return - Line Feed) are DOS/Windows line endings. Unix/Linux uses LF only. You should be able to convert the file in Notepad++ using Edit -> EOL Conversion -> UNIX/OSX Format. I added this to my answer. – Dubu Feb 11 '16 at 06:35