手机
当前位置:查字典教程网 >脚本专栏 >linuxshell >Shell脚本中判断输入变量或者参数是否为空的方法
Shell脚本中判断输入变量或者参数是否为空的方法
摘要:1.判断变量复制代码代码如下:read-p"inputaword:"wordif[!-n"$word"];thenecho"youhaven...

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脚本中判断输入变量或者参数是否为空的方法】相关文章:

shell字符串比较判断是否为数字

Shell脚本获取进程的运行时间

用shell脚本在mysql表中批量插入数据的方法

shell脚本中一些特殊符号

shell判断文件,目录是否存在或者具有权限的代码

shell脚本命令行参数简介

Shell脚本数组用法小结

Linux shell脚本中字符串连接的方法

Shell脚本定义变量和重新赋值

Shell脚本中实现把输入的密码转换为*(星号)的方法

精品推荐
分类导航