From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: In-Reply-To: <20200617143125.23999-2-bst@pengutronix.de> MIME-Version: 1.0 Message-Id: From: Michael Olbrich Date: Sat, 20 Jun 2020 00:04:06 +0200 Subject: Re: [ptxdist] [APPLIED] ptxd_lib_template: add ptxd_template_read_options List-Id: PTXdist Development Mailing List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: ptxdist@pengutronix.de Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ptxdist-bounces@pengutronix.de Sender: "ptxdist" To: ptxdist@pengutronix.de Cc: Bastian Krause Thanks, applied as bf1a401a968193cf8fd8ebc4a6f8c899c2c3bae3. Michael [sent from post-receive hook] On Sat, 20 Jun 2020 00:04:06 +0200, Bastian Krause wrote: > Read a variable from user input, limited to a set of options. > > Example: > > $ ptxd_template_read_options "select your option" TYPE "option A" "option B" "option C" > ptxdist: select option by number: > ptxdist: [1] option A > ptxdist: [2] option B > ptxdist: [3] option C > ptxdist: select your option................: 2 > $ echo $TYPE > option B > > The user can select 1, 2 or 3. Other input is not accepted. > > Signed-off-by: Bastian Krause > Message-Id: <20200617143125.23999-2-bst@pengutronix.de> > [mol: fix indention] > Signed-off-by: Michael Olbrich > > diff --git a/scripts/lib/ptxd_lib_template.sh b/scripts/lib/ptxd_lib_template.sh > index 3d47cd85a4c4..9cace6c14a0b 100644 > --- a/scripts/lib/ptxd_lib_template.sh > +++ b/scripts/lib/ptxd_lib_template.sh > @@ -30,6 +30,32 @@ ptxd_template_read() { > export "${2}" > } > export -f ptxd_template_read > +# > +# Read a variable from user input, limited to an array of options > +# > +# $1 prompt prefix > +# $2 variable name > +# $3..$n options > +# > +ptxd_template_read_options() { > + local -a options=("${@:3}") > + local i=1 > + echo "${PTXDIST_LOG_PROMPT}select option by number:" > + for option in "${options[@]}"; do > + echo "${PTXDIST_LOG_PROMPT}[${i}] ${option}" > + ((i++)) > + done > + while : ; do > + ptxd_template_read "${1}" chosen > + [ -n "${chosen}" ] && [ "${chosen}" -gt 0 ] && > + [ "${chosen}" -le "${#options[@]}" ] && > + break > + > + echo "${PTXDIST_LOG_PROMPT}invalid option" > + done > + export "${2}=${options[$chosen-1]}" > +} > +export -f ptxd_template_read_options > > ptxd_template_read_name() { > ptxd_template_read "enter package name" package_name _______________________________________________ ptxdist mailing list ptxdist@pengutronix.de To unsubscribe, send a mail with subject "unsubscribe" to ptxdist-request@pengutronix.de