一句话判断

[ ! $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. MYSQL-max_binlog_cache_size参数

    max_binlog_cache_size 解释:这是设置最大二进制日志的缓存区大小的变量.若处理多语句事务时需要的内存大小比设置值大的话就会提示一个error:Multi-statement tra ...

  2. 小技巧------教你释放C盘空间

    1.删除休眠文件hiberfil.sys. 该文件在c盘根部目录为隐藏的系统文件,隐藏的这个hiberfil.sys文件大小正好和自己的物理内存是一致的,当你让电脑进入休眠状态时,windows7在关 ...

  3. 浅谈Lambda表达式详解

    lambda简介 lambda运算符:所有的lambda表达式都是用新的lambda运算符 " => ",可以叫他,“转到”或者 “成为”.运算符将表达式分为两部分,左边指定 ...

  4. make linux test main attempt to index a nil value

    Lua: getting started http://www.lua.org/start.html#learning Building from source Lua is very easy to ...

  5. 当url作为id时的删除

    API Documentation — Elasticsearch 6.3.1 documentation https://elasticsearch-py.readthedocs.io/en/mas ...

  6. Mysql EXPLAIN 相关疑问: Using temporary ; Using filesort

    一.什么是Using temporary ; Using filesort 1. using filesort filesort主要用于查询数据结果集的排序操作,首先MySQL会使用sort_buff ...

  7. PhotoSwipe中文API(三)

    http://photoswipe.com/documentation/api.html 所有的方法和这个网页上列出的属性是公开的.如果你想看看例子什么API可以做的,拿在默认PhotoSwipe U ...

  8. PAT 1053 Path of Equal Weight[比较]

    1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight W​i​​ assigned t ...

  9. python16_day40【数据结构】

    一.链表 #!/usr/bin/env python # -*-coding:utf8-*- __author__ = "willian" # 一.简单链表 class Node( ...

  10. 2.5 The Object Model -- Observers

    Ember支持监视任何属性,包括计算的属性.你可以使用Ember.observer为一个对象设置一个监视者: Person = Ember.Object.extend({ //these will b ...