Subject: Re: Unable to run even simple batch scripts any more
From: "Ehud Karni" <[email-masked]>
To: [email-masked]
Cc: [email-masked]
Date: Wed, 14 Feb 2007 21:45:10 +0200
List-Id: <cygwin.cygwin.com>

On Tue, 13 Feb 2007 00:56:24 Eric Backus wrote:
>
> Obviously, the horse is still alive and attempting to limp along.  I predict
> it'll stay alive for quite some time longer.
>
 [snip]
>
> * Workaround inserting "set -o igncr" is impractical when there are lots of
> scripts.
 [snip]

Among my many scripts, there are some with DOS (i.e. CR LF) ending
that I prefer to leave as is (mostly to be able to edit them with
notpad).

I wrote a little script that can fix bash scripts with CRs by adding
the required "set -o igncr" line.

Notes:
 1. You can, of course, replace the `awk' with a simple `d2u' if
    you don't need the CRs at all.
 2. Change the `grep' arguments (mostly the "-r" and the files list)
    to suit your needs.
 3. This script will fail on a file with ":" in its name (which can
    occur only on manged mounts, because of the OS limitations)

Ehud.

 -------------------- cut here --------------------
#! /bin/bash -ex

chk_it ()
{
   while read HEAD
   do
       echo "        Checking $HEAD"                   # show some progress
       case "$HEAD" in
           *":1:#"*"^M" )                              # ONLY (ba)sh files with CR ending
                   SNM=`echo "$HEAD" | cut "-d:" -f1`  # extract script name
                   echo "Fixing $SNM ++++++++++++++++++++"
                   cp -p "$SNM" "$SNM"-new             # copy with permissions
                   awk '
BEGIN          { IA = 0 }

$0 ~ /^[ ]*#/  { print $0 ; next }

               {
                   if ( IA == 0 )
                   {
                       print "set -o igncr #\r" ;
                       IA = 1 ;
                   }

                   print $0 ;
               }
       ' < "$SNM" > "$SNM"-new                         # add "set -o igncr" after initial comments

                   mv "$SNM" "$SNM"-old                # save old script
                   mv "$SNM"-new "$SNM"                # rename new to current
       esac
   done
}
# ----------------------------------------------------------------------

grep -E -H -I -m 1 -n -U -r -e '^#![ ]*/bin/(ba)?sh' *     \
       | cat -v | chk_it                               # check & replace CR scripts

##################### end of fix-cr.sh script ####################

--
 Ehud Karni           Tel: +972-3-7966-561  /"\
 Mivtach - Simon      Fax: +972-3-7966-667  \ /  ASCII Ribbon Campaign
 Insurance agencies   (USA) voice mail and   X   Against   HTML   Mail
 http://www.mvs.co.il  FAX:  1-815-5509341  / \
 GnuPG: 98EA398D <http://www.keyserver.net/>    Better Safe Than Sorry

--
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/