bash 在mac上的bash中大写到小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2505771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
uppercase to lowercase in bash on a mac
提问by Mala
I'm writing a bash script which needs to convert a string to lowercase. The problem is I'm doing it on a mac so 'tr' is not available. How can I go about doing this on a mac?
我正在编写一个需要将字符串转换为小写的 bash 脚本。问题是我是在 mac 上做的,所以 'tr' 不可用。我该如何在 Mac 上执行此操作?
The problem I'm trying to tackle is that my script needs to recognize an if an extension is a .gif or a .jpg - and I don't want to have to check for .jpeg, .jPeg, .JPEG, .JPeg, etc etc etc... if there's a smarter way of doing this than just converting to lowercase and testing for gif, jpg and jpeg, i'm all ears :)
我试图解决的问题是我的脚本需要识别扩展名是 .gif 还是 .jpg - 我不想检查 .jpeg、.jPeg、.JPEG、.JPeg ,等等等等......如果有更聪明的方法来做到这一点,而不仅仅是转换为小写并测试 gif、jpg 和 jpeg,我都听得一清二楚:)
UPDATE:
I am an idiot.The reason this mac "doesn't have" these basic text-conversion programs is because I overwrote PATH with "hello" when doing some testing >_<
更新:
我是个白痴。这个mac“没有”这些基本的文本转换程序的原因是因为我在做一些测试时用“hello”覆盖了PATH>_<
采纳答案by ghostdog74
in bash, you can use nocaseglob
在 bash 中,您可以使用 nocaseglob
shopt -s nocaseglob
for file in *.jpg *.jpeg *.gif
do
echo "$file"
done
#turn off
shopt -u nocaseglob
in general to convert cases, various ways
一般来说,转换案例,各种方式
echo "stRING" | awk '{print toupper(y="this Is A test"
echo "${y^^}"
)}'
echo "STRING" | tr "[A-Z]" "[a-z]" # upper to lower
echo "StrinNG" | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' #lower to upper
回答by nachtigall
this is built into bash:
这是内置于 bash 中的:
to convert $y into uppercase:
将 $y 转换为大写:
y="THIS IS a TeSt"
echo "${y,,}"
and to convert $y into lowercase:
并将 $y 转换为小写:
$ echo 'this IS some TEXT' | tr '[:upper:]' '[:lower:]'
this is some text
回答by crsuarezf
echo "HelLo! how ArE you?" | capitalize -u
echo "HelLo! how ArE you?" | capitalize -l
echo "HelLo! how ArE you?" | capitalize -c
回答by Chompi
In bash you can use capitalize:
在 bash 中,您可以使用大写:
##代码##
