Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    bash script for/while nested loops

    Hi everyone,

    I just joined the forums here hoping to get someone to take a quick peek at a simple shell script I just wrote. I'm wanting to go through all files in a directory, convert the file to unix line endings, then do some search/replace on the contents.

    It works fine except it doesn't seem to leave the inner while loop and contine to the next file (in the for loop).

    I've tried calling this a few ways on the cmd line to be sure it wasn't the way I am executing it. Examples: ./this_script file1 file2, ./this_script file*, ./this_script | ls, etc..

    Can anyone see anything obvious that would prevent the loops from acting as expected? Thanks in advance.

    # Loop over the command-line arguments
    for filename in "$@"; do
            perl -pi -e 'tr/\015/\012/;' $filename
            echo "Examining file $filename"
            wc -l $filename
            cat $filename
            while read line; do
                    perl -pe 's/\"..\/css/\"\/css/;' $filename > temp
                    mv -f temp $filename
                    perl -pe 's/..\/gallery.html/\/gallery\//;' $filename > temp
                    mv -f temp $filename
                    perl -pe 's/..\/images/\/images/;' $filename > temp
                    mv -f temp $filename
                    rm -f temp

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    St. Petersburg, FL
    You did:
    cat $filename
    while read line; do
    whereas it should be:
    cat $filename |
    while read line; do
    Also, why are you using perl? It would be faster to use the 'sed' and 'tr' programs instead of the perl commands...

  3. #3
    thank you for the like a charm! the perl is mostly habit. your point is well taken about sed and tr being faster. i'll do a little research and rewrite that bit.

  4. $spacer_open
  5. #4
    Tip: if your patterns contain '/' characters, consider using a different delimiter:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts