Wednesday, March 4, 2009

Shell script error: bad interpreter: No such file or directory


Shell script error: bad interpreter: No such file or directory
Some times when you take a file from a DOS/Windows system and move it to a Linux or Unix system you'll have problems with the dreaded ^M character. This happened recently when I moved a shell script from a Windows system to my Unix system. When I tried to run the shell script under the Unix I got this error message:

: bad interpreter: No such file or directory
I couldn't figure out what the message meant at first, but finally it hit me: the dreaded ^M problem. Sure enough, I opened the file in the vim editor, like this:

vi -b myfile.sh
Opening the file in vi using the binary switch (-b) I saw the extra ^M at the end of each line. I then issued one of my favorite vi commands to remove all these ^M characters, and the script magically began working. Here's the magic vi command:

:g/^M/s///
Note that you don't really type a ^ character and then a M character to generate that command. You actually type a [Control][v] and then a [Control][m] to create the necessary character. I don't know the genesis of this, but if you'll type those two keystrokes I think you'll see that it works as described.    

============================================================================== Please access the attached hyperlink for an important electronic communications disclaimer:   http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html ============================================================================== 

2 comments: