Here is my shell script and the error I get when running it:
#!/bin/bash
path=$1
execute=$2
a=$3
operation=$4
name=$5
if [ "$operation" == "run" ]; then
cd $path
./$execute $a
fi
elif [ "$operation" == "copy" ]; then
mkdir -p $path
cp $execute $path/$name
fi
elif [ "$operation" == "delete" ]; then
rm $path
cd copy
rm $name
cd ..
rmdir copy
fi
./commandsScript.sh: line 14: syntax error near unexpected token `elif' ./commandsScript.sh: line 14: `elif [ "$operation" == "copy" ]; then'
I’ve spent a long time trying all sort if-else statements variances but have not found the error solution. May someone help?
Consider using a
caseexpression instead of a set of chainedifstatements:I’ve also taken the liberty to quote all of your parameter expansions, which is what you should get into the habit of doing to make your script robust against arguments/variables with embedded whitespace.
Also, I would advise investing in proper error handling.