您现在的位置是:网站首页> 编程资料编程资料
Shell脚本中判断输入变量或者参数是否为空的方法_linux shell_
2023-05-26
294人已围观
简介 Shell脚本中判断输入变量或者参数是否为空的方法_linux shell_
先给大家分享一篇关于shell判断一个变量是否为空方法总结内容
shell判断一个变量是否为空方法总结
https://www.jb51.net/article/154835.htm
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
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
5. 使用""判断
复制代码 代码如下:
#!/bin/sh
dmin=
if [ "$dmin" = "" ]
then
echo "dmin is not set!"
else
echo "dmin 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 [ $1 -eq 2 ];then
./app12;./app123
elif [ $1 -eq 90 ];then
echo "yy";
fi
您可能感兴趣的文章:
相关内容
- Shell脚本中判断输入参数个数的方法_linux shell_
- Shell最多支持多少个参数?_linux shell_
- Python执行Linux系统命令的4种方法_linux shell_
- bash: /usr/bin/autocrorder: /usr/bin/python^M: bad interpreter: No such file or directory_linux shell_
- 写出健壮Bash Shell脚本的一些技巧总结_linux shell_
- Linux下统计当前文件夹下的文件个数、目录个数_linux shell_
- 让Linux下的cron以秒为单位执行shell脚本的3种方法_linux shell_
- Shell脚本实现自动检测修改最快的Ubuntu软件源_linux shell_
- Shell实现判断进程是否存在并重新启动脚本分享_linux shell_
- linux shell 自定义函数方法(定义、返回值、变量作用域)_linux shell_
