范围

2009-11-24 74 views
4

下壳股代息将检查磁盘空间和改变变量diskfull1如果使用率超过10% 最后的回声总是显示0 我试过global diskfull=1 if子句中,但它不工作。 如果消耗的磁盘超过10%,如何将变量更改为1范围

#!/bin/sh 
diskfull=0 

ALERT=10 
df -HP | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output; 
do 
    #echo $output 
    usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 ) 
    partition=$(echo $output | awk '{ print $2 }') 
    if [ $usep -ge $ALERT ]; then 
    diskfull=1 
    exit 
    fi 
done 

echo $diskfull 
+0

您确定正在执行if语句的主体吗?如果计算结果为false,diskfull = 1行永远不会执行。 – asm 2009-11-24 12:37:26

+0

感谢所有答复。我所做的是我退出了退出状态为1的if子句,然后在“完成”之后立即将其保存到diskfull变量。此解决方法似乎正在工作。 – shantanuo 2009-11-24 16:03:29

回答

1

使用管道时,接缝使用子壳来完成工作。由于这些子shell不知道$diskfull,所以值永远不会改变。

请参见: http://www.nucleardonkey.net/blog/2007/08/variable_scope_in_bash.html

我修改你的脚本如下。它适用于我,也应该在你的系统上工作。

#!/bin/sh 
diskfull=0 

ALERT=10 
stats=`df -HP | grep -vE '^Filesystem|tmpfs|cdrom|none|udev' | awk '{ print $5 "_" $1 }'` 
for output in $stats 
do 
    usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 ) 
    partition=$(echo $output | sed s/.*_//) 
    #echo $partition - $usep 
    if [ $usep -le $ALERT ]; then 
    diskfull=1 
    break 
    fi 
done 
echo $diskfull 
0

我想你一定不会得到的diskfull=1行,因为如果你是,你会在all--以下exit行将退出脚本得不到任何输出。

我不知道为什么,这是行不通的,但请注意,AWK可以处理剩下的工作:

diskfull=$(df -HP | grep -vE '^Filesystem|tmpfs|cdrom' | awk 'BEGIN { x = 0 } { if ($5 + 0 >= '$ALERT') { x = 1 } } END { print x }') 

这样,你不需要while循环。

0

你可以用gawk这样做(不需要使用grep)。为警报您可以发送电子邮件到根。

threshold=10 
df -HP | awk -v t="$threshold" -v msg="" 'NR>1 && $5+0 > t{ 
    msg=msg $1" is "$5"\n" 
}END{print msg}' | mail root 

或检查是否有 “味精” 或不先

threshold=10 
result=$(df -HP | awk -v t="$threshold" -v msg="" 'NR>1 && $5+0 > t{ 
    msg=msg $1" is "$5"\n" 
}END{print msg}') 
if [ -n "$result" ];then 
    echo "Overload" 
    echo "$result" | mail root 

fi 
0

在这一行:

usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 ) 

这是没有必要使用cut。你可以这样做:

usep=$(echo $output | awk -F% '{ print $1}') 
+0

使用gawk,您可以使用$ 5 + 0将字段更改为整数。 %是“自动消失”。 (缺少更好的单词) – ghostdog74 2009-11-24 13:42:28

1

@OP,使用外部支架或()

count=0 
max=10 
diskfull=0 
df -HP | { while read disk b c d used e 
do  
    if [ "$count" -gt 1 ];then 
     used=${used%?} 
     if [ "$used" -gt "$max" ];then 
      echo "overload: $disk, used: $used%" 
      diskfull=1 
     fi 
    fi 
    count=$((count+1)) 
done 
echo "diskfull: $diskfull" 
} 
5

这是在管道中使用while的副作用。有两种解决方法:

1)把while循环,在一个单独的范围内使用的所有变量由levislevis86

some | complicated | pipeline | { 
    while read line; do 
     foo=$(some calculation) 
    done 
    do_something_with $foo 
} 
# $foo not available here 

2所证实),如果你的shell允许,使用进程替换和您可以将管道的输出重定向到while循环的输入

while read line; do 
    foo=$(some calculation)} 
done < <(some | complicated | pipeline) 
do_something_with $foo 
+0

您可能想在2)之前尝试['set + o posix'](http://www.linuxjournal.com/content/shell-process-redirection)。也看[这里](http://tldp.org/LDP/abs/html/process-sub.html)。 – sjngm 2012-02-27 11:57:51