help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Conditional actions according to user input


From: David
Subject: Re: Conditional actions according to user input
Date: Sun, 23 Apr 2023 04:34:22 +0000

On Sun, 23 Apr 2023 at 04:28, uzibalqa <uzibalqa@proton.me> wrote:

> I have a set of possible user input formats stared in an array
>
> For instance
>
> pary=( "-p+([[:digit:]])"  "-g+([[:digit:]])"  "-q" )
>
> I then want to match these with the actual user inputs
>
>   for gpn in "${pary[@]}"
>   do
>     if [[ "$arg" == $gpn ]]; then
>
> What I do after is dependent on whether the user used the digit form (e.g 
> "-p+([[:digit:]])")
> or the isolated predicate form (e.g. "-q").
>
> How can I handle the aforementioned functionality in a clear and compact way?

Don't use an array.

Use a case statement with a list of patterns, one pattern for each
user input format.
https://mywiki.wooledge.org/BashGuide/TestsAndConditionals#Choices_.28case_and_select.29



reply via email to

[Prev in Thread] Current Thread [Next in Thread]