Home > front end >  Can you help me solve this bash syntax error?
Can you help me solve this bash syntax error?

Time:04-11

I am receiving an error within CentOS saying

'line 7: syntax error near unexpected token `then' and 'syntax error: unexpected end of file'

The point of this code is to ask a user for a filename and then either copy move or delete the file based off what they pick.

echo "Please enter the file name you wish to alter: "
read filename

if [ -f $filename ]; then
echo "Please enter either C, M, or D to copy, move or delete the file: "
read op
        if [ "$op" = "C" ] || [ "$op" = "c" ]; then
echo "Please enter the destination directory in which you wish to copy to$
read dest
cp $filename $dest/$filename
echo "Complete"

elif [ "$op" = "M"] || [ "$op" = "m" ]; then
echo "Please enter the destination directory in which you wish to move to$
read dest
mv $filename $dest/$filename
echo "Complete"

elif [ "$op" = "D"] || [ "$op" = "d" ]; then
rm -i $filename
echo "Complete"

else "$filename does not exists try again."

fi

CodePudding user response:

On line 7, you are closing your brackets too fast:
Instead of:

if [ "$op" = "C"]  ||  ["$op" = "c" ]; then

you should have:

if [ "$op" = "C"  ||  "$op" = "c" ]; then

CodePudding user response:

The third echo command has an unterminated string literal:

echo "Please enter the destination directory in which you wish to copy to$

Perhaps you wanted to write this instead:

echo "Please enter the destination directory in which you wish to copy to:"

The fifth echo command has the same problem.

Also, this statement is not valid:

else "$filename does not exists try again."

Perhaps you wanted to write this instead:

else
    echo "$filename does not exists try again."

Also, there is no fi corresponding to the first if statement.

Also, the syntax [ "$op" = "M"] is invalid. You must have a space before the ] character, like this: [ "$op" = "M" ].

You have the same problem again in [ "$op" = "D"].

  • Related