Tech_20
asked on
Shell script errors
After creating the following script (see attached text file). I get the following error. This is supposed to be a short, simple script which is why I have trouble finding the source of the error. I sometimes see shell scripts with a semicolon after the [condition] in the "if" statement but didn't use one here after trying it and getting the same error.
Please help. Thanks.
shellScript.txt
shellScript-error.txt
Please help. Thanks.
shellScript.txt
shellScript-error.txt
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well spotted, wmp - I converted the script to DOS format and did indeed get the syntax errors on those lines (once I'd added an extra line after the "#!").
The odd text in ": command not foundine 4:" is because it really says "scriptname.sh: line 3: $'\r': command not found" but the "\r" is being treated as a carriage return, so the second half of the error overwrites the start of the first.
The odd text in ": command not foundine 4:" is because it really says "scriptname.sh: line 3: $'\r': command not found" but the "\r" is being treated as a carriage return, so the second half of the error overwrites the start of the first.
ASKER
@woolmilkporc:
Good point. I attached the results of the cat -v command.
shellScript-error-cat-v.txt
Good point. I attached the results of the cat -v command.
shellScript-error-cat-v.txt
ASKER
@woolmilkporc:
Also, I'll plan to test this on a Linux machine since this error is from Terminal on an iMac (Yosemite OS X 10.10) which usually works well with bash scripting.
Also, I'll plan to test this on a Linux machine since this error is from Terminal on an iMac (Yosemite OS X 10.10) which usually works well with bash scripting.
Mac up to version 9 has CR at the line ends, while Unix has LF. Windows/DOS have CRLF, by the way.
OS/X should generally (obviously not always) have LF just like Unix, however!
Anyway, if you have the "dos2unix" tool installed (I guess you have) you can try
dos2unix -c mac filename.sh filename.unix.sh
OS/X should generally (obviously not always) have LF just like Unix, however!
Anyway, if you have the "dos2unix" tool installed (I guess you have) you can try
dos2unix -c mac filename.sh filename.unix.sh
If the above doesn't work please let me know, or see " man dos2unix" for more/other options of this tool.
Rewriting the whole thing under Unix/Linux is still an option ...
Rewriting the whole thing under Unix/Linux is still an option ...
from the cat -v command output file, it is clear that you have control-M char at the end of each line.
You can use the command dos2unix as suggested by woolmilkporc
or you can:
1- transfer file with ftp and choose text mode
or
2- if you know how to replace char using vi editor then you could get control-M replaced e.g.
:1,$ /^V^M//g
^V^M means you control-V followed with control-M
You can use the command dos2unix as suggested by woolmilkporc
or you can:
1- transfer file with ftp and choose text mode
or
2- if you know how to replace char using vi editor then you could get control-M replaced e.g.
:1,$ /^V^M//g
^V^M means you control-V followed with control-M
ASKER
Thanks everyone. I wrote the same script on a Linux machine (vi) and had no issues. I'll do a little more testing and finished the question.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As an aside, the first line normally starts "#!" with no space - that's what the interpreter normally looks for. If you run it as "bash script.sh" it doesn't matter, because it is just a comment in that case. It only comes into play if you make it executable and run "./script,sh" without the leading "sh ".
Edit: Forgot to mention the semicolon. In shell scripts semicolons are *separators", not *terminators*. You only need them to separate multiple statements on the same line, so you would write:
Open in new window
orOpen in new window