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 "包含ERROR..."
;;
*ORA-*)
echo "包含bbb..."
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