0

Simple question. In a cli script

Do you want to proceed?

If user says "no". Exit code must be 0? Or not and why?

Iván E. Sánchez
  • 1,183
  • 15
  • 28
  • 1
    If you're writing the program, it's entirely up to you. Exit status 0 means "everything's OK", any other status means some error or exceptional circumstance. In your situation, if the user chooses to not proceed, is that considered an error? – glenn jackman Nov 14 '18 at 16:29
  • Yep, I'm coding it. But I mean is that an error? the program's behavior is correct. – Iván E. Sánchez Nov 14 '18 at 16:36
  • related: [How do I prompt for yes-no-cancel in a Linux shell script](https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script) – kvantour Nov 14 '18 at 16:53
  • 2
    If you want to be able to use the script in a condition in another script, like `if askuser; then` where `askuser` is your script and you want to stop if they say "no", then you should return `1` if they say no. If you'll use the script stand-alone and it's not considered an error if they say "no", it should return `0`. – Benjamin W. Nov 14 '18 at 16:56
  • 1
    Imagine you want to do something like `commandWithQuestion && command` and you don't want to run `command` if you answer `no`, then you should exit with `1`. But if you do want to run it if you answer `no`, then you should exit with `0`. It all depends on what your intentions are. – kvantour Nov 14 '18 at 16:57
  • Right so basically, if another script depends not only on the correct execution of this script, but also on some user confirmation. It must throw something different than 0 to capture that. Am I right? – Iván E. Sánchez Nov 14 '18 at 17:25
  • 1
    @IvánSánchez sort of. If you want to continue your script (answer "yes") then the script runs trough and does its thing. Finally, it will exit with exit code `0` (unless an error happens and it exits with something else). If you want your answer `no` to be distinguishable from 100% success, then it should exit with a different exit code. – kvantour Nov 14 '18 at 17:49
  • nice, that's what I thought. I think we need better conventions for this. Because everyone could have it's own definitions of succeed. Anyway. Thanks – Iván E. Sánchez Nov 14 '18 at 19:25

1 Answers1

-1

From the program perspective definitely it's 0. However I've written few scripts where the exit code was captured to explain the exit status of the program. In that case I've captured the system exit code and also assigned new exit codes for each options selected to inform the final status of the program execution. Hope this helps!

Vino
  • 91
  • 3
  • 9