Skip items in bash when in exclusion array -


i have script loops on database names , if name of current database in exclusion array want skip it. how accomplish in bash?

excluded_databases=("template1" "template0")  database in $databases   if ...;     # perform on database...   fi done 

you can testing each name in turn, might better off filtering list in 1 operation. (the following assumes no name in $databases contains whitespace, implicit given for loop).

for database in $(printf %s\\n $databases |                   grep -fvx "${excluded_databases[@]/#/-e}");   # done 

explanation of idioms:

  1. printf %s\\n ... prints each of arguments on single line.

  2. grep -fvx searchs exact matches (-f) of whole line (-x) , inverts match result (-v).

  3. "${array[@]/#/-e}" prepends -e each element of array array, useful when need provide each element of array (repeated) command-line option utility. in case, utility grep , -e flag used provide match pattern.

i've been criticized in past printf %s\\n -- people prefer printf '%s\n' -- find first 1 easier type. ymmv.


as comment, seems better make $databases array $excluded_databases, allow names including whitespace. printf | grep solution still doesn't allow newlines in names; it's complicated work around that. if make change, you'd need change printf printf %s\\n "${databases[@]}".


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -

jquery - Keeping Kendo Datepicker in min/max range -