How can I convert uppercase to lowercase or the other way around in Bash?
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
converting string to lower case in bash shell scripting
例如:
1 2 3 4 | echo *****Language translator***** echo please choose the language for Chinese enter c for French enter f |
我想,在简单的方式能够认识到这两个C和C是中国;与该事的F和F,公认的法国。
有一个办法是转换到下一切的案例吗?
在这部分的代码:
如果[美元] = C语言;然后回波信号的输入字的翻译是:"翻译_ _读到Word
您可以使用
1 | echo $language | tr '[A-Z]' '[a-z]' |
您可以使用以下大小写修饰符(来自
1 2 3 4 | ${parameter^} # Convert the first character in ${parameter} to uppercase ${parameter^^} # Convert all characters in ${parameter} to uppercase ${parameter,} # Convert the first character in ${parameter} to lowercase ${parameter,,} # Convert all characters in ${parameter} to lowercase |
因此,您的代码可能如下所示:
1 2 3 4 5 6 7 8 9 | # Read one character into $lang, with a nice prompt. read -n 1 -p"Please enter c for Chinese, or f for French:" lang if ["${lang,,}" =="c" ]; then echo"Chinese" elif ["${lang,,}" =="f" ]; then echo"French" else echo"I don't speak that language." fi |
我可能会使用案例陈述,不过还有其他方法:
1 2 3 4 5 6 | read language case"$language" in ([cC]) echo 'Chinese';; ([fF]) echo 'French';; (*) echo 'Unrecognized language abbreviation';; esac |
如果需要开关之外的值,可以在
1 2 3 4 5 6 | read language case"$language" in ([cC]) lingua='zh_tw'; echo 'Chinese';; ([fF]) lingua='fr_fr'; echo 'French';; (*) echo 'Unrecognized language abbreviation';; esac |
现代版本的tr支持posix字符类[:upper:]和[:lower:]
1 | tr -s '[:upper:]' '[:lower:]' < inputfile > outputfile |
所有字符类都在这里:
1 | http://ss64.com/bash/tr.html |