我写了一个脚本来获取顶级会话PID,即会话启动程序,它可以是bash,dash,ksh甚至systemd之类的shell。该脚本可能将PID作为初始参数,但是我需要对其进行过滤以检查其是否为有效整数,而不是34fg45
,-5467
之类的东西,并且我不希望它以零开头,例如05467
。
这是脚本的片段。
if [ "$1" != "" ]; then
if [[ "$1" == [1-9]*([0-9]) ]]; then <- Check for Integer; error here in non bash shell
if ps -p $1 -o "pid=" >/dev/null 2>&1; then
pid=$1
else
echo "PID $1, no such process." >&2
exit 1
fi
else
echo "Invalid pid." >&2
exit 1
fi
else
pid=$$
fi
该代码以bash运行,但由于语法错误而不能在破折号上运行:
./tspid: 16: ./tspid: Syntax error: "(" unexpected (expecting "then")
据我了解
if [[ "$1" =~ ^[0-9][1-9]*$ ]];
使用=~
进行正则表达式匹配,并且
if [[ "$1" == [1-9]*([0-9]) ]];
使用==
进行模式匹配
答案 0 :(得分:6)
使用case conditional construct。每个POSIX外壳都有它,与双括号不同,它看起来并不可怕。
# make sure 0-9 is literally 0 to 9
LC_COLLATE=C
# assume set -u is not in effect or $1 is set
case $1 in
('')
# handle empty argument
;;
(0*|*[!0-9]*)
# handle invalid PID (0, 042, 42a, etc.)
;;
(*)
# handle valid PID
;;
esac
# restore LC_COLLATE if necessary