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
2
Answers
The third
echo
command has an unterminated string literal:Perhaps you wanted to write this instead:
The fifth
echo
command has the same problem.Also, this statement is not valid:
Perhaps you wanted to write this instead:
Also, there is no
fi
corresponding to the firstif
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"]
.On line 7, you are closing your brackets too fast:
Instead of:
you should have: