Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

help with shell scripting "syntax error near unexpected token"

  • 27-11-2009 8:30am
    #1
    Registered Users, Registered Users 2 Posts: 584 ✭✭✭


    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

    + ''
    + 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 :D:D


Comments

  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    It would really help if you indented your code.

    Stupid question, but lines like these:

    read subdirectory

    Are these actual commands, or should they be comments?


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    eoin wrote: »
    It would really help if you indented your code.

    Stupid question, but lines like these:

    read subdirectory

    Are these actual commands, or should they be comments?
    read var - reads stdin to var so you can access via $var

    However, at the end you used an elif instead of an else which expects a condition and a then
    [B]elif[/B]
    mkdir $subdirectory
    cp $file
    
    fi
    exit
    

    Btw, in future use code tags to encapsulate your code!


  • Registered Users, Registered Users 2 Posts: 584 ✭✭✭BeansBeans


    Webmonkey wrote: »
    read var - reads stdin to var so you can access via $var

    However, at the end you used an elif instead of an else which expects a condition and a then
    [B]elif[/B]
    mkdir $subdirectory
    cp $file
    
    fi
    exit
    

    Btw, in future use code tags to encapsulate your code!

    the code tags didnt work for me when i tried to preview :o or my brain is just gone down the toilet.

    Thanks very much i cant believe i didnt see the elif there. Thats weird i dont get how i stuck that in there. too early in the morning i think.

    Thank you. :)


Advertisement