在 case 语句中使用数字范围



我在执行案件时遇到了麻烦。每次,它都会转到最后一个情况,即 * 字符。

这是我正在使用的:

case $used_space in
    [1-84])
        echo "OK - $used_space% of disk space used."
        exit 0
        ;;
    [85])
        echo "WARNING - $used_space% of disk space used."
        exit 1
        ;;
    [86-100]*)
        echo "CRITICAL - $used_space% of disk space used."
        exit 2
        ;;
    *)
        echo "$used_space% of disk space used."
        exit 3
        ;;
esac

如何更改我的案例陈述以使用数字范围?

带 bash 和大小写:

case $used_space in
  [1-9]|[1-7][0-9]|8[0-4]) # range 1-84
    echo "OK - $used_space% of disk space used."
    exit 0
    ;;
  85)
    echo "WARNING - $used_space% of disk space used."
    exit 1
    ;;
  8[6-9]|9[0-9]|100)        # range 86-100
    echo "CRITICAL - $used_space% of disk space used."
    exit 2
    ;;
  *)
    echo "$used_space% of disk space used."
    exit 3
     ;;
esac

只需使用一系列 if-s,如下所示:

if [[ "$used_space" -le 84 && "$used_space" -ge 1 ]]
then
    echo "OK - $used_space% of disk space used."
    exit 0
elif [[ "$used_space" -eq 85 ]]
then
    echo "WARNING - $used_space% of disk space used."
    exit 1
elif [[ "$used_space" -gt 85 && "$used_space" -le 100 ]]
then
    echo "CRITICAL - $used_space% of disk space used."
    exit 2
else
    echo "$used_space% of disk space used."
    exit 3
fi

相关内容

  • 没有找到相关文章

最新更新