discussing some technical aspects covering tools, frameworks, technologies etc (the areas where i m involved and getting experienced) - feel free to discuss!

Monday, September 08, 2008

Editing the shell script in Windows OS

Are you one among them who happened to edit the shell script in Windows OS? You may have various reasons to do that.

You should have got the script working very well in the appropriate linux (or any other flavour) box. But just before doing the final operation (be it checking it into CVS (version control system) or sending to the client waiting at onsite) you might have thought of giving a final/finishing touch like adding some documentation etc.,

Are you asking, "wats up?" or "what's a big deal.. huh?". Wait a minute. All is fine until you execute the script back in the linux box again.

Boooom.. The linux OS spits out an error message saying that "/bin/bash^M: bad interpreter: no such file or directory". What had happened?
Wondering..? Surprised? Shocked rather??

Well, here it goes. The way the Windows OS deals with the file saving option is quite different than that of Linux OS. Once you are done with the so called final/finishing touch and when you save the file, some control characters are getting added to the shellscript (.sh file). That's what the culprit is and hence the error when executing it again in linux. The Bash Shell is unable to deal with such special character ('Control M' -- is referred as "^M").

It is assumed that Bash is the default and widely used Shell. The shell being used and the actual error message might vary as per your system's default shell and the way it is customized.

What and Where is the rescue?

There has to be a way. Isn't it? Yes, of course. There are two options.

(1) You can open the file in Linux OS (using your favorite editor like
vi, emacs etc.,) and can delete such characters what you find at the end. I am not very sure of this as to what extent those characters are explicitly visible to the end user.

(2) But the other excellent alternative is to use the 'dos2unix' command wherein the usage goes as follows.

'dos2unix ' [eg., dos2unix myShellScript.sh]

This do2unix command converts the file in the DOS (for Microsoft environment) to the Unix properitary format. While doing so, it removes the special/control characters and it ultimately cleans the file to be used in Linux versions :).

Note/Word of Caution: Never edit the Linux related files in non-linux OS (Windows)! :)

Hope this helps to solve the tactics and saves you from the critical moment!

No comments:

About Me

ஏதோ பிறந்தோம், ஏதோ வாழ்ந்தோம் என்றிருப்பதல்ல வாழ்க்கை! எப்படி வாழ்ந்தோம் என்பதும் ஒரு அங்கம். வாழக் கிடைத்த வாழ்க்கையில், ஒரு சிலருக்காவது வசந்தத்தின் முகவரியை அறிமுகம் செய்தோமேயானால் அதுவே வசீகரத்தின் வனப்பைக் கூட்டும்!