BASH -gt一元运算符预期错误

时间:2015-10-25 00:22:04

标签: macos bash shell osx-yosemite

我已在先前版本的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

最受赞赏的想法或建议!

1 个答案:

答案 0 :(得分:1)

df的输出几乎肯定已经改变,导致脚本不正确。看起来第6列(意图是mount point)已成为其中一个指标(blocksavailableused等等。

您需要运行df -l来检查第6列是否仍然是您需要的第6列。如果没有,请修复脚本或找到在“旧版”模式下运行df的方法,以便它恢复为旧格式。