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:
printf %s\\n ...prints each of arguments on single line.grep -fvxsearchs exact matches (-f) of whole line (-x) , inverts match result (-v)."${array[@]/#/-e}"prepends-eeach element of arrayarray, useful when need provide each element of array (repeated) command-line option utility. in case, utilitygrep,-eflag 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
Post a Comment