我在脚本中运行了这个条件:
if [[ $(df -h /data --output=pcent | tail -1 | grep -o -E [0-9]+) > 60 ]]; then echo "Not enough space on box"; else echo "Enough space on box"; fi检查应该是打开磁盘空闲输出,验证挂载/data是否有超过40%的空闲,并打印出检查结果。除了下面列出的案例外,这对所有的情况都有效。我认为问题在于grep (GNU 2.20)的使用或与60的比较,但考虑到它对(某些)单数条目和所有两位数都有效,我不明白它是如何失败的。
在CentOS 7框中运行以下操作:
if [[ $(df -h /data --output=pcent | tail -1 | grep -o -E [0-9]+) > 60 ]]; then echo "Not enough space on box"; else echo "Enough space on box"; fi( df -h /data --output=pcent的输出是一个数字和%,即( "X%")当运行测试时,例如
if [[ $(echo 100% | tail -1 | grep -o -E [0-9]+) > 60 ]]; then echo "Not enough space on box"; else echo "Enough space on box"; fi
if [[ $(echo 100% | tail -1 | grep -o -E [0-9]*) > 60 ]]; then echo "Not enough space on box"; else echo "Enough space on box"; fi预期的输出是“框上的空间不够”,但是它是“框上的足够空间”,并且正在运行:
if [[ $(echo 7% | tail -1 | grep -o -E [0-9]+) > 60 ]]; then echo "Not enough space on box"; else echo "Enough space on box"; fi预期的输出是“框上的足够空间”,而输出是“框上的空间不够”。
发布于 2022-11-29 19:30:13
它在做字符串比较。7大于6。用((...))代替[[...]]进行数学评价。
$: if [[ $(echo 7% | tail -1 | grep -o -E [0-9]+) > 60 ]]; then echo "Not enough space on box"; else echo "Enough space on box"; fi
Not enough space on box
$: if (( $(echo 7% | tail -1 | grep -o -E [0-9]+) > 60 )); then echo "Not enough space on box"; else echo "Enough space on box"; fi
Enough space on box你也可以这样做-
$: not=( '' "Not " )
$: echo "${not[$(echo 7% | tail -1 | grep -o -E [0-9]+) > 60]}enough space on box"
enough space on box
$: echo "${not[$(echo 70% | tail -1 | grep -o -E [0-9]+) > 60]}enough space on box"
Not enough space on boxhttps://stackoverflow.com/questions/74618073
复制相似问题