0

I'm converting a script to utilize arguments, but it seems that my case statement is not evaluating all of the options I've provided. Below is the argument that I coded which is not working:

#!/bin/zsh

...

############################################################
# Delete Screenshots                                       #
############################################################
Delete()
{
   echo "\n*** DELETING SCREENSHOTS FROM ~/Pictures/Screenshots ***\n"
   rm  ~/Pictures/Screenshots/*.png
   echo "Done!"
}

...

############################################################
############################################################
# Main program                                             #
############################################################
############################################################

while getopts ":chapd:" option; do
   case $option in
      c) # Create Directories
         CreateDir
         exit;; 
      h) # Display Help
         Help
         exit;;
      a) # Archive
         Archive
         exit;;
      p) # Purge Archive
         pArchive
         exit;;
      d) # Delete Screenshots
         Delete
         exit;;
     \?) # Invalid option
         echo "ERROR: Invalid option!"
         exit;;
   esac
done

When testing, all options appear to work (c,h,a,p) with the exception of d. Can anyone help me understand why all of the options work except d? Is there a limitation of case that I'm not aware of? I tried searching for this answer, but I could not find something similar. I run the program like this: hsshots -d. This option only fails without any error. I tried adding an echo "DEBUG" just to see if there would be any output. I've even deleted the rm command to see if only the echo would prompt, but it does not. It seems to me that case is just ignoring this last option.... OR getopts is somehow ignoring this option as an option; I'm not sure.

Edited for simplicity as requested.

P05TMAN
  • 123
  • 1
  • 8
  • 3
    How are you using `-d`? Did you provide the argument that your optstring defines it to take? (Every option that is followed by `:` has a *required* argument..) – chepner Feb 25 '23 at 18:21
  • I changed the tag, as your script is clearly for `zsh`, not `bash` – cornuz Feb 25 '23 at 18:24
  • Thanks, I tried to set that tag, but it said it was a "new tag" and I couldn't add it due to rep... – P05TMAN Feb 25 '23 at 18:25
  • 2
    consider reducing this question to a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example); in particular, we don't need to see 80 lines of function definitions (especially don't need to see dozens of `echo` calls); what we do need to see is the actual case statement and the sample calls of your script (w/ command line args); also, a brief description of what you mean by option `d` not working ... error message? no message? it hangs? something else? – markp-fuso Feb 25 '23 at 18:25
  • @chepner: I did in the while statement: `while getopts ":chapd:" option; do` ..then defined option `d`: ` d) # Delete Screenshots Delete exit;; ` – P05TMAN Feb 25 '23 at 18:27
  • 1
    But when you call the script, are you using `myscript -d foo` or just `myscript -d`? You haven't said *what* isn't working. – chepner Feb 25 '23 at 18:28
  • 2
    Show how you're calling the program. You haven't ruled out the observation chepner added in the very first comment, that putting a `:` after the `d` specifies that an option-argument is needed at invocation time. Certainly from your help string I don't see anything indicating that the `:` is correct/appropriate. – Charles Duffy Feb 25 '23 at 18:28
  • @chepner: ah I misunderstood... sorry. Program called like `hsshots -d`. I'm editing this to include. – P05TMAN Feb 25 '23 at 18:29
  • 2
    If it's just `hsshots -d`, then the `:` at the end of your getopts string is wrong for the reason chepner told you in the very first comment. Take it out. It should just be `getopts :chapd option` – Charles Duffy Feb 25 '23 at 18:30
  • Hmm. [unix getopts for multiple switches](https://stackoverflow.com/questions/46734649/unix-getopts-for-multiple-switches) is arguably a stronger duplicate. Still touches on the same `:`-only-after-a-switch-with-an-optarg problem, but it's explicitly about getopts in shell, not about the C function it's modeled from. – Charles Duffy Feb 25 '23 at 18:33
  • BTW, if you're going to replace removed code with `...`, put that `...` in a comment. We want a [mre] -- it's supposed to be minimal, yes, but it's also supposed to produce _the exact error you're asking about_ when run without changes, not a different error about a `...` command not being found. – Charles Duffy Feb 25 '23 at 18:35
  • BTW, `hsshots -d something` would have worked with your original code -- the "something" in this case becomes an optarg, an _argument to an option_, like the `:` tells getopts to expect. – Charles Duffy Feb 25 '23 at 18:41
  • @chepner: Your suggestion was correct. I didn't quite understand what you were getting at. – P05TMAN Feb 25 '23 at 18:41
  • @CharlesDuffy's clarification drove it home. Thank you both! BTW, Charles, that link does clarify much better than the manpage. I just wanted the argument alone: `hsshots -d` rather than `hsshot -d something`. I didn't quite understand this. Thank you. – P05TMAN Feb 25 '23 at 18:41

0 Answers0