背景
– shell脚本中需要读取到以下类似命令的参数并运用
simplebench.sh -year 20221020 -month 10
- vim simplebench.sh
#!/bin/sh
until [ $# -eq 0 ]
do
name=${1:1}; shift;
# 将参数结果直接赋值给参数的变量
if [[ -z "$1" || $1 == -* ]] ; then eval "export $name=true"; else eval "export $name=$1"; shift; fi
done
echo "year=$year"
echo "month=$month"
- 执行结果如下
[root@zdsg1024 ~]# ./simplebench.sh -year 20221020 -month 10
year=20221020
month=10