From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: Received: from dude02.hi.pengutronix.de ([2001:67c:670:100:1d::28] helo=dude02.lab.pengutronix.de) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jmARb-00035D-PR for ptxdist@pengutronix.de; Fri, 19 Jun 2020 08:24:07 +0200 Received: from mol by dude02.lab.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1jmARb-0001XG-HD for ptxdist@pengutronix.de; Fri, 19 Jun 2020 08:24:07 +0200 Date: Fri, 19 Jun 2020 08:24:07 +0200 From: Michael Olbrich Message-ID: <20200619062407.GJ9312@pengutronix.de> References: <20200617143125.23999-1-bst@pengutronix.de> <20200617143125.23999-2-bst@pengutronix.de> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20200617143125.23999-2-bst@pengutronix.de> Subject: Re: [ptxdist] [PATCH v3 1/6] 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 On Wed, Jun 17, 2020 at 04:31:20PM +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 > --- > Introduced in v3. Used to ask user about HSM type in a later commit. > --- > scripts/lib/ptxd_lib_template.sh | 26 ++++++++++++++++++++++++++ > 1 file changed, 26 insertions(+) > > diff --git a/scripts/lib/ptxd_lib_template.sh b/scripts/lib/ptxd_lib_template.sh > index f39e6e033..6b405763b 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]}" Wrong indention. I'll fix it when I apply this. Michael > +} > +export -f ptxd_template_read_options > > ptxd_template_read_name() { > ptxd_template_read "enter package name" package_name > -- > 2.27.0 > > > _______________________________________________ > ptxdist mailing list > ptxdist@pengutronix.de > To unsubscribe, send a mail with subject "unsubscribe" to ptxdist-request@pengutronix.de > -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ ptxdist mailing list ptxdist@pengutronix.de To unsubscribe, send a mail with subject "unsubscribe" to ptxdist-request@pengutronix.de