public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Odd behavior of scripts in dos mode
@ 2012-04-26 14:14 Andrew DeFaria
  2012-04-26 14:26 ` Earnie Boyd
  2012-04-26 14:41 ` Eric Blake
  0 siblings, 2 replies; 6+ messages in thread
From: Andrew DeFaria @ 2012-04-26 14:14 UTC (permalink / raw)
  To: cygwin

I remember that Cygwin used to not be able to run scripts that were 
converted or were in "DOS" mode - had trailing carriage returns in the 
file. It would fail because the #! line might have /bin/bash\r which was 
not a file (bash with a carriage return that is). But the behavior has 
changed. Now it seems to ignore the trailing carriage return and it 
execs bash itself. But it fails later on with other carriage returns.

For example, consider the following:

#!/bin/bash
if [ 1 = 1 ]; then
   echo "test"
fi

Then use unix2dos to make the file have trailing carriage returns and 
execute the script:

Neptune:test.sh
./test.sh: line 2: $'\r': command not found
./test.sh: line 6: syntax error: unexpected end of file

Cygwin used to say something like "Unable to exec /bin/bash" but now it 
execs /bin/bash and bash fails to execute the if statement. So it seems 
to me that Cygwin is trying to be nice and solve the problem of people 
who get their scripts edited in some tools that writes out DOS mode 
files. My question is if it strips the trailing carriage return from the 
#! line why doesn't it do it for all lines?

(And yes I know I can dos2unix the script and it will work - the problem 
is it coming from some SCM system checkout (perforce) and that tool is 
adding trailing carriage returns to the file when it checks it out).
-- 
Andrew DeFaria <http://defaria.com>
Why is it that the guy who comes up behind you while you're waiting for 
an elevator presses the already lit button as though he has some magical 
powers that you don't?


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Odd behavior of scripts in dos mode
  2012-04-26 14:14 Odd behavior of scripts in dos mode Andrew DeFaria
@ 2012-04-26 14:26 ` Earnie Boyd
  2012-04-26 14:33   ` Andrew DeFaria
  2012-04-26 16:07   ` Buchbinder, Barry (NIH/NIAID) [E]
  2012-04-26 14:41 ` Eric Blake
  1 sibling, 2 replies; 6+ messages in thread
From: Earnie Boyd @ 2012-04-26 14:26 UTC (permalink / raw)
  To: cygwin

On Thu, Apr 26, 2012 at 10:13 AM, Andrew DeFaria wrote:
> I remember that Cygwin used to not be able to run scripts that were
> converted or were in "DOS" mode - had trailing carriage returns in the file.
> It would fail because the #! line might have /bin/bash\r which was not a
> file (bash with a carriage return that is). But the behavior has changed.
> Now it seems to ignore the trailing carriage return and it execs bash
> itself. But it fails later on with other carriage returns.

http://cygwin.com/faq/#faq.api.cr-lf

-- 
Earnie
-- https://sites.google.com/site/earnieboyd

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Odd behavior of scripts in dos mode
  2012-04-26 14:26 ` Earnie Boyd
@ 2012-04-26 14:33   ` Andrew DeFaria
  2012-04-26 16:07   ` Buchbinder, Barry (NIH/NIAID) [E]
  1 sibling, 0 replies; 6+ messages in thread
From: Andrew DeFaria @ 2012-04-26 14:33 UTC (permalink / raw)
  To: cygwin

On 04/26/2012 07:26 AM, Earnie Boyd wrote:
> On Thu, Apr 26, 2012 at 10:13 AM, Andrew DeFaria wrote:
>> I remember that Cygwin used to not be able to run scripts that were
>> converted or were in "DOS" mode - had trailing carriage returns in the file.
>> It would fail because the #! line might have /bin/bash\r which was not a
>> file (bash with a carriage return that is). But the behavior has changed.
>> Now it seems to ignore the trailing carriage return and it execs bash
>> itself. But it fails later on with other carriage returns.
> http://cygwin.com/faq/#faq.api.cr-lf
That's an interesting read but it really does nothing to explain the 
behavior I've observed. If it did then it would not be able to invoke 
bash\r.
-- 
<a href="http://defaria.com">Andrew DeFaria</a><br>
<small><font color="#999999">24 hours in a day...24 beers in a 
case...coincidence?</font></small>


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Odd behavior of scripts in dos mode
  2012-04-26 14:14 Odd behavior of scripts in dos mode Andrew DeFaria
  2012-04-26 14:26 ` Earnie Boyd
@ 2012-04-26 14:41 ` Eric Blake
  2012-04-26 17:22   ` Andrew DeFaria
  1 sibling, 1 reply; 6+ messages in thread
