bash - How to use goto statement in shell script -
i beginner in shell script. don't have idea how use goto statement. using following code.
start: echo "main menu" echo "1 copy" echo "2 exit" read num case $num in "1") echo "copynum" goto start:; "2") echo "haiiii"; goto start: *) echo "ssss"; esac
as others have noted, there's no goto
in bash
(or other posix-like shells) - other, more flexible flow-control constructs take place.
heading compound commands
in man bash
.
in case, select
command right choice. since how use may not obvious, here's started:
#!/usr/bin/env bash echo "main menu" # define choices present user, # presented line line, prefixed sequential number # (e.g., '1) copy', ...) choices=( 'copy' 'exit' ) # present choices. # user chooses entering *number* before desired choice. select choice in "${choices[@]}"; # if invalid number chosen, $choice empty. # report error , prompt again. [[ -n $choice ]] || { echo "invalid choice." >&2; continue; } # examine choice. # note choice string itself, not number # reported in $choice. case $choice in copy) echo "copying..." # set flag here, or call function, ... ;; exit) echo "exiting. " exit 0 esac # getting here means valid choice made, # break out of select statement , continue below, # if desired. # note without explicit break (or exit) statement, # bash continue prompt. break done
Comments
Post a Comment