Find the answer to your Linux question:
Results 1 to 3 of 3
Hello. I cannot for the life of me figure this out. It was all running fine until i added another while loop for the directory part, same as the first ...
  1. #1
    Just Joined!
    Join Date
    Nov 2009
    Posts
    1

    syntax error near unexpected token

    Hello.
    I cannot for the life of me figure this out. It was all running fine until i added another while loop for the directory part, same as the first one. Then i removed the loop and started getting this error.

    syntax error near unexpected token `fi'


    I have an assignment to write a script to where the user specifies a file to copy then a directory to copy it to, if the directory doesnt exist create it.

    the code is:


    #!/bin/bash -x

    while [ -z $file ]

    do

    echo "Please enter the name of the file you want to copy: "

    read file

    done

    if [ -f $file ]

    then

    echo "Enter the directory you want to copy the file to:"

    read subdirectory


    elif [ ! -f $file ]

    then

    echo "$file does not exist. Please run the script again and enter a valid filename"
    exit

    fi

    if [ -d $subdirectory ]

    then

    cp $file $subdirectory

    elif

    mkdir $subdirectory
    cp $file

    fi
    exit


    When i run it with bash -x

    it goes down to entering the directory

    + '[' -f file1 ']'
    + echo 'Enter the directory you want to copy the file to:'
    Enter the directory you want to copy the file to:
    + read subdirectory

    then i get

    ./assignment1.sh: line 42: syntax error near unexpected token `fi'
    ./assignment1.sh: line 42: `fi'

    after i enter a directory


    Starting to get panicky now, i have changed things around to no avail, i cannot understand what is wrong. Have to have the assignment done for saturday unfortunately.

    Anyway thanks in advance for any help forthcoming

  2. #2
    Just Joined!
    Join Date
    Aug 2005
    Posts
    65
    u have to read about the if sentence

  3. #3
    Linux Enthusiast KenJackson's Avatar
    Join Date
    Jun 2006
    Location
    Maryland, USA
    Posts
    506
    I can't exactly see what the problem is either, but here are some comments:
    Why not just use else here?
    Code:
    elif [ ! -f $file ]
    This line needs a condition. Or just replace elif with else.
    Code:
    elif
    BTW, if you click on the # symbol, you'll get a pair of code tags. If you put your code between them, it will make it look better and will make it easier for others to help you.

Posting Permissions

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