bash CRLF problems (I have read the recent announcement)

Eric Blake ebb9@byu.net
Tue Dec 5 04:30:00 GMT 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Kevin Layer on 12/4/2006 3:59 PM:
> I'm really perplexed by the following behavior:
> 
> rfr@mad64 ~
> $ cat -v foo1.sh
> echo 8010 > foo1.out
> 
> rfr@mad64 ~
> $ cat -v foo2.sh
> sh foo1.sh
> version=`cat foo1.out`

The result of `` comes from a pipe instead of a disk file, and only disk
files are affected by text mode mounts, so command substitution can only
strip \r if igncr is set.  cat reads its inputs in binary (at one point,
cat used to read in text mode by default, unless you passed --binary, but
the upstream maintainer removed that on the ground that POSIX requires
binary behavior from cat).  I could consider adding cat --text to cygwin's
cat, but it would be cygwin-only, so you would not be very portable in
using it.  However, instead of spawning two processes to assign your
variable (a subshell for ``, then cat), you could try this instead, with
several benefits - bash opens the file in text mode, the variable
assignment is faster due to fewer processes, and the line does not rely on
any cygwin-specific extensions:

read version < foo1.out

Another thing you could do is avoid putting line endings in foo1.out in
the first place - either use 'echo -n 8081 > foo1.out', 'echo -e 8081\\c >
foo1.out' (both of which rely on unportable usage of echo), or 'printf
8081 > foo1.out' (portable to POSIX sh, but not all platforms conform to
POSIX yet).  A patch along this line was just submitted to autoconf, in
its use of files containing results of runtime programs.

> $ mount
> C:\cygwin\bin on /usr/bin type system (textmode)

Asking for problems.  Usually, it is safer if you limit textmode mounts to
the directories that contain your files, rather than globally making the
basic cygwin files text mode.

> 
> The bug, IMO, is the assigment to version of `cat foo1.out` contains a
> ^M.  I'm on a text mount, so this is counter to what I thought would
> happen.

Again, based on the POSIX wording, cat must operate on binary files, so it
opens in binary mode by default, even on text mounts.  Which means the
command substitution sees the \r.  You can also play with CYGWIN=nobinmode
to turn pipes into text mode, so that the command substitution never sees
the \r, but that has other odd effects, such as breaking 'tar xjf
foo.tar.bz2'.

> 
> I experimented with `igncr' and couldn't get it to work.

Examples of what you tried?

- --
Life is short - so eat dessert first!

Eric Blake             ebb9@byu.net
volunteer cygwin bash maintainer
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFdPX+84KuGfSFAYARApzLAKCh46ov7l9hLZsPnrpw13K1T2JWBgCeOqFb
EkIlGw93EAndVHNyzKA+Zaw=
=fyM1
-----END PGP SIGNATURE-----

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



More information about the Cygwin mailing list