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 "$@";

    if [[ -n $confirm ]]; #this is a getopts switch asking for confirmation like rm -i
        #asks user whether to confirm deletion
        if [ $answer != [Yy]* ]];
            continue #go to next param argument

    if [ -z "$param" ] #if no argument has been specied,
         #print an error that additional operand is needed.

    elif [ -d ./$param ] #if a directory name is specified
        if [[ -n $recursive]] #getops recursive switch, like rm -r
            #recursively delete a directory
        #error message about deleting directory without -r switch
    elif [ ! -e ./$param ] 
        #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" ]]
        #This prevents script from trying to delete itself or restore script

    if [[ -n $verbose ]] third and final getopts switch, similar to rm -v
        #message confirming deletion

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)

closed as off-topic by G-Man, Michael Homer, Anthon, Archemar, John WH Smith Aug 24 '15 at 18:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – G-Man, Michael Homer, Anthon, Archemar, John WH Smith
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    "script with no arguments should echo a message, but it doesnt". No surprise it doesn't echo - there is no echo command anywhere. – jimmij Aug 23 '15 at 23:45
  • that's not a valid if statement is it, where is the then – gwillie Aug 23 '15 at 23:47
  • I've only provided the actual conditions as I thought the ordering of the conditions might be the problem. If there isn't I could try and add more code to it? – user3120554 Aug 23 '15 at 23:59
  • Yes please try to come up with a minimal, complete, and verifiable example of the problem – steeldriver Aug 24 '15 at 0:13
  • I hope the edit helps – user3120554 Aug 24 '15 at 0:35

The for loop being used to iterate over the arguments terminates and goes to its done statement when its condition is met (the end of the list). When the script reaches the for loop with no arguments the start of the list is the same as the end, and the loop's condition is false.

In the original example the commands inside of the loop would have produced the incorrect result given an empty string. If the variable "param" was empty the first case [ -d ./$param ] would match the current directory ./ before and the script reached the check for an empty string [ -z "$param" ].

  • I tried moving the [ -z "$param" ] test to the top, but it still doesn't seem to work. If it matters, all the tests are in a giant for loop for param in "$@"; do – user3120554 Aug 24 '15 at 0:07
  • If no argument is specified then the for loop has nothing to iterate over and gets skipped over. – kdhp Aug 24 '15 at 0:38
  • surely the for loop will just have one iteration? – user3120554 Aug 24 '15 at 0:42
  • 1
    It will check its condition at the start of the loop like a while loop, if this condition is not met then it will go to the end of the loop. – kdhp Aug 24 '15 at 0:46
  • Thanks, I moved the test outside the loop and it worked. I wonder why it worked on a different script and i noticed it didnt have a loop either – user3120554 Aug 24 '15 at 0:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.