LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-18-2007, 01:00 PM   #1
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Rep: Reputation: 45
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??
 
Old 12-18-2007, 08:18 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
It seems to stick in random characters and letters in my script,
how does it look like
 
Old 12-19-2007, 10:05 PM   #3
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
Here's my code:

Code:
#!/bin/bash
#
# backup script 
D=`/usr/bin/date -d "-1 day" +"%Y-%m-%d"`
DOM=`/usr/bin/date +%a`
/usr/bin/tar -N $D -czvf /cygdrive/g/uhall/projects-$DOM.tar.gz /cygdrive/d/projects/
exit
Here's how I am executing the shell script in DOS
Code:
c:\cygwin\bin\bash --login "c:\backup-script.sh"
Unfortunately I can't copy/paste the error, but it basically starts changing my paths by adding extra "\" and "r" in there. This script works 100% fine in Linux, but not Cygwin (paths are changed appropriately, of course). Keep in mind that this works fine when I manually type it in Cygwin without the variables, but when I launch it from a batch script in DOS then it gets all messed up.

Last edited by Micro420; 12-19-2007 at 10:08 PM.
 
Old 12-20-2007, 05:53 AM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
How are you editing your scripts? \r looks suspiciously like Windows carriage return.
 
Old 12-20-2007, 07:28 AM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 12-20-2007, 10:45 AM   #6
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by allend View Post
How are you editing your scripts? \r looks suspiciously like Windows carriage return.
I am using either notepad.exe or the simple 'edit' command in DOS. I don't see any weird characters in my script.

Quote:
Originally Posted by Hivemind View Post
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.
What the heck is a binary mount? What are unix style endings?
 
Old 12-20-2007, 05:08 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
Old 12-20-2007, 06:24 PM   #8
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
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?
 
Old 12-20-2007, 06:45 PM   #9
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
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
 
Old 12-20-2007, 07:01 PM   #10
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
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
 
Old 12-20-2007, 07:10 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Frankly, if possible, only edit on the machine type your code will run on, although you can use dos2unix, vim etc to 'fix' stuff.
 
Old 01-04-2008, 10:56 PM   #12
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
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.
 
Old 01-05-2008, 07:33 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Correctamundo
 
Old 01-07-2008, 11:29 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
cat -vets is a good way to see these characters,

dos2unix and unix2dos corrects them
 
Old 10-17-2009, 11:25 AM   #15
Shock2k
LQ Newbie
 
Registered: Oct 2009
Posts: 1

Rep: Reputation: 1
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Cygwin shell script? corbintechboy General 1 12-26-2007 10:59 AM
Cygwin shell programming question Rostfrei Linux - General 6 06-30-2006 03:36 PM
Possible ways to run shell scripts on Windows Uday123 Linux - General 1 04-05-2006 12:48 AM
writing Linux shell scripts in Windows NightWolf_NZ Linux - Newbie 3 09-10-2003 09:28 PM
Cygwin:How do you turn the bash shell bell off? petercool Linux - Newbie 8 05-23-2003 10:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration