如何在 Bash 中将大写字母转换为小写字母或相反?

发布于 2025-01-07 03:18:58 字数 559 浏览 1 评论 0原文

可能的重复:
在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

画中仙 2025-01-14 03:18:58

您可以使用 tr 将字符切换为小写/大写:

echo $language | tr '[A-Z]' '[a-z]'

You can use tr to switch the chars to lowercase/uppercase:

echo $language | tr '[A-Z]' '[a-z]'
凉薄对峙 2025-01-14 03:18:58

您可以使用以下大小写修饰符(来自 man bash):

${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

因此您的代码可能如下所示:

# 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

You can use the following case-modifiers (from man bash):

${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

So your code might look something like this:

# 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
草莓酥 2025-01-14 03:18:58

现代版本的 tr 支持 POSIX 字符类 [:upper:] 和 [:lower:]

tr -s '[:upper:]'  '[:lower:]' < inputfile > outputfile

所有字符类都在这里:

http://ss64.com/bash/tr.html

Modern versions of tr have support for POSIX character classes [:upper:] and [:lower:]

tr -s '[:upper:]'  '[:lower:]' < inputfile > outputfile

All of the character classes are here:

http://ss64.com/bash/tr.html
救赎№ 2025-01-14 03:18:58

我可能会使用 case 语句,尽管还有其他方法可以做到这一点:

read language
case "$language" in
([cC]) echo 'Chinese';;
([fF]) echo 'French';;
(*)    echo 'Unrecognized language abbreviation';;
esac

如果您需要开关外部的值,您可以在 case 中进行规范分配:

read language
case "$language" in
([cC]) lingua='zh_tw'; echo 'Chinese';;
([fF]) lingua='fr_fr'; echo 'French';;
(*)    echo 'Unrecognized language abbreviation';;
esac

I'd probably use a case statement, though there are other ways to do it:

read language
case "$language" in
([cC]) echo 'Chinese';;
([fF]) echo 'French';;
(*)    echo 'Unrecognized language abbreviation';;
esac

You could make a canonical assignment in the case if you need the values outside the switch:

read language
case "$language" in
([cC]) lingua='zh_tw'; echo 'Chinese';;
([fF]) lingua='fr_fr'; echo 'French';;
(*)    echo 'Unrecognized language abbreviation';;
esac
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文