On 3/21/00 18:51, Steve Youngs at [EMAIL PROTECTED] wrote:

> 
>Hey People
>
>I'm having a problem with a bash shell script that I'm hoping someone
>here can help me with.
>
>The script is supposed to convert ^M to ^J in all the files in a
>directory.  The script is invoked with 'scriptname *'.  The thing that
>I just can't nut out is how to increment the variable that represents
>the filename.
>
>Here's what I have..
>
>,----[ Le Script ]
>| #! /bin/bash
>| 
>| LOOP_TIMES=$#
>| LOOP=1
>| FILE=$1
>| 
>| while [ $LOOP -le $LOOP_TIMES ]; do
>|     cat $FILE | tr "\r" "\n" > $FILE
>|     somehow increment $FILE from $1 to $2, $3, $4 etc up to $#
>|     LOOP=$[ LOOP + 1 ]
>| done
>`----
>
>I know the answer will be something really simple and easy, I just
>can't think of it. :-(
>

Why not do something like:

for file in `ls thedirectorythefilesarein`
     do
     `sed -e s/\^M/\^J/ < $file > tmpfile` ;
     mv $tmpfile $file
     done

or instead of using ls, use find:

for file in `find /thedirectorythefilesarein -name ./* -print`
     do
     etc...

These methods would list each of the files in the directory and run them 
through sed, changing your ^M's to ^J's.  If the directory the files are 
in has subdirectories, you would probably want to add in a test to make 
sure the file is in fact a normal file and not a directory or some other 
type of file.  Something like: if [ -f $file ] then...

A word of caution.  You will have to experiment with this a little bit to 
get it to work.  I am pretty much a beginner with shell scriptign myself, 
and so I can not warrant this will work for you first time out.  But, it 
should get you started at least.  I strongly recommend reading the man 
pages for find, ls and test (there are at least two of these; one is a 
bash built-in, the other a utility).

>-- 
>|---<Regards, Steve Youngs>-------------------------------------|
>|     It's a funny thing about life; if you refuse to accept    |
>|          anything but the best, you very often get it         |
>|------------------------------------<[EMAIL PROTECTED]>---|
>
HTH,

Sean


                  Theo. Sean Schulze

[EMAIL PROTECTED]        [EMAIL PROTECTED]
**************************************************
     Save the whales.  Collect the whole set.
     



-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [EMAIL PROTECTED]
Please read the FAQ at http://www.linux-learn.org/faqs

Reply via email to