Shell scripts in Windows Cygwin
Just wondering if anyone else writes shell/BASH scripts using Cygwin in Windows? For some reason, my backup script, which works fine in Linux, does not work in Cygwin under Windows. It seems to stick in random characters and letters in my script, which causes it to fail. Anyone else notice any irregularities with Cygwin in Windows??
|
Quote:
|
Here's my code:
Code:
#!/bin/bash Code:
c:\cygwin\bin\bash --login "c:\backup-script.sh" |
How are you editing your scripts? \r looks suspiciously like Windows carriage return.
|
If you are using binary mounts on cygwin (which is the default and also recommended), your script must use unix style line endings. Run the command d2u on your scripts.
|
Quote:
Quote:
|
See here: http://en.wikipedia.org/wiki/Newline
Basically, dos/windows uses 2 (invisible) chars to denote a line ending (cr & lf), Unix/Linux uses only 1 (lf). These are represented in explicit coding as \r\n (MS), \n (Linux). See also: http://www.asciitable.com/ HTH |
Thanks for the info! So if these 2 characters are invisible, how would I know they are there other than seeing the output error? I am seeing the \r in my path, and that does explain why it is magically showing up. Are there other editors in Windows I can use that won't put these invisible characters?
|
You can see more information about your problem here:
http://cygwin.com/cygwin-ug-net/usin....html#id319037 You can get information on editors here: http://x.cygwin.com/docs/faq/cygwin-...html#q-editors |
Thank you, all! It's frustrating to see that there are those small nuisances that can make or break a script. :( I'm also glad that I'm not the only one that is going crazy over this and that it is documented
|
Frankly, if possible, only edit on the machine type your code will run on, although you can use dos2unix, vim etc to 'fix' stuff.
|
Sorry for the long delays but I was out on a 2-week vacation!
I fixed the problem by using and installing the Cygwin nano editor. I had to go to the end of each line and press the DEL key to get rid of the hidden "\r". My scripts work fine now. So my future advice to others using Cygwin is to NOT use the Windows Notepad or Wordpad editor to write your scripts. |
Correctamundo :)
|
cat -vets is a good way to see these characters,
dos2unix and unix2dos corrects them |
Might Help
I'm a Windows guy working in a Linux world. So I wrote my .sh script in a 'Notepad.exe' before FTP'ing it up to my linux servers.
I spent about 8 hours on this problem, so sad. I opened the file in gedit (centos), and just went to the begining of each line and hit 'delete' twice (if you have to hit it twice you know you have the problem) the rentered the "return". This cleared the problem. Cheers! Chris. |
All times are GMT -5. The time now is 11:37 PM. |