Alex Rivera | Logout

Command not found error message when running script

Asked 2010-10-05T19:26:16.843
15

I'm learning how to make shell scripts in UNIX, but I keep coming across this stupid error. Let's say I make a script like this:

#!/bin/sh
echo HELLO

I save the file as test, and make the command executable by me with chmod 700 test. I save the file in my home directory, and (attempt) to run the file like so:

./test

Only for UNIX to reply back:

./test: Command not found.

What is going on? When I type out ls -l, there is an asterisk next to the file name. That wasn't there before I used the chmod command. Can anyone tell me what I'm doing wrong?

Edit
Report

1 Answer

12

This is very strange indeed. The steps you describe should have worked, so there must be some small error in your environment or the execution of those steps. There are some things you can do to help diagnose this:

CHECKING FOR CONTROL CHARACTERS

What you've documented looks fine, as long as there are no typos or control characters. You should check by typing:

cat -vt ./test

If you see any unexpected extra text that could explain the problem. For example, "^M" at the end of a line would indicate that your editor saved the file in Windows format.

REGENERATING THE FILE RELIABLY

To create a known-good ./test2, copy-paste the commands below:

`which bash`
printf "#\!`which sh`\necho HELLO\n" > ./test2
chmod +x ./test2
./test2
exit

CHECKING WHICH COMMAND CAN NOT BE FOUND

If you type...

./ajio

...do you get exactly...

./ajio: Command not found.  

...as you described with ./test? I just made up the name ajio so it shouldn't exist. If they messages match then it doesn't really tell you anything new. But if the messages differ, that confirms that ./test was at least found and executable.

It's also possible that your version of sh is trying to tell you not that test can't be found, but that while running test some command the shell tried to run couldn't be found. That shouldn't be the echo command, as with most shell implementations that will be an internal command, implemented inside the shell. But, the shell may run an initialisation script containing a line specifying a command it can't run. If you run man sh, it will tell you about all the different startup files that your shell might try to run. These can be different from those used when you start the shell interactively. But, as a b

answered 2010-10-06T01:15:52.800

Your Answer