0

I'm using bash for the first time. Wrote a code that is supposed to take "stats" as a command, but whenever I use "stats" in my command lines I kept getting the following error:

bash: stats: command not found

I googled around and a lot of people are saying this error is usually associated with PATH problems. Running "echo $PATH" yields the following results:

/bin:/sbin:/usr/local/bin:/usr/bin:/usr/local/apps/bin:/usr/bin/X11:/nfs/stak/students/z/myname/bin:.

I made sure my program started with

#!/bin/bash 

Is my PATH wrong? If so, how do I fix it? If not, any suggestions on what else I should look into? Thank you all for your time and help.

Aisha Ashwal
  • 83
  • 3
  • 11
  • 1
    Does the command `stats` actually exist? don't you mean `stat` (without `s`) instead? – gniourf_gniourf Jan 08 '17 at 07:51
  • The name of the script is called stats – Aisha Ashwal Jan 08 '17 at 07:58
  • Where is the script located? (Run `pwd`). Does it have executable permissions? (Run `ls -al` in the folder the script exists). – ffledgling Jan 08 '17 at 08:02
  • after running pwd the output says /nfs/stak/students/z/myname/344. after runnin ls -al it displays the script with "-rw-rw----" not sure what that means – Aisha Ashwal Jan 08 '17 at 08:13
  • 2
    Use `chmod +x ./stats` and run your script with `./stats`. – Cyrus Jan 08 '17 at 08:17
  • 4
    If it's your 'stats' script -- it means it is **not executable**. `chmod 0755 statsfilename` and then execute it with `./statsfilename` (in the current directory) or `/absolute/path/to/the/file`. – David C. Rankin Jan 08 '17 at 08:17
  • I tried both methods above and kept getting the ": No such file or directory" message, which is a bit confusing because when I type "ls" it shows me that a file named "stats" IS in the directory. – Aisha Ashwal Jan 08 '17 at 08:21
  • 3
    Does the script have Windows/DOS-style line endings? Try printing it with `cat -vet stats`, and see if the first line is "#!/bin/bash^M$" (DOS-style, with a carriage return, indicated by "^M", before the linefeed) or just "#!/bin/bash$" (no "^M" = no carriage return = unix-style line endings, which is what you want). – Gordon Davisson Jan 08 '17 at 08:50
  • Thank you! That was the problem! I then googled how to remove ^M and my program works just fine now. Can you make this into an answer so I can check it? Thank you SO much. – Aisha Ashwal Jan 08 '17 at 16:55

2 Answers2

1

It might be a PATH problem, it might be a permission problem. Some things to try.

1) If stats is not in your current directory, change directory (cd) to the directory where stats is and do

bash stats

If stats executes correctly, then you know at least that the script is OK. Otherwise, look at the script itself.

2) Try to execute the script with

./stats

If this gives

bash: ./stats: Permission denied

Then you have a permission problem. Do a

chmod a+rx stats

and retry. Note: a+rx is perhaps a bit wide; some may suggest

chmod 755 stats

is a better choice. Hint: from the comments, I see that this is one of your problems.

3) From the name of the directory, I get the impression that the file is on NFS. It might therefore be mounted as 'noexec', meaning that you cannot execute any files from that mount. You might try:

cp stats /tmp
chmod 700 /tmp/stats
/tmp/stats

4) Check the full path name for stats. If you are still in the same directory as stats, try

pwd

Check if this directory is present in the PATH. If not, add it.

export PATH=$PATH:/nfs/stak/students/z/myname/344

and try stats again.

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
0

Your PATH looks reasonable. PATH problems are a common source of errors like this, but not the only one.

The error message ": No such file or directory" suggests that the script file has DOS/Windows-style line endings (consisting of a carriage return followed by linefeed) instead of unix-style (just linefeed). Unix programs (including shells) tend to mistake the carriage return for part of the line, causing massive confusion.

In this instance, it sees the shebang line as "#!/bin/bash^M" (where "^M" indicates the carriage return), goes looking for an interpreter named "/bin/bash^M", can't find it, and prints something like "/bin/bash^M: No such file or directory". Since the carriage return makes the terminal return to the beginning of the line, ": No such file or directory" gets printed on top of the "/bin/bash" part, so it's all you see.

If you have the dos2unix program, you can use that to convert to unix-style line endings; if not, there are a variety of alternate conversion tools. But you should also figure out why the file has Windows/DOS format: did you edit it with a Windows editor, or something like that? Whatever caused it, you should make sure it doesn't happen again, because Windows/DOS format files will cause problems with most unix programs.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151