0

I am writing a bash script that is suppose to auto restart my autofs in a loop. but when I try to run it I just get a syntax error.

#./dd_nfs.sh
./dd_nfs.sh: line 3: syntax error near unexpected token `/etc/init.d/autofs'
./dd_nfs.sh: line 3: `/etc/init.d/autofs reload'

# cat dd_nfs.sh
    #!/bin/bash
    for i in `seq 1 10`
    /etc/init.d/autofs reload
    sleep 5
    echo "read test"
    do time echo "read"
    echo "read test done"
    done

I tried the dos2unix. I replaced line 3 with just 'pwd' to print my current dir and I tried to strip out the /r but I still get the same error. So I am not sure what's going here.

Has anyone seen this before? Thanks

5
  • 1
    The pattern for bash is for ... do ... done. You missed the do Commented May 8, 2015 at 18:13
  • you need a line do after the for loop definition, before the autofs reload line Commented May 8, 2015 at 18:13
  • do is there, I just commented it out along with the bulk of the code. Commented May 8, 2015 at 18:19
  • 1
    The do has to be immediately after the for; you can't just add it on some arbitrary line. The line you added in the edit will just cause another syntax error. Consult the bash documentation for the syntax of a for loop. Commented May 8, 2015 at 18:20
  • 1
    Your edit also changed the indentation of the script after the cat command. That matters; the #! must be at the very beginning of the line. Please include an actual copy-and-pasted script in your question, one that reproduces the problem. If your original script is too big, reduce it to something smaller, verify that the problem occurs with the smaller script, and copy-and-paste that into your question. By editing your script without checking it, you've been introducing new errors. Commented May 8, 2015 at 18:31

1 Answer 1

2

You have the wrong syntax for the for loop. It requires the do keyword.

Change this:

for i in `seq 1 10`

to this:

for i in `seq 1 10` ; do

Or, if you prefer, you can write it like this:

for in in `seq 1 10`
do
    # body of loop
done

(Also, indenting your code would make it easier to read.)

Since you're using bash, the $(command) syntax is IMHO easier to read than `command`:

for i in $(seq 1 10) ; do

And bash provides a special syntax for simple ranges:

for i in {1..10} ; do

In response to your latest edit, you added this line:

do time echo "read"

in the body of the loop. The do keyword is a syntax error in that context. The shell might not report it because of the previous syntax error caused by the missing do at the top of the loop.

Sign up to request clarification or add additional context in comments.

3 Comments

ah sorry, the do part is commented out by me. i will update my post to reflect that.
I got it working now. I misunderstood where the do should be. I thought it should go in front of the contents of the for loop but it doesn't. Thanks
@D.Zou: It does go in front of the body of the for loop. You added it to the middle of the for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.