我正在使用BASH 3.2。我可以从命令行执行以下操作:
$ build_number=23332
$ if [[ $build_number != +([0-9]) ]]
> then
> echo "Bad Build Number ($build_number). No CPU time for you!"
> else
> echo "Build Number ($build_number) is numeric"
> fi
Build Number (2332) is numeric"
如果我更改build_number to
23332a`,则返回:
Bad Build Number (23332a). No CPU time for you!
现在,我将尝试将其放入我的shell脚本中:
#! /bin/bash
...
#
# Set options
#
while getopts :hu:j:b:p: option
do
case $option in
p) promotion_name="$OPTARG";;
u) jenkins_url="$OPTARG";;
j) job_name="$OPTARG";;
b) build_number="$OPTARG"
if [[ $build_number != +([0-9]) ]]
then
error "Build Number must be numeric"
fi
;;
h) printf "\n$USAGE\n"
exit 0;;
*) error "Invalid Argument";;
esac
done
shift $(( $OPTIND - 1 ))
当我尝试执行程序时,出现以下错误:
$ ./promotion.sh -b 238d -jasdad
./promotion.sh: line 55: syntax error in conditional expression: unexpected token `('
./promotion.sh: line 55: syntax error near `+(['
./promotion.sh: line 55: ` if [[ $build_number != +([0-9]) ]]'
那么,我做错了什么?
答案 0 :(得分:2)
您需要启用扩展通配:
shopt -s extglob
答案 1 :(得分:0)
最显而易见的是,加号检查预先出现的字符是否与模式匹配,这里你没有预先设定的字符加上后面的加号