Pattern matching in ksh script

拟墨画扇 提交于 2019-12-25 01:35:45

问题


I have a URL parameters in a variable like

a=44&search=My World

here I want to do a pattern matching like

if [ $a =~ "search" ] ;
then
   value=1
else
   value=0

fi

But it is not working in KSH script.


回答1:


You need [[ for ksh regular expressions, not the Bourne shell [. Although in this case it hardly seems worth using an RE.

So:

if [[ $a =~ "search" ]]
then 
    value=1 
else 
    value=0
fi



回答2:


found=`echo $a | grep search`
if [ -z $found ]; then
  value=0
else
  value=1
fi


来源:https://stackoverflow.com/questions/14942920/pattern-matching-in-ksh-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!