Bash Shell找出变量是否为空

时间:2020-01-09 10:45:54  来源:igfitidea点击:

在Linux/Apple OS X/Unix之类的操作系统下,如何查找名为$_JAIL path的变量是否为空?
您能告诉我bash shell上的命令以了解变量是否为空吗?

您可以将-z选项传递给if命令或者条件表达式。
如果STRING的长度为零,则变量($var)为空。

test命令用于检查文件类型并比较值。
本教程显示如何使用test命令找出bash shell变量是否为空。

用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=""
  local pdfurl=""
  local pdftitle=""
  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=""
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=""
## 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