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: alex xmb ratchev
Subject: Re: Conditional actions according to user input
Date: Sun, 23 Apr 2023 10:48:36 +0200

On Sun, 23 Apr 2023, 10:35 am alex xmb ratchev, <fxmbsw7@gmail.com> wrote:

> to make globs act instead of flat text , dont quote em
>
> arr=( "*.ext1" "*.ext2" )
> for f in ${arr[@]} ; do ..
>
> or for i in "${arr[@]}" ; do
> files=( $i )
>
> or just at beginning
> arr=( *.1 *.2 )
>
> u seem to have -p or -q somewhere at end , u can
> pary=( "-p+([[:digit:]])"  "-g+([[:digit:]])"  "-q" )
> files=( ${pary[@]/#/*.} )
>
> or , u go manually thru the list ?
>   for glob in "${pary[@]}" ; do
>  if [[ $glob == "-q" ]] ; then # quote
> files=( *.$glob ) # noquote
>  fi
>   done
>
> greets
>
>
> 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").
>>
>
u do parse args like this ?

t='"${arg#-?}"' # cut prefix for num only
 declare -A match=(
"-p+([[:digit:]])" "printf 'p %s\\n' $t"
"-g+([[:digit:]])" "printf 'g %s\\n' $t"
-q 'printf q\\n'
 )

set -- -q -g55 -p727

   for arg ; do
  for m in "${!match[@]}" ; do
 if [[ $arg == $m ]] ; then
eval "${match[$m]}"
continue 2
 fi
  done
printf 'nomatch %s\n' "${arg@Q}"
   done

~ $ bash c.1
q
g 55
p 727

How can I handle the aforementioned functionality in a clear and compact
>> way?
>>
>>
>>
>>
>>
>>


reply via email to

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