Bash脚本检查字符串是否为大写字母 [英] Bash script check string for uppercase letter

查看:128
本文介绍了Bash脚本检查字符串是否为大写字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查任何大写字母的字符串.我的代码没有显示任何输入的上限,可能是"sss","Sss","SSS"

I am trying to check a string for any Uppercase letter. my code shows NO UPPER for any input, may it be "sss", "Sss", "SSS"

if [[ "$pass" =~ [^a-zA-Z0-9] ]]
then
   echo "Upper found"
else
   echo "no upper"
fi

推荐答案

[^ a-zA-Z0-9] 表示除 az 以外的任何内容,即小写字母, AZ (即大写字母)和 0-9 (即数字). sss Sss SSS 都只包含字母,因此无法匹配.

[^a-zA-Z0-9] means anything except for a-z, i.e. lowercase letters, A-Z, i.e. uppercase letters, and 0-9, i.e. digits. sss, Sss, SSS all contain just letters, so they can't match.

[[ $password =~ [A-Z] ]]

如果密码包含任何大写字母,则

为true.

is true if the password contains any uppercase letter.

例如,在运行这种测试之前,您应该设置 LC_ALL

You should set LC_ALLbefore running this kind of tests, as for example

$ LC_ALL=cs_CZ.UTF-8 bash -c '[[ č =~ [A-Z] ]] && echo match'
match
$ LC_ALL=C           bash -c '[[ č =~ [A-Z] ]] && echo match'
# exit-code: 1

[[[:upper:]] 应该总是可以工作.

这篇关于Bash脚本检查字符串是否为大写字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