我有这个巴什 - 如何检查是否字符串包含不止一个特殊cahracter
if [[ ! $newstring == *['!'@#\$%^\&*()_+]* ]]
then
echo Error - Does not contain One Special Character - $newstring
i=$((i+1))
fi
,检查如果字符串只有从银行一个单个字符,我要检查,如果有一个以上的?
什么是最好的方法?
我有这个巴什 - 如何检查是否字符串包含不止一个特殊cahracter
if [[ ! $newstring == *['!'@#\$%^\&*()_+]* ]]
then
echo Error - Does not contain One Special Character - $newstring
i=$((i+1))
fi
,检查如果字符串只有从银行一个单个字符,我要检查,如果有一个以上的?
什么是最好的方法?
要么添加一个第二类
if [[ "$newstring" != *['!'@#\$%^\&*\(\)_+]*['!'@#\$%^\&*\(\)_+]* ]]
或条别的出来,并检查长度
t="${newstring//[^[email protected]#\$%^\&*()_+]}"
if [ ${#t} -lt 2 ]
你写的任何理由'[[!! x == y]]'vs'[[x!= y]]'? – 2014-10-31 16:25:58
@glennjackman复制OPs风格,没有很好的理由。 – 2014-10-31 16:27:08
#!/bin/bash
a='!*@%6789';
if [[ `echo $a | sed "s/\(.\)/\1\n/g"|grep -c "[[:punct:]]"` -gt 1 ]]; then echo shenzi; else echo koba; fi
这需要至少一个子shell和两个外部命令。 – 2014-10-31 16:28:44
我同意。感谢您指出Etan。 – 2014-10-31 16:38:40
grep
可以是有用的,以提供匹配
grep -oP "^[^'\!'@#\$%^\&*()_+]*['\!'@#\$%^\&*()_+][^'\!'@#\$%^\&*()_+]+$"
测试
$ echo "#asdfasdf234" | grep -oP "^[^'\!'@#\$%^\&*()_+]*['\!'@#\$%^\&*()_+][^'\!'@#\$%^\&*()_+]+$"
将匹配字符串作为
#asdfasdf234
$ echo "#asdf#asdf234" | grep -oP "^[^'\!'@#\$%^\&*()_+]*['\!'@#\$%^\&*()_+][^'\!'@#\$%^\&*()_+]+$"
不会匹配字符串
的if
结构可以
echo $newstring| grep -oP "^[^'\!'@#\$%^\&*()_+]*['\!'@#\$%^\&*()_+][^'\!'@#\$%^\&*()_+]+$"
if [[ $? -eq 0 ]] > /dev/null
then
echo Error - Does not contain One Special Character - $newstring
i=$((i+1))
fi
这里正则表达式
^[^'\!'@#\$%^\&*()_+]*['\!'@#\$%^\&*()_+][^'\!'@#\$%^\&*()_+]+$
匹配所有字符串与特定字符的确切出现次数
您是否可以使用[[:punct:]]? – 2014-10-31 16:37:35
@ArunSangal是的,我可以有。刚刚从OPs问题中复制而来。 – nu11p01n73R 2014-10-31 16:43:12
我们可以使用tr
来解决它。
$ string='[email protected][email protected]#*&%)([email protected]#@#'
$ number=$(($(tr -d '[[:alnum:]]' <<< "$string"|wc -m) - 1))
$ echo "We have $number of special characters"
$ 16
这应该是短而快。
停止编写自己的密码复杂度检查并让人们使用密码短语。 – 2014-10-31 16:20:10
我不认为检查“one and only one”的测试......末尾的'*'可能包含任何数量的附加“特殊”字符......所以它只检查“至少一个”.. – twalberg 2014-10-31 18:05:22