how to exit immediately with getopts option in bash script
-1
votes
1
answer
364
views
what I tried.
~~~sh
#!/bin/bash
# set -e
echo hello
while getopts sr o; do
case $o in
s)
echo "no"
yell() { echo "$0: $*" >&2; }
die() { yell "$*"; exit 111; }
try() { "$@" || die "cannot $*"; }
;;
r)
echo "yes"
yell() { echo "$0: $*" >&2; }
die() { yell "$*"; exit 111; }
try() { "$@" || die "cannot $*"; }
;;
\?) exit 3;; #invalid option
esac
done
echo after_case
~~~
What I am trying to do:
in terminal, the following 2 command:
bash /home/jian/Desktop/pgdev/test1.sh -s
bash /home/jian/Desktop/pgdev/test1.sh -r
do not print out aftercase
.
references: https://unix.stackexchange.com/questions/684717/how-to-use-getopts-in-bash
https://stackoverflow.com/questions/1378274/in-a-bash-script-how-can-i-exit-the-entire-script-if-a-certain-condition-occurs
Asked by jian
(597 rep)
Oct 12, 2023, 05:02 PM
Last activity: Oct 13, 2023, 03:22 AM
Last activity: Oct 13, 2023, 03:22 AM