便携式过滤掉无效的PID

时间:2020-06-21 01:59:23

标签: bash shell sh posix

我写了一个脚本来获取顶级会话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]) ]];使用==进行模式匹配

  1. 是吗?
  2. 如何将以上表达式转换为既可以在bash shell中运行,又可以在bash shell中运行?

1 个答案:

答案 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