954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

shell scripting problem 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 :D :D

Chowley
Newbie Poster
2 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Error at line #37. Seems like you're trying to implement an else type of thing. Replace elif with else.

nileshgr
Junior Poster
166 posts since Aug 2009
Reputation Points: 17
Solved Threads: 23
 

Ah thank you, that elif before mkdir. I cant believe i didnt that, also didnt spot it. Too early in the morning!

Thanks very much :)

Chowley
Newbie Poster
2 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You