如何在bash中将检查多个值的链式if语句转换为case语句



我有一个if-elif-else-fi语句,我想在bash脚本中转换为case语句。这是代码:

if [[ -n $1 ]] && [[ -n $2 ]] && [[ -n $3 ]]; then
# sudoed audit or fsr w/ IP: set user, user home directory, IP.
CURR_USER="$1";
CURR_HOME="$2";
ADDR="$3";
elif [[ -n $1 ]] && [[ -n $2 ]] && [[ -z $3 ]]; then
# sudoed audit or fsr w/o IP: set user, user home directory.
CURR_USER="$1";
CURR_HOME="$2";
ADDR='';
elif [[ -n $1 ]] && [[ -z $2 ]] && [[ -z $3 ]]; then
# root w/ IP: set audit user, audit home directory, IP.
CURR_USER='audit';
CURR_HOME='/home/audit';
ADDR="$1";
else
# root w/o IP: set audit user, audit home directory.
CURR_USER='audit';
CURR_HOME='/home/audit';
ADDR="";
fi

我尝试了以下操作,但不起作用

case $# in
[[ -n $1 ]] && [[ -n $2 ]] && [[ -n $3 ]] )
# sudoed audit or fsr w/ IP: set user, user home directory, IP.
CURR_USER="$1";
CURR_HOME="$2";
ADDR="$3";;
[[ -n $1 ]] && [[ -n $2 ]] && [[ -z $3 ]] )
# sudoed audit or fsr w/o IP: set user, user home directory.
CURR_USER="$1";
CURR_HOME="$2";
ADDR='';;
[[ -n $1 ]] && [[ -z $2 ]] && [[ -z $3 ]] ) 
# root w/ IP: set audit user, audit home directory, IP.
CURR_USER='audit';
CURR_HOME='/home/audit';
ADDR="$1";;
*                                         )
# root w/o IP: set audit user, audit home directory.
CURR_USER='audit';
CURR_HOME='/home/audit';
ADDR="";;
esac
fi

我之所以需要一份案例陈述,是因为我的老板认为案例陈述比如果elif-else陈述更容易理解

使用$#可以看到参数的数量。当您使用""作为参数或有3个以上的参数时,下一个case的工作方式会有所不同。我认为这表明了你老板对case ... esac的看法
Charles的解决方案更好,你的老板可能也会告诉你避免重复行。

case $# in
3)
# sudoed audit or fsr w/ IP: set user, user home directory, IP.
CURR_USER="$1"
CURR_HOME="$2"
ADDR="$3";;
2)
# sudoed audit or fsr w/o IP: set user, user home directory.
CURR_USER="$1"
CURR_HOME="$2"
ADDR='';;
1)
# root w/ IP: set audit user, audit home directory, IP.
CURR_USER='audit'
CURR_HOME='/home/audit'
ADDR="$1";;
*)
# root w/o IP: set audit user, audit home directory.
CURR_USER='audit'
CURR_HOME='/home/audit'
ADDR="";;
esac

或使用默认

CURR_USER='audit'
CURR_HOME='/home/audit'
ADDR=""
case $# in
3)
# sudoed audit or fsr w/ IP: set user, user home directory, IP.
CURR_USER="$1"
CURR_HOME="$2"
ADDR="$3";;
2)
# sudoed audit or fsr w/o IP: set user, user home directory.
CURR_USER="$1"
CURR_HOME="$2";;
1)
# root w/ IP: set audit user, audit home directory, IP.
ADDR="$1";;
*)
# root w/o IP: set audit user, audit home directory.
;;
esac
简而言之,这是一个非常糟糕的主意。然而,如果你的价值观中不存在一个sigil(用下面的:进行演示(,那么可以这样做:
case $1:$2:$3 in
'::')  CURR_USER=audit; CURR_HOME=/home/audit; ADDR=;;
*::)   CURR_USER=audit; CURR_HOME=/home/audit; ADDR=$1;;
*:*:)  CURR_USER=$1;    CURR_HOME=$2;          ADDR=;;
*:*:*) CURR_USER=$1;    CURR_HOME=$2;          ADDR=$3;;
esac

然而,使用默认值参数扩展可以让几乎更清楚地获得完全相同的逻辑:

CURR_USER=${1:-audit}
CURR_HOME=${2:-/home/audit}
ADDR=$3

或者,对于完全相同的逻辑:

CURR_USER=${1:-audit}
CURR_HOME=${2:-/home/audit}
if [ -z "$2" ] && [ -z "$3" ]; then
ADDR=$1
else
ADDR=$3
fi

如果我理解正确,您的意图是有两种方法来调用您的程序:

Usage: my_program CURR_USER CURR_HOME [ADDR] (1st form)
or:  my_program [ADDR] (2nd form)

你可以写为:

if (( $# >= 2 )) ; then
# sudoed audit or fsr:
CURR_USER="$1"
CURR_HOME="$2"
ADDR="$3" # may be empty
else
# root:
CURR_USER=audit
CURR_HOME=/home/audit
ADDR="$1" # may be empty
fi

或者:

if (( $# >= 2 )) ; then
# sudoed audit or fsr:
CURR_USER="$1"
CURR_HOME="$2"
shift 2
else
# root:
CURR_USER=audit
CURR_HOME=/home/audit
fi
ADDR="$1" # may be empty

相关内容

  • 没有找到相关文章

最新更新