一句话判断 [ ! $a ] && echo "a is null" 1.判断变量 read -p "input a word :" word if [ ! -n "$word" ] ;then echo "you have not input a word!" else echo "the word you input is $word" fi 或者 #!/bin/sh a= if [ !
从数据库中取出值后判断是否为空,这个看起来很简单,只要和null比较一下就可以了,其实不然, if($obj==null){ } 这样写会报错的:Notice: Trying to get property of non-object problem, 查了一下发现需要使用下面的写法 if (isset($obj)) { echo "This var is set set so I will print."; } 这个isset是做什么的呢? isset函数是检测变量是否设置. 格式:
ok,以后最好是每天一个shell小脚本吧,这样以后工作时还可以直接套用,嗯,比较不错,顺便还可以带给刚入门shell的朋友一些帮助,好了,废话不多说,下面是我两种判断的实现方式: 1.通过grep去筛选非数字,判断其输出状态,以下两种方式: #!/bin/bashread -p "please input a num: " num if echo $num | grep -q '[^0-9]' then echo "this is not a num,please inpu