Reputation: 281
I get the below error while trying to execute a shell script,
$'\r': command not found: line 2:
Please suggest a solution for the same.
Below are the intial lines used in the script,
#!/bin/sh
if [[ $# -lt 1 ]]; then
echo "ERROR Environment argument missing <dev,test,qa,prod>"
export RC=50
exit $RC
fi
Upvotes: 28
Views: 75208
Reputation: 3
You can just replace '\r'
with ''
(nothing), this will do the trick.
If you have any GUI editor (Notepad++) recommended directly find/replace.
If you are only doing it on Linux server then use something like:
sed -i 's/old-text/new-text/g' input.txt
But Since '\r'
comes only if you have edited it in windows as it's a new line character (\r\n
), so first method should work.
Upvotes: 0
Reputation: 181
I used notepad++ to convert the line endings.
Edit > EOL Conversion > UNIX/OSX Format
Upvotes: 18
Reputation: 251
You can use sed -i 's/\r$//' scriptname.sh
Replace the scriptname with actual script name.
Upvotes: 25
Reputation: 168
I had the same error and what I did was to transform the characters '\r'
to '\n'
. using this line:
tr '\r' '\n' < oldfile.sh > newfile.sh
mv newfile.sh oldfile.sh
chmod +x oldfile.sh
./oldfile.sh
I think you could also delete the '\r' characters by using:
tr -d '\r' < oldfile.sh > newfile.sh
tr
is the command trasnform, and the -d
is delete the following character.
I think the shell actually doesn't like '\r'
character.
Upvotes: 3
Reputation: 104050
I got a different error message when running your script under /bin/sh
, but when I switched to /bin/bash
, it worked fine:
$ cat foo.sh
#!/bin/sh
if [[ $# -lt 1 ]];
then echo "ERROR Environment argument missing"
RC=50
exit $RC
fi
$ sh foo.sh
foo.sh: 6: [[: not found
$ bash foo.sh
ERROR Environment argument missing
You've built in a bashism. This may or may not be a big deal for your organization. If you want to keep using bash
-specific features, change the shebang line to #!/bin/bash
and see if that helps.
Upvotes: 1
Reputation: 360085
Your problem is that the file has Windows line endings. This can be caused by editing a file in Windows and trying to run it on a non-Windows system.
You can fix this problem using dos2unix
to convert the line endings:
dos2unix ConstruedTermsXMLGenerator.sh
The corresponding utility to convert in the other direction is unix2dos
.
Some systems have fromdos
and todos
.
Upvotes: 41
Reputation: 234
I had this exact issue when creating a .sh file on a Mac (unix) and executing it in Linux. Turns out that I had to set FileZilla FTP settings to 'Binary' transfer type:
Upvotes: 1