Find the answer to your Linux question:
Results 1 to 2 of 2
./test.sh: line 3: syntax error near unexpected token `cd' ./test.sh: line 3: `cd $f' -------------------------------- #!/bin/bash for f in "dir1 dir2"; cd $f do print "print test1" echo "echo test1" ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Posts
    2

    cd error from for loop


    ./test.sh: line 3: syntax error near unexpected token `cd'
    ./test.sh: line 3: `cd $f'
    --------------------------------
    #!/bin/bash
    for f in "dir1 dir2";
    cd $f
    do
    print "print test1"
    echo "echo test1"
    cd ..
    done

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,599
    Placing quotes around dir1 dir2 result in "cd $f" evaluating to "cd dir1 dir2", which is an error. Try "for f in dir1 dir2 ; do ... ; done". Also, remember the spaces as shown before and after commands that are on the same line, including the commands "do" and "done". So, try this:
    Code:
    #!/bin/bash
    for f in dir1 dir2
    do
    cd $f
    print "print test1"
    echo "echo test1"
    echo dir=`pwd`
    cd ..
    done
    BTW, the "print" command is probably an error, depending upon your system, and will probably generate a "print: command not found" error.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

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