首页 > 系统服务 > 详细

shell判断输入变量或者参数是否为空

时间:2015-09-17 17:46:17      阅读:254      评论:0      收藏:0      [点我收藏+]

判断变量

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/bash

if [ ! -n "$1" ] ;then

    echo "you have not input a word!"

else

    echo "the word you input is $1"

fi

以下未验证,转自http://blog.csdn.net/lllxy/article/details/3255554

2. 直接通过变量判断

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

  1. #!/bin/sh

  2. para1=

  3. if 

    $para1 

    ]; 

    then

  4.  

     

    echo 

    "IS NULL"

  5. else

  6.  

     

    echo 

    "NOT NULL"

  7. fi 

3. 使用test判断

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

  1. #!/bin/sh

  2. dmin=

  3. if 

    test 

    -z 

    "$dmin"

  4. then

  5.  

     

    echo 

    "dmin is not set!"

  6. else 

     

  7.  

     

    echo 

    "dmin is set !"

  8. fi

 

4. 使用""判断

  1. #!/bin/sh 

  2. dmin=

  3. if 

    "$dmin" 

    "" 

    ]

  4. then

  5.  

     

    echo 

    "dmin is not set!"

  6. else 

     

  7.  

     

    echo 

    "dmin is set !"

  8. fi


shell判断输入变量或者参数是否为空

原文:http://my.oschina.net/ghm7753/blog/507753

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!