一句话判断

[ ! $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 [ ! -n "$a" ]; then
echo "IS NULL"
else
echo "NOT NULL"
fi

或者

#!/bin/sh
a=
if [ ! $a ]; then
echo "IS NULL"
else
echo "NOT NULL"
fi

2.判断输入参数

#!/bin/bash
if [ ! -n "$1" ] ;then
echo "you have not input a word!"
else
echo "the word you input is $1"
fi

以下未验证。

3. 直接通过变量判断

如下所示:得到的结果为: IS NULL

#!/bin/sh
para1=
if [ ! $para1 ]; then
echo "IS NULL"
else
echo "NOT NULL"
fi

4. 使用test判断

得到的结果就是: dmin is not set!

#!/bin/sh
dmin=
if test -z "$dmin"
then
echo "dmin is not set!"
else
echo "dmin is set !"
fi

或者

#!/bin/sh
a=
if test -z "$a" then
echo "a is not set!"
else
echo "a is set !"
fi

5. 使用""判断

#!/bin/sh
dmin=
if [ "$dmin" = "" ]
then
echo "dmin is not set!"
else
echo "dmin is set !"
fi

或者

#!/bin/sh
a=
if [ "$a" = "" ]; then
echo "a is not set!"
else
echo "a is set !"
fi
 

下面是我在某项目中写的一点脚本代码, 用在系统启动时:

#! /bin/bash
echo "Input Param Is [$1]" if [ ! -n "$1" ] ;then
echo "you have not input a null word!"
./app1;./app12;./app123
elif [ $ -eq ];then
./app12;./app123
elif [ $ -eq ];then
echo "yy";
fi

shell 判断变量是否为空的更多相关文章

  1. PHP判断变量是否为空的几种方法小结

    1. isset功能:判断变量是否被初始化 说明:它并不会判断变量是否为空,并且可以用来判断数组中元素是否被定义过注意:当使用isset来判断数组元素是否被初始化过时,它的效率比array_key_e ...

  2. js 判断变量是否为空或未定义

    判断变量是否定义: if(typeof(hao) == "undefined"){ //未定义 }else{ //定义 } 判断变量是否为空或NULL,是则返回'', 反之返回原对 ...

  3. null、 is_null() 、empty() 、isset() PHP 判断变量是否为空

    PHP中,在判断变量是否为空的时候,总会纠结应该选用哪个函数,下面列取常用的多种情况,其中1/3经过我的验证,其它来自网络,验证后使用... 使用 PHP 函数对变量 $x 进行比较 表达式 gett ...

  4. js 判断变量是否为空

    js 判断变量是否为空 欢迎指正,补充! /** * 判断变量是否为空, * @param {[type]} param 变量 * @return {Boolean} 为空返回true,否则返回fal ...

  5. 关于Javascript判断变量是否为空

    如何判断Javascript对象是否存在 原文网址:http://www.ruanyifeng.com/blog/2011/05/how_to_judge_the_existence_of_a_glo ...

  6. javascript判断变量是否为空的方法

    javascript判断字符串变量是否为空的方法代码如下<pre> if (typeof(ndesc)=="undefined" || ndesc=='' || nde ...

  7. linux bash shell 判断目录是否为空的函数

    #!/bin/sh ##方法一 判断输出字符数统计为0 is_empty_dir(){ |wc -w` } ##方法二 判断输出string为空 #is_empty_dir(){ # ` ] #} t ...

  8. php中判断变量是否为空

    从数据库中取出值后判断是否为空,这个看起来很简单,只要和null比较一下就可以了,其实不然, if($obj==null){ } 这样写会报错的:Notice: Trying to get prope ...

  9. IF()判断变量是否为空

    一 变量是字符串,判断为空 第一种:strs == null string strs = "test"; if (strs == null) { //这里是为空的字符串,返回你指定 ...

随机推荐

  1. This function has none of DETERMINISTIC, NO SQL

    错误信息: [Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declara ...

  2. [Android Tips] 30.如何在 Android Studio 中一次性格式化所有代码

    在目录上面右击,有 Reformat Code Ctrl + Alt + L 参考 如何在IntelliJ IDEA或Android Studio中一次性格式化所有代码?

  3. 170607、SQL Select语句完整的执行顺序

    SQL Select语句完整的执行顺序: 1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函 ...

  4. Oracle下where子句

    课外题 要求:删除某一个用户,同时保留该用户的数据?如何解决 alter user scott account lock :改天需要使用则解锁unlock 锁定用户使用sysdba登录还是可以查看数据 ...

  5. This module embeds Lua, via LuaJIT 2.0/2.1, into Nginx and by leveraging Nginx's subrequests, allows the integration of the powerful Lua threads (Lua coroutines) into the Nginx event model.

    openresty/lua-nginx-module: Embed the Power of Lua into NGINX HTTP servers https://github.com/openre ...

  6. 洛谷P4428二进制 [BJOI2018] 线段树

    正解:线段树 解题报告: 传送门! 话说开始看到这题的时候我想得hin简单 因为关于%3有个性质就是说一个数的各个位数之和%3=这个数%3嘛,小学基础知识? 我就想着,就直接建一棵树,只是这棵树要用个 ...

  7. MVC模式:python案例

    quotes = ('A man is not complete until he is married. Then he is finished.', 'As I said before, I ne ...

  8. ftp文件上传和下载

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  9. (21)纹理缓存(Texture Cache)

    简介 纹理缓存是将纹理缓存起来方便之后的绘制工作.每一个缓存的图像的大小,颜色和区域范围都是可以被修改的.这些信息都是存储在内存中的,不用在每一次绘制的时候都发送给GPU. CCTextureCach ...

  10. 解决 failed to push some refs to 'git@github.com:zle1992/head-first-java' hint: Updates were rejected because the tip of your curr

    问题描述: 寒假之前用实验室电脑push到github 上head first java 的程序,寒假回家后,想用自己的笔记本继续编,继续push . 我先从github下载zip到本地,然后 解压后 ...