#!/usr/bin/env zsh
./client localhost 5000 Bryan (my command)
我想检查我的命令参数是否正确,在脚本zsh中表示:
if
first argument = int
second argument = int
third argument = string
fi
答案 0 :(得分:0)
为了清楚起见,所有这些条件必须为真,以便脚本继续,是吗?另外,请注意"字符串"几乎意味着任何字符(A-Z,0-9,标点符号) - 控制字符以外的任何字符。假设是这样的,那么这样的事情:
re='^[0-9]+$'
echo " "
echo "Parm 1: $1"
if ! [[ "$1" =~ $re ]]; then
echo "The first parameter must be an integer."
exit 1
fi
echo " "
echo "Parm 2: $2"
if ! [[ $2 =~ $re ]]; then
echo "The second parameter must be an integer."
exit 2
fi
re='^[[:graph:]]+$'
echo " "
echo "Parm 3: $3"
if ! [[ $3 =~ $re ]]; then
echo "The third parameter must be an string."
exit 3
fi
echo " "
echo "Congratulations -- your parms are valid\!"
echo " "
编辑:以下是一些示例运行:
~/test1$
~/test1$./tmp 123 456 abdc
Parm 1: 123
Parm 2: 456
Parm 3: abdc
Congratulations -- your parms are valid\!
~/test1$./tmp abc 123 456
Parm 1: abc
The first parameter must be an integer.
~/test1$./tmp 12b 123 abc
Parm 1: 12b
The first parameter must be an integer.
~/test1$./tmp 123 4h5 abc
Parm 1: 123
Parm 2: 4h5
The second parameter must be an integer.
~/test1$./tmp 123 jkl asdf
Parm 1: 123
Parm 2: jkl
The second parameter must be an integer.
~/test1$
~/test1$
希望这有帮助!