Results 1 to 7 of 7

Thread: Bash Script: unexpected end of file error

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Question Bash Script: unexpected end of file error

    I'm trying to make my first ever shell script. I almost copied and pasted this one from the web (actually I did to start with and that didn't work either).

    This is what I have currently:

    Code:
    for pattern in "this is string one" "this is string two" ; do
    for file in `find /root/pete/ -exec grep -l $pattern {} \;` ; do
    ls -l $file ;
    sed -e 's/$pattern/newpattern/' $file > $file.tmp
    mv $file.tmp $file
    
    echo "Done"
    done | mail user@mydomain
    I'm hoping that this file will search for all files in /root/pete/ which contain "this is string one" and change it to "this is string two".

    Unfortunately, I get "line 10: syntax error: unexpected end of file"

    Eventually, I need to end up with a script which will search the entire server, for file names, directory names or file contents containing a string and replace it with another string.

    Any help would be greatly appreciated. Thanks.

    pete

  2. #2
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,498
    Thanks
    17
    Thanked 788 Times in 506 Posts
    Rep Power
    10

    Default

    You are missing done and grep strings should be double quoted:
    Code:
    for pattern in "this is string one" "this is string two"
    do
      for file in `find /root/pete/ -exec grep -l "$pattern" {} \;` ; do
       ls -l $file ;
       sed -e 's/$pattern/newpattern/' $file > $file.tmp
       mv $file.tmp $file
       echo "Done"
      done
    done | mail user@mydomain
    A better way is to use egrep directly on /root/pete/ and put it through via while read .. done loop.
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  3. The Following User Says Thank You to nixcraft For This Useful Post:

    snowweb (21st April 2010)

  4. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Thanks for that nixcraft, I'm still having problems though.

    First question is, is this script going to rewrite every single file in the directory path, regardless of whether the pattern matches?

    Second, I corrected the code as you said. This is it now,

    Code:
    for pattern in "this is string one" "this is string two"
    do
       for file in `find /root/pete/test/ -exec grep -l "$pattern" {} \;` ; do
          ls -l $file ;
          sed -e 's/$pattern/newpattern/' $file > $file.tmp
          mv $file.tmp $file
          echo "Done"
       done
    done | mail "user@domain"
    I also put the sample file with the pattern in it, in a subdirectory one level lower and altered the search path, as I think it was finding this script during the search and was encountering an access denied error when it tried to rewrite it.

    It now runs without errors and I get an email at the end, but the sample file doesn't get changed. What am I doing wrong please?

    What might be a better solution, is if the script can instead, just provide a list of the files with paths, where the pattern is found. I think it will find around 10 files max.

    Thanks,

    pete

  5. #4
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    0
    Thanked 6 Times in 5 Posts
    Rep Power
    0

    Default Quotes

    Change the line:
    Code:
    sed -e 's/$pattern/newpattern/' $file > $file.tmp
    to
    Code:
    sed -e "s/$pattern/newpattern/" $file > $file.tmp
    The single quotes (') wouldn't do variable expansion and hence there would be no replacement. Which is why we need double quotes there (")

    Besides, you can also replace:
    Code:
    sed -e 's/$pattern/newpattern/' $file > $file.tmp
    mv $file.tmp $file
    with
    Code:
    sed -i "s/$pattern/newpattern/" $file
    which would edit the file itself. You can check the man page for details.


    Thanks,
    NNS

  6. The Following User Says Thank You to nnsjunior For This Useful Post:

    snowweb (21st April 2010)

  7. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Thanks for that nnsjunior.

    I'm a little closer now! I updated my code and am now using:

    Code:
    for pattern in "old string" "new string"
    do
       for file in `find /root/pete/test/ -exec grep -l "$pattern" {} \;` ; do
          ls -l $file ;
          sed -i "s/$pattern/newpattern/" $file
          echo "Done"
       done
    done | mail "peter@domain"
    It now does edit the file and it replaced "old string" with "newpattern", but I was kinda hoping that it would replace the old string with "new string". I must have messed up the syntax for accessing the second string in the pattern. What have I done wrong please?

    Thanks for your patience.

    pete

  8. #6
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    0
    Thanked 6 Times in 5 Posts
    Rep Power
    0

    Default

    Then the first for loop is unnecessary. See if this works for you:

    Code:
    pattern="old string"
    newpattern="new string"
    for file in `find /root/pete/test/ -exec grep -l "$pattern" {} \;` ; do
          ls -l $file ;
          sed -i "s/$pattern/$newpattern/" $file
          echo "Done"
    done | mail "peter@domain"
    Thanks,
    NNS

  9. The Following User Says Thank You to nnsjunior For This Useful Post:

    snowweb (21st April 2010)

  10. #7
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Smile

    NNS, that's done the trick thanks.

    I'll have some fun modifying it now. It always helps when you want to learn by modifying a script, if you can start out with a working one!

    Cheers.

    peter

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [Solved] .bash_profile error syntax error: unexpected end of file
    By shoboy in forum Shell scripting
    Replies: 7
    Last Post: 17th March 2011, 09:50 PM
  2. Replies: 6
    Last Post: 9th September 2010, 09:55 PM
  3. [HowTo] Bash Shell Script To Ftp File To Server
    By jaysunn in forum Getting started tutorials
    Replies: 8
    Last Post: 1st January 2010, 11:24 AM
  4. error : unexpected inconsistency
    By chaitanyarhce in forum Linux software
    Replies: 1
    Last Post: 13th September 2006, 03:15 AM
  5. error : unexpected inconsistency
    By chaitanyarhce in forum Linux software
    Replies: 0
    Last Post: 12th September 2006, 05:48 PM

Tags for this Thread

Posting Permissions

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