I'm questioning why my script still does not run the condition when no argument is provided. I think it might be the ordering of my if statements but I'm not sure. Any advice? It doesn't seem to be a simple mistake like misplaced white spaces.
for param in "$@"; do if [[ -n $confirm ]]; #this is a getopts switch asking for confirmation like rm -i then #asks user whether to confirm deletion if [ $answer != [Yy]* ]]; then continue #go to next param argument fi fi if [ -z "$param" ] #if no argument has been specied, then #print an error that additional operand is needed. elif [ -d ./$param ] #if a directory name is specified then if [[ -n $recursive]] #getops recursive switch, like rm -r then #recursively delete a directory fi #error message about deleting directory without -r switch elif [ ! -e ./$param ] then #If not an existing file either then print error that there is no such file or directory elif [[ $param = "safe_rm" ]] || [[ $param = "safe_rm_res" ]] then #This prevents script from trying to delete itself or restore script fi if [[ -n $verbose ]] third and final getopts switch, similar to rm -v then #message confirming deletion fi done
My code is about making a recycling bin, it is based off of the
rm command where the scripts also has and uses switches the same way as
rm -i -v and
-r. The if statements above changes how I handle deletion depending on the arguments.The first if statement is about whether the argument is a directory. The second is whether it is a file, the third is whether it is empty, the fourth is whether the argument is itself (deleting itself)