BASH -gt一元操作符预期错误



我在以前版本的OSX上成功地使用了以下脚本,当磁盘卷达到定义的阈值时向我发出警报。

在Yosemite上运行时,它不工作:

% bash disk-full-alert.sh 
disk: 10766513
df: 10766513: No such file or directory
output:
current:
threshold: 65
_________________
disk-full-alert.sh: line 26: [: -gt: unary operator expected
disk: 129154082
df: 129154082: No such file or directory
output:
current:
threshold: 65
_________________
disk-full-alert.sh: line 26: [: -gt: unary operator expected
disk: 710743471
df: 710743471: No such file or directory
output:
current:
threshold: 65
_________________
disk-full-alert.sh: line 26: [: -gt: unary operator expected

脚本:

#!/bin/bash
PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH
HOSTNAME='server.example.com'
ERECIP='dan@example.com'
# Threshold is percentage full
THRESHOLD=65

get_data() {
  df -l | grep -v Mounted| awk ' { print $6 } '
}
get_data | while IFS= read -r disk
do
echo disk: $disk
OUTPUT=($(LC_ALL=C df -P ${disk}))
echo output: $OUTPUT
CURRENT=$(echo ${OUTPUT[11]} | sed 's/%//')
echo current:  $CURRENT
echo threshold: $THRESHOLD
echo _________________
echo " "
[ $CURRENT -gt $THRESHOLD ] &&
 (
echo "From: Server Admin <admin@$HOSTNAME>"
echo "To: $ERECIP"
echo "Subject: Warning!! $disk file system is $CURRENT% full on $HOSTNAME"
echo ""
date
echo ""
echo "ALERT!!"
echo " "
echo The $disk file system is $CURRENT% full
echo
) | /usr/sbin/sendmail -it

想法或建议最受欢迎!

几乎可以肯定的是,df的输出发生了变化,导致脚本不正确。看起来,打算表示mount point的第6列,不知何故已成为指标之一(blocks, available, used等)。

您需要运行df -l来检查第6列是否仍然是您需要的正确的列。如果没有,修复脚本或找到一种在"legacy"模式下运行df的方法,使其恢复到旧格式。

最新更新