Find the answer to your Linux question:
Results 1 to 4 of 4
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2005
    Posts
    2

    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.

    Code:
    #!/bin/sh
    # 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
            done
    done

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    You did:
    Code:
    cat $filename
    while read line; do
    whereas it should be:
    Code:
    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
    Just Joined!
    Join Date
    Aug 2005
    Posts
    2
    thank you for the reply...works 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. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Tip: if your patterns contain '/' characters, consider using a different delimiter:
    Code:
    s|\"../css|\"/css|

Posting Permissions

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