当前位置: 技术问答>linux和unix
请教:linux shell 关于字符串匹配的问题
来源: 互联网 发布时间:2016-03-30
本文导语: 代码: str='aaabbbreplicationStateready' echo $str if [ `echo $str | grep 'replication'` ] then echo "yes" else echo "no" fi 执行: # ./test.sh aaabbbreplicationStateready yes 没有问题 如果我将str=...改为str='aaabbb test-replicationStaterea...
代码:
str='aaabbbreplicationStateready'
echo $str
if [ `echo $str | grep 'replication'` ]
then
echo "yes"
else
echo "no"
fi
执行:
# ./test.sh
aaabbbreplicationStateready
yes
没有问题
如果我将str=...改为str='aaabbb test-replicationStateready=ready'
执行:
# ./test.sh
aaabbb test-replicationStateready=ready
./test.sh: line 3: [: aaabbb: unary operator expected
no
请教高手这怎么解决,Thanks!
str='aaabbbreplicationStateready'
echo $str
if [ `echo $str | grep 'replication'` ]
then
echo "yes"
else
echo "no"
fi
执行:
# ./test.sh
aaabbbreplicationStateready
yes
没有问题
如果我将str=...改为str='aaabbb test-replicationStateready=ready'
执行:
# ./test.sh
aaabbb test-replicationStateready=ready
./test.sh: line 3: [: aaabbb: unary operator expected
no
请教高手这怎么解决,Thanks!
|
grep返回的状态是其执行状态,不是结果,需要用grep -c的输出进行判断。
c=`echo $str ¦ grep 'replication'`
if [ $c -gt 0 ]
then
。。。
c=`echo $str ¦ grep 'replication'`
if [ $c -gt 0 ]
then
。。。