[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Conditional actions according to user input
From: |
alex xmb ratchev |
Subject: |
Re: Conditional actions according to user input |
Date: |
Sun, 23 Apr 2023 11:19:22 +0200 |
On Sun, 23 Apr 2023, 11:02 am alex xmb ratchev, <fxmbsw7@gmail.com> wrote:
>
>
> On Sun, 23 Apr 2023, 6:28 am 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").
>>
>
> another try
>
> set -- -q -q200 -v -v400
>
> for arg ; do
> [[ $arg != -* ]] &&
> continue # no arg
> pre=${arg%%[0-9]}
> if [[ $arg == *[0-9] ]] ; then
> num=${arg##[!0-9]?}
> printf 'arg %s num %s\n' "$pre" "$num"
> else
> printf 'arg %s nonum\n' "$pre"
> fi
> done
>
> ~ $ bash c.2
> arg -q nonum
> arg -q20 num 200
> arg -v nonum
> arg -v40 num 400
>
fixed shopt version
the nonshopt seems me unlogically bugged
~ $ bash c.2
arg -q nonum
arg -q num 200
arg -v nonum
arg -v num 400
~ $ cat c.2
shopt -s extglob
set -- -q -q200 -v -v400
for arg ; do
[[ $arg != -* ]] &&
continue # no arg
pre=${arg%%+([0-9])}
if [[ $arg == *[0-9] ]] ; then
num=${arg##+([!0-9])}
printf 'arg %s num %s\n' "$pre" "$num"
else
printf 'arg %s nonum\n' "$pre"
fi
done
greets
How can I handle the aforementioned functionality in a clear and compact
>> way?
>>
>>
>>
>>
>>
>>
Re: Conditional actions according to user input, alex xmb ratchev, 2023/04/23
Re: Conditional actions according to user input, alex xmb ratchev, 2023/04/23
- Re: Conditional actions according to user input,
alex xmb ratchev <=
Re: Conditional actions according to user input, alex xmb ratchev, 2023/04/23