首页 > 代码库 > grep -n 查看字符串不为空失败

grep -n 查看字符串不为空失败

测试目的:

目的:测试/bin/grep  -q   "root" menu.lst的用途

结果:文本中是否包含root文字内容,如果是,返回0,如果不是,返回非零

#!/bin/bash

/bin/grep  -q   "root" menu.lst

rc=$?

if [ $rc -ne "0" ] ;then

        echo "bad"

else

        echo "good"

fi

~      


目的:测试[ ](中间有一个空格)在grep中的用法 

/sbin/ethtool eth0 |grep "Speed:[ ]1000Mb"




#!/bin/bash

#test script pass parmeter

echo $parmeter

if [ ! -z $parmeter ]; then

#如果parmeter不为空,打印full,说明有赋值

        echo "full"

else

#如果parmeter为空,打印empty,说明没有任何的参数传递

        echo "empty"

fi


执行如下:

[root@localhost /]# parmeter=author ./test.sh 

author

full

[root@localhost /]# ./test.sh 


empty



但是使用-n 替换掉 不管如何执行都打印full

-n 参数说明参数不为空