Bash Shell Script Error. “bad interpreter: No such file or directory error”
Today I created a simple shell script and I was getting a few odd errors:
cody@taylor:/var/some_folder/server$ ./process_xml.sh
-bash: ./process_xml.sh: /bin/sh^M: bad interpreter: No such file or directory
I figured it was probably a permissions error or an issue with the shebang (#!/bin/sh) line. I tried removing the shebang line, changing it to use dash or bash explicitly, chmoding to 777 and still no luck and another odd error.
cody@taylor:/var/some_folder/server$ sh process_xml.sh
: not found.sh: 4:
I then checked the log file that the commands were supposed to be writing to and it was filled with ‘^M’ on every line break and the log name itself was followed by a ‘?’. Took a minute or two but I finally clued in that I wrote that script on a windows machine and then exported it to an ubuntu linux server via subversion. It was just a basic text format issue.
Under DOS (Windows/PC) the end of a line of text is signalled using the ASCII code sequence CarriageReturn,LineFeed. Alternately written as CR,LF or the bytes 0×0D,0×0A. On the Macintosh platform, only the CR character is used. Under UNIX, the opposite is true and only the LF character is used.
After a quick :
cody@taylor:/var/some_folder/server$ apt-get install tofrodos
cody@taylor:/var/some_folder/server$ dos2unix process_xml.sh
Everything worked fine.
September 20th, 2009 at
[...] Bash Shell Script Error. "bad interpreter: No such file or directory error" | tech stuff F… codytaylor.org/2009/09/bash-shell-script-error-bad-interpreter-no-such-file-or-directory-error.html – view page – cached Description of a Bash Shell Script Error: bad interpreter: No such file or directory error. My problem and solution. — From the page [...]
September 20th, 2009 at
You can also use these solutions
1) sed -e “s/^V^M//” >
2) perl -pi -e “s:^V^M::g”
3) cat | tr -d “^V^M” >
4) using (vi) :%s/^V^M//
September 20th, 2009 at
[...] the rest here: Bash Shell Script Error. "bad interpreter: No such file or … By admin | category: directory script | tags: bad-interpreter, such-file | No Related [...]
September 20th, 2009 at
I think the given error was quite clear.
No extra tool like dos2unix required to remove the windows line breaks. Just do it with vi:
vi process_xml.sh
:1,$s/^M//
:x
Where ^M is [Ctrl]+V then [Ctrl]+M
September 20th, 2009 at
Thanks for the tip.