SHELL查字符串中包含字符的命令1.通配符
string='My long string'
if [[ $string == *"My long"* ]]; then
echo "It's there!"
fi
2.正则匹配
string='My long string'
if [[ $string =~ .*My.* ]]; then
echo "It's there!"
fi
3.switch…case版本的通配符(速度最快……)
string='My long string'
case "$string" in
*ERROR*)
# Do stuff
echo "包含"
;;
*ORA-*)
echo "包含"
return 1
;;
*) #若以上都不符合,则给出交互式提⽰并退出。
usage
return 0
;;
正则匹配两个字符之间的字符串esac
4.⽤grep来实现
string='My long string'
if grep -q foo <<<$string; then
echo "It's there"
fi
5.⽤字符串替换/删除来实现
string='My long string'
if [ "$string" != "${string/foo/}" ]; then
echo "It's there!"
fi
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论