Reputation: 67
I have this function
function doit {
echo " Do you want to continue? {y/n} "
case $opt in
y) ??????
n) exit ;;
esac
echo " 32b or 64b? {32/64} "
case $opt in
32) do32 ;;
64) do64 ;;
esac
}
How do I make this work ? I need to keep moving forward in the same function after the YES is chosen.
Upvotes: 0
Views: 827
Reputation: 246847
I wouldn't use a case
statement when you only care about one of the branches:
function doit {
read -p " Do you want to continue? {y/n} " ans
[[ $ans == [nN]* ]] && return 1
PS3=" 32b or 64b? "
select size in 32 64; do
case $size in
32|64) break ;;
esac
done
do${size}
}
Upvotes: 2
Reputation: 189477
This is extremely unclear, but I guess you mean
doit () {
read -p " Do you want to continue? {y/n} " opt
case $opt in
y) ;;
n) return 1 ;;
esac
read -p " 32b or 64b? {32/64} " opt
case $opt in
32) do32 ;;
64) do64 ;;
esac
}
The option to do nothing at all in the y
case seems to be the answer to your question. But notice also the use of return
rather than exit
to leave the function, the absence of a function
keyword in the function declaration (this is only a matter of preference really; but I see no advantage of the Bash-only syntax variant), and the use of read
to read input (I guess that's what you wanted here?). The return
argument 1
signals failure to the caller, so you can say e.g.
doit || echo "$0: doit failed or aborted" >&2
Strictly speaking, the first case
only needs an n
branch; you'll notice that the code simply falls through if the answer is not either n
or y
, and this behavior is actually suitable for the y
case too. Where this is not the case, a final *)
case can be used to handle all the otherwise unhandled values.
Upvotes: 2