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 "包含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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM