Results 1 to 6 of 6

Thread: Syntax error: word unexpected (expecting "do")

  1. #1
    Join Date
    Apr 2009
    Beans
    3

    Syntax error: word unexpected (expecting "do")

    Hi! i am writting a code to delete excel reports older than one day.
    The code is as follows:

    path="/home/directory/Reports"
    echo "The reports older than one day are getting deleted..."
    for i in `find $path/*.xls -mtime +1 -print`
    do
    echo "File $i is deleted"
    sudo rm $i
    done
    echo "Deletion is completed"

    When i am executing this script, it is giving me the error that
    Syntax error: word unexpected (expecting "do")

    but if i write the same code on command prompt instead of in shell script, it is getting executed properly. Please let me know where i am getting wrong.

    Thanks in advance

  2. #2
    Join Date
    Apr 2008
    Location
    Campinas, SP - Brasil
    Beans
    355

    Re: Syntax error: word unexpected (expecting "do")

    Put this in the first line of your script file:

    #!/bin/sh

    This sets which interpreter you are using (in this case, sh, or bash). This
    may solve the problem. I cannot see anything else now.

    Just for curiosity: Do you really need root privileges to delete the files? (Is the use of "sudo" necessary, it seems strange, and dangerous).

  3. #3
    Join Date
    Oct 2007
    Beans
    441

    Re: Syntax error: word unexpected (expecting "do")

    The other thing, if that doesn't solve it, is that you might need a semicolon at the end of the for line.

    Also, if you post code, you can put it inside tags to make it formatted so its easier to read. Its ok here but helps for longer snippets. Just put it inside [code] and [/code] tags

  4. #4
    Join Date
    Apr 2009
    Beans
    3

    Re: Syntax error: word unexpected (expecting "do")

    Well, even after adding #!/bin/sh, the code is giving same error.

    The code snippet is as follows:
    Code:
    #!/bin/sh
    path="/home/directory/Reports"
    echo "The reports older than one day are getting deleted..."
    for i in `find $path/*.xls -mtime +1 -print`;
    do
    echo "File $i is deleted"
    rm $i
    done
    echo "Deletion is completed"

    The output is as follows:
    $sh Reportremoval.sh
    The reports older than one day are getting deleted...
    Reportremoval.sh: 5: Syntax error: word unexpected (expecting "do")


    Please give me some suggestion.
    Last edited by rohandeshmukh; April 21st, 2009 at 06:28 AM.

  5. #5
    Join Date
    Oct 2007
    Beans
    441

    Re: Syntax error: word unexpected (expecting "do")

    I think it needs more semicolons You can never have too many. When you type it in the shell it adds them automatically, so put them after the "echo" and "rm" lines.

    Edit: It also occurred to me that this could be made much easier - find has a delete option:
    Code:
    find $path/*.xls -mtime +1 -delete

  6. #6
    Join Date
    Apr 2009
    Beans
    3

    Re: Syntax error: word unexpected (expecting "do")

    I think the proble was associated with the permission. I changed the user name and re-executed the same script and this time it worked fine!

    anyways, Thanks for your help!

Bookmarks

Posting Permissions

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