From: Eric Blake @ 2012-04-26 14:41 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 2335 bytes --]

On 04/26/2012 08:13 AM, Andrew DeFaria wrote:
> I remember that Cygwin used to not be able to run scripts that were
> converted or were in "DOS" mode - had trailing carriage returns in the
> file. It would fail because the #! line might have /bin/bash\r which was
> not a file (bash with a carriage return that is). But the behavior has
> changed. Now it seems to ignore the trailing carriage return and it
> execs bash itself. But it fails later on with other carriage returns.

If you want bash to ignore carriage returns, then ask it to do so:
http://sourceware.org/ml/cygwin-announce/2011-02/msg00027.html

> 4. This version of bash has a cygwin-specific set option, named "igncr",
> to force bash to ignore \r, independently of cygwin's mount style.  As
> of bash-3.2.3-5, it controls regular scripts, command substitution, and
> sourced files.  I hope to convince the upstream bash maintainer to
> accept this patch into a future bash release even on Linux, rather than
> keeping it a cygwin-specific patch, but only time will tell.  There are
> several ways to activate this option:
> 4a. For a single affected script, add this line just after the she-bang:
>  (set -o igncr) 2>/dev/null && set -o igncr; # comment is needed
> 4b. For a single script, invoke bash explicitly with the option, as in
> 'bash -o igncr ./myscript' rather than the simpler './myscript'.
> 4c. To affect all scripts, export the environment variable BASH_ENV,
> pointing to a file that sets the shell option as desired.  Bash will
> source this file on startup for every script.
> 4d. Added in the bash-3.2-2 release: export the environment variable
> SHELLOPTS with igncr included in it.  It is read-only from within bash,
> but you can set it before invoking bash; once in bash, it auto-tracks
> the current state of 'set -o igncr'.  If exported, then all bash child
> processes inherit the same option settings; with the exception added in
> 3.2.9-11 that certain interactive options are not inherited in
> non-interactive use.
> 4e. bash-4.1.9-1 dropped support for 'shopt -s igncr'; it did not make
> sense to support the option through both set and shopt, and SHELLOPTS
> proved to be more powerful.

-- 
Eric Blake   eblake@redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 620 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: Odd behavior of scripts in dos mode
  2012-04-26 14:26 ` Earnie Boyd
  2012-04-26 14:33   ` Andrew DeFaria
@ 2012-04-26 16:07   ` Buchbinder, Barry (NIH/NIAID) [E]
  1 sibling, 0 replies; 6+ messages in thread
From: Buchbinder, Barry (NIH/NIAID) [E] @ 2012-04-26 16:07 UTC (permalink / raw)
  To: cygwin

Earnie Boyd sent the following at Thursday, April 26, 2012 10:27 AM
>
>http://cygwin.com/faq/#faq.api.cr-lf

You can also avoid to change the source code at all
                   ^^^^^^^^^
                   changing

- Barry
  Disclaimer: Statements made herein are not made on behalf of NIAID.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Odd behavior of scripts in dos mode
  2012-04-26 14:41 ` Eric Blake
@ 2012-04-26 17:22   ` Andrew DeFaria
  0 siblings, 0 replies; 6+ messages in thread
From: Andrew DeFaria @ 2012-04-26 17:22 UTC (permalink / raw)
  To: cygwin

On 4/26/2012 7:40 AM, Eric Blake wrote:
> On 04/26/2012 08:13 AM, Andrew DeFaria wrote:
>> I remember that Cygwin used to not be able to run scripts that were
>> converted or were in "DOS" mode - had trailing carriage returns in the
>> file. It would fail because the #! line might have /bin/bash\r which was
>> not a file (bash with a carriage return that is). But the behavior has
>> changed. Now it seems to ignore the trailing carriage return and it
>> execs bash itself. But it fails later on with other carriage returns.
> If you want bash to ignore carriage returns, then ask it to do so:
> http://sourceware.org/ml/cygwin-announce/2011-02/msg00027.html
Interesting and it kinda explains that this is relatively new behavior 
and why it seems to interpret the #! line correctly.
-- 
Andrew DeFaria <http://defaria.com>
What hair color do they put on the driver's licenses of bald men?


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-04-26 17:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-26 14:14 Odd behavior of scripts in dos mode Andrew DeFaria
2012-04-26 14:26 ` Earnie Boyd
2012-04-26 14:33   ` Andrew DeFaria
2012-04-26 16:07   ` Buchbinder, Barry (NIH/NIAID) [E]
2012-04-26 14:41 ` Eric Blake
2012-04-26 17:22   ` Andrew DeFaria

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).