I am a beginner at unix and this is my first for loop with if statement. I am getting an error which says syntax error near unexpected token 'then'. Please can you help me fix this. What have i done wrong.
for file in $@
do
if [[ ! -f $@ ]]
then
echo "Error. File does not exist."
exit $ERROR_NO_FILE
elif
then
chmod 755 $file
echo "File permissions have been changed."
exit $SUCCESS
fi
done
[[ ! -f $@ ]]? Eh?-fcan't take an arbitrary number of arguments, and you don't know how many things$@will expand to. You want[[ ! -f "$file" ]](lack of quoting acceptable there because of side effects of[[ ]], though it still doesn't hurt).for file in "$@",chmod 755 "$file", etc; -- as it is, this will break badly with filenames with spaces. Run your code through shellcheck.net and fix what it finds.for f in $@. Quotes are important. It should be eitherfor f in "$@"; do...orfor f; do ...unless you have a very special use case and know the difference and you actually want an unquoted $@. In that case you will want to add a comment explaining exactly what motivates the unusual usage.