我想将我在UPPERCASE中输入的任何输入转换为小写,如何使用此sed命令使这个bash脚本部分工作以降低任何输入?
selection=
until [ "$selection" = "0" ]; do
echo -n "Enter selection: "
sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'
read selection
echo ""
case $selection in
答案 0 :(得分:1)
您打算写一些类似
的内容selection=
until [ "$selection" = "0" ]; do
echo -n "Enter selection: "
read selection
selection=$(printf %s "$selection" | sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/')
echo "$selection"
case $selection in
使用更简单的tr
命令:
selection=$(printf %s "$selection" | tr '[a-z]' '[A-Z]')
# or tr '[:lower:]' '[:upper:]'
使用bash
4或更高版本中提供的功能:
declare -u selection # Convert any lowercase characters to uppercase upon assignment
until [[ $selection = 0 ]]; do
read selection
case $selection in
或
until [[ $selection = 0 ]]; do
read selection
selection=${selection^^} # expand with lowercase to uppercase
case $selection
答案 1 :(得分:0)
您可以使用typeset
大写:
;-> typeset -u selection
;-> selection="UP and down"
;-> echo "${selection}"
UP AND DOWN
小写:
;-> typeset -l selection
;-> selection="UP and down"
;-> echo "${selection}"
up and down
我不会使用sed
。如果您想使用sed
,可以使用
sed -r "s/(.)/\l\1/g" <<< "${selection}" # Better use tr, typeset or ${selection,,}