【发布时间】:2012-12-21 10:13:31
【问题描述】:
我的脚本使用的参数是我的两倍:
第一次使用以下代码,以确保没有我不想要的参数(我只考虑 arg1:arg2:arg3)
PARAM=$@
while [ "$#" -gt "0" ]; do
case $1 in
arg1)
shift2
;;
arg2)
shift
;;
arg3)
shift
;;
*)
echo "Usage : ./test.sh arg1 <VAL1> <VAL2> [arg2] [arg3]"
exit 2
;;
esac
shift
done
我想重新解析这些参数,并在我得到 arg1 时能够得到以下两个参数,所以我从这样的东西开始:
for command in $PARAM
do
case $command in
arg1)
shift
VALUE1=$command
shift
VALUE2=$command
exec_arg1
;;
esac
shift
但在使用“shift”时,我收到错误 shift: can't shift that many
shebang 是“#!/bin/sh”,我正在寻找无需使用 bash 的 shebang(即“#!/bin/bash”)的解决方案
【问题讨论】:
-
@ZackDibe “混合参数位置”到底是什么意思?
-
什么是
shift2?缺少空间?此外,shift更改$@不是$command,所以不要分配$command而是$1。