You may facing the problem like this. The workable shell script in Linux, after you copy and paste into windows editor and save as .sh, it doesn’t work under Cygwin shell. Why?
Bear in mind that, Cygwin shell giving you a *nix environment (either Unix or Linux), which is actually a bash environment. Everything task carry on outside the bash environment will be under the windows environment.
Line terminator between windows and Linux environment are different. When you hit enter, in windows it actually inject code CR and LF into the end of the line; where in *nix only CR is inject.
Therefore if your script was written by using windows text editor (even w32 gvim), it will gives you line terminator with CR and LF.
How to verify?
Check your written shell script with cat -v, if your script display something ^M at the end of the line, that indicates the text file is created under windows environment. And CR LF hurts the execution of bash script, ouch!
cat -v myscript.sh
How to fix it?
There is a command for you to convert windows text format into unix text format.
To convert windows text format into unix text format, do this:
Now you are ready to run the script, but remember to chmod to the script to make it executable before executing it.
chmod +x myscript.sh