How to exit a shell script using gum #351
-
ProblemFirstly, thank you producing such a great tool! I'm having issues quitting a program using gum. To ReproduceGiven the following shell script. If I press gum confirm "foo"
gum confirm "bar"
gum confirm "baz" Expected behaviorMore of a question, is this expected? Is there a way to exit the program entirely? Setup InfoOS: Ubuntu 20.04.3 LTS |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hey @jbmoorhouse! Yes, this is bound to happen, as You can propagate it by adding a #!/bin/bash
set -e
gum confirm "foo"
gum confirm "bar"
gum confirm "baz" That means that if anything inside the script has an exit code > 0, it will fail right away, so pressing CTRL+C (or replying "no") in any of the confirms quits the entire script propagating the exit code. All this to say that, in your script, if you don't wan't the script to on, say, replying "no", you'll need to handle the exit codes. For reference,
Here's an example handling the exit codes: #!/bin/bash
set -e
if gum confirm "Do you want some soda?" ; then
if gum confirm "What about some chips with that soda?" ; then
gum format "OK, here's your soda with chips!"
exit 0
elif test $? -eq 130; then
exit 130
else
gum format "OK, here's your soda!"
exit 0
fi
elif test $? -eq 130; then
exit 130
else
if gum confirm "OK, no soda, what about some coffee?"; then
gum format "Cool, here's your coffee!"
elif test $? -eq 130; then
exit 130
else
gum format "OK, just water then, incoming!"
exit 0
fi
fi Hope that helps :) |
Beta Was this translation helpful? Give feedback.
-
Does this work even for Like when I try to assign a value to a variable via |
Beta Was this translation helpful? Give feedback.
Hey @jbmoorhouse!
Yes, this is bound to happen, as
gum
is another process within the script.You can propagate it by adding a
set -e
, e.g.:That means that if anything inside the script has an exit code > 0, it will fail right away, so pressing CTRL+C (or replying "no") in any of the confirms quits the entire script propagating the exit code. All this to say that, in your script, if you don't wan't the script to on, say, replying "no", you'll need to handle the exit codes.
For reference,
gum confirm
:exit 0
on "Yes"exit 1
on "No"exit 130
on CTRL+CHere's an example handling the exit codes: