如何在 Bash Shell 中判断变量是否为空
我的 shell 脚本依赖于用户输入。如何在 Linux / Apple OS X / Unix 类操作系统下找出名为 $_JAIL 路径的变量是否为空?你能告诉我在 bash shell 上查找变量是否为空的命令吗?
您可以将-z选项传递给 if 命令或条件表达式。如果 STRING 的长度为零,则变量 ($var) 为空。test 命令用于检查文件类型和比较值。本页介绍如何使用 test 命令确定 bash shell 变量是否为空。
教程详细信息 | |
---|---|
难度等级 | 简单的 |
Root 权限 | 不 |
要求 | Linux 终端 |
类别 | Linux shell 脚本 |
操作系统兼容性 | BSD • Linux • macOS • Unix • WSL |
预计阅读时间 | 4 分钟 |
图 01:Bash 脚本使用各种方法判断变量是否为空
要确定 bash 变量是否为空:
- 如果 bash 变量未设置或设置为空字符串,则返回 true:if [ -z "$var" ];
- 另一种选择:[ -z "$var" ] && echo "Empty"
- 确定 bash 变量是否为空:[[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"
Bash Shell 判断变量是否为空
让我们详细了解语法和示例。 if 命令的语法如下:
if [ -z "$var" ] then echo "\$var is empty" else echo "\$var is NOT empty" fi
或者
if test -z "$var" then echo "\$var is empty" else echo "\$var is NOT empty" fi
检查 bash shell 变量是否为空的另一种方法
您还可以尝试控制运算符。语法为:
[ -z "$var" ] && echo "Empty" [ -z "$var" ] && echo "Empty" || echo "Not empty"
或者
[[ -z "$var" ]] && echo "Empty" [[ -z "$var" ]] && echo "Empty" || echo "Not empty"
或者
## Check if $var is set using ! i.e. check if expr is false ## [ ! -z "$var" ] || echo "Empty" [ ! -z "$var" ] && echo "Not empty" || echo "Empty" [[ ! -z "$var" ]] || echo "Empty" [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"
例如:
## taken from gen_html_to_pdf() ## IFS='|' set -- $line local pdfid="$1" local pdfurl="$2" local pdftitle="$3" local pdf="${output}/${pdfid}.pdf" [[ -z "$pdfurl" ]] && { echo "Error: URL not found in $_db"; exit 1; } echo "Creating $pdf ..." # rest of code ...
Sh/Bash 单行示例,用于确定 bash 变量是否为空
键入以下命令
## set _JAIL _JAIL="/path/to/apache" ## find out if it is empty or not ## [ -z "$_JAIL" ] && echo "Empty: Yes" || echo "Empty: No"
另一个例子:
_JAIL="" [ -z "$_JAIL" ] && echo "Empty: Yes" || echo "Empty: No"
另一个例子:
## unset (remove) $_JAIL ## unset _JAIL [ -z "$_JAIL" ] && echo "Empty: Yes" || echo "Empty: No"
if 命令语法和示例
#!/bin/sh _JAIL="$1" if [ -z "$_JAIL" ] then echo "Please set \$_JAIL" else echo "Setting up jail at $_JAIL" //call setjail() // setjail fi
关于双括号语法的说明
如果您不关心便携性,请尝试:
## set _JAIL _JAIL="/path/to/apache" ## find out if it is empty or not ## [[ -z "$_JAIL" ]] && echo "Empty" || echo "Not empty"</kbd>
或者
#!/bin/bash ################################### ### Warning: Portability issue. ### ################################### _JAIL="$1" ## note double bracket syntax: if [[ -z "$_JAIL" ]] then echo "Please set \$_JAIL" else echo "Setting up jail at $_JAIL" //call setjail() // setjail fi
示例:确定 Bash 变量是否为空
最后,这里有一个脚本来演示基于 bash/sh/posix 的 shell 的各种可能性:
#!/bin/bash JAIL="/nginx" HINT="" # Do three possibilities for $JAIL ## for i in 1 2 3 do case $i in 1) JAIL="/nginx/jail"; HINT="value set";; 2) JAIL=""; HINT="value set to empty string";; 3) unset JAIL; HINT="\$JAIL unset";; esac ############################################### # Update user with actual values and action # $JAIL set to a non-empty string (1) # $JAIL set to the empty string (2) # $JAIL can be unset (3) ################################################ echo "*** Current value of \$JAIL is '$JAIL' ($HINT) ***" ## Determine if a bash variable is empty or not ## if [ -z "${JAIL}" ]; then echo "JAIL is unset or set to the empty string" fi if [ -z "${JAIL+set}" ]; then echo "JAIL is unset" fi if [ -z "${JAIL-unset}" ]; then echo "JAIL is set to the empty string" fi if [ -n "${JAIL}" ]; then echo "JAIL is set to a non-empty string" fi if [ -n "${JAIL+set}" ]; then echo "JAIL is set, possibly to the empty string" fi if [ -n "${JAIL-unset}" ]; then echo "JAIL is either unset or set to a non-empty string" fi done
Bash 版本 3.2.51 的示例输出:
图 01:测试变量的各种可能性
结论
您刚刚学习了如何使用各种方法确定 bash 变量是否为空。使用以下语法,在 Linux 或类 Unix 系统下的 Bash 中很容易找到变量是否为空:
## syntax 1 ## if [[ -z "$variable" ]]; then echo "Empty $variable" else echo "Do whatever you want as \$variable is not empty" fi ## Syntax 2 ## [[ -z "$variable" ]] && echo "Empty" || echo "Not empty" ## Syntax 3 ## [ -z "$var" ] && echo "Empty: Yes" || echo "Empty: No"
我建议您阅读以下资源以获取更多信息或查看此处的GNU/bash 手册页:
- Bash Shell:查明变量是否已设置
- 帮助 [
- 帮助 [[