如何在 Bash 中将大写字母转换为小写字母或相反?
可能的重复:
在 bash shell 脚本中将字符串转换为小写 < /p>
对于例如:
echo *****Language translator*****
echo please choose the language
for Chinese enter c
for French enter f
以一种简单的方式,我希望能够识别中文的 C 和 c; f 和 F 也是如此,被识别为法语。
有没有办法将所有内容都转换为小写?
这里是部分代码:
if [ $language == c ];
那么
echo“输入要翻译的单词:”
读取单词到翻译
Possible Duplicate:
converting string to lower case in bash shell scripting
For example:
echo *****Language translator*****
echo please choose the language
for Chinese enter c
for French enter f
In a simple way I want to be able to recognize both C and c for Chinese;
and the same thing for f and F, recognized as French.
Is there a way to convert everything to lower case?
Here part of the code:
if [ $language == c ];
then
echo "Enter the word to translate:"
read word_to_translate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
tr
将字符切换为小写/大写:You can use
tr
to switch the chars to lowercase/uppercase:您可以使用以下大小写修饰符(来自
man bash
):因此您的代码可能如下所示:
You can use the following case-modifiers (from
man bash
):So your code might look something like this:
现代版本的 tr 支持 POSIX 字符类 [:upper:] 和 [:lower:]
所有字符类都在这里:
Modern versions of tr have support for POSIX character classes [:upper:] and [:lower:]
All of the character classes are here:
我可能会使用 case 语句,尽管还有其他方法可以做到这一点:
如果您需要开关外部的值,您可以在
case
中进行规范分配:I'd probably use a case statement, though there are other ways to do it:
You could make a canonical assignment in the
case
if you need the values outside the switch: