bashに配列があります。
WHITELIST=(
"THIS"
"examPle"
"somTHing"
)
既存または新しい配列ですべての要素を小文字に変換するにはどうすればよいですか?
配列全体を一度に変換できます。
WHITELIST=( "${WHITELIST[@],,}" )
printf "%s\n" "${WHITELIST[@]}"
this
example
somthing
使用できます${parameter,,}
:
WHITELIST=(
"THIS"
"examPle"
"somTHing"
)
i=0
for elt in "${WHITELIST[@]}"
do
NEWLIST[$i]=${elt,,}
i=$((${i} + 1))
done
for elt in "${NEWLIST[@]}"
do
echo $elt
done
マンページから:
${parameter,,pattern} Case modification. This expansion modifies the case of alpha‐ betic characters in parameter. The pattern is expanded to pro‐ duce a pattern just as in pathname expansion. The ^ operator converts lowercase letters matching pattern to uppercase; the , operator converts matching uppercase letters to lowercase. The ^^ and ,, expansions convert each matched character in the expanded value; the ^ and , expansions match and convert only the first character in the expanded value. If pattern is omit‐ ted, it is treated like a ?, which matches every character. If parameter is @ or *, the case modification operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the case modification operation is applied to each member of the array in turn, and the expansion is the resultant list.
それを行う1つの方法:
$ WHITELIST=("THIS" "examPle" "somTHing")
$ x=0;while [ ${x} -lt ${#WHITELIST[*]} ]
do WHITELIST[$x]=$(tr [A-Z] [a-z] <<< ${WHITELIST[$x]})
let x++
done
$ echo "${WHITELIST[@]}"
this example somthing