Dynamic patterns for the Case Statement in Shell Scripting -
the code below rough outline of trying do. don't worry code doing after each case evaluated.
my question in case *','* )
trying recognize dynamic pattern of files separated commas.
ex: getfile.sh -fileextension file1,file2,file3,file4 input. how go recognizing input $2(this equal var2) follows *','*
pattern * represents file?
if [ $flag -eq 1 ]; case $var2 in #lists files if option selected list | l | list | l | ls | ls) stuff here | | | a) stuff here *','* ) stuff here * ) stuff here esac fi
i should add program supposed list set of files based on extension , allow user either files, or select mutliple (or single) file. because not know user going input i'm trying match pattern instead of static input.
so here new code looks , closer i'm trying accomplish:
let's jsut concentrate on case not confused.
case $var2 in #lists files if option selected list | l | list | l | ls | ls) ls -1 $directory*$extension exit ;; #copy files in directory | | | a) echo ">cleaning temp dir" allcheck=1 cleantemp copy zip exit ;; [0-9][0-9a-za-z] | [0-9]','[0-9a-za-z]) echo "this valid input" exit ;; * ) echo ">>bad argument" #help exit 1 #fi esac
this pattern i'm trying match
[0-9]','[0-9a-za-z]
i figured out after finding link gave me clue: how test if variable number in bash?
anyways here's ended with:
case $var2 in #lists files if option selected list | l | list | l | ls | ls) ls -1 $directory*$extension exit ;; #copy files in directory | | | a) echo ">cleaning temp dir" allcheck=1 cleantemp copy zip exit ;; [0-9][0-9a-za-z] | [0-9][0-9a-za-z],[0-9][0-9a-za-z],*) echo "this valid input" exit ;; * ) echo ">>bad argument" #help exit 1 #fi esac
this test input such 56 or 5a [0-9][0-9a-za-z]
test input separated comma such tryign accomplish. [0-9][0-9a-za-z],[0-9][0-9a-za-z],*
(i still have make sure invalid characters not entered, however, cut down on work have now.)
thanks trying 'the other guy'.
your code correctly matches values of var2
containing commas:
flag=1 var2="file1,file2,file3,file4" if [ $flag -eq 1 ]; case $var2 in #lists files if option selected list | l | list | l | ls | ls) stuff here ;; | | | a) stuff here ;; *','* ) echo "it works" ;; * ) stuff here ;; esac fi
will print "it works".
if problem how separate comma separated list, can use
ifs="," file in $var2 echo "processing $file" done
Comments
Post a Comment