磁盘 space 使用脚本出错
Error on a disk space usage script
我试图修改一个检查磁盘 space 使用情况的小脚本,但我遇到了以下错误:
disk_space.sh: line 32: [: Use: integer expression expected
# Set alert limit, 90% we remove % for comparison
alert=90
# is the partition name
# is the used percentage
# Print the df -h, then loop and read the result
df -h | awk '{ print " " }' | while read output;
do
#echo $output
# Get partition name
partition=$(echo $output | awk '{ print }')
#echo 'Partition: ' $partition
# Used space with percentage
useSpPer=$(echo $output | awk '{ print }')
#echo 'Used space %: ' $useSpPer
#used space (remove percentage)
useSp=$(echo -n $useSpPer | head -c -1)
#echo 'Used space digit: ' $useSp
# Recap
#echo $useSp ' has ' $partition
# -ge is greatter than or equal
if [ $useSp -ge $alert ]; then # THIS LINE 32
echo $partition 'is running out of space with '$useSpPer
#else
#echo 'Down'
fi
done
如果有人有想法,请感激并提前致谢
将 set -x
放在脚本的顶部,以便它在执行前回显每一行是调试 shell 脚本的好方法 - 您几乎肯定会发现其中一个变量 (在 [
命令中使用)未按预期设置。
这是很好的一般性建议,但是,对于您已定位问题的这个问题,将此行放在生成问题的行之前可能就足够了(而且肯定不那么冗长):
echo "DEBUG [$useSp]"
如果这样做,您会发现您检查的值根本不是数值。那是因为 df -h
的输出看起来像这样:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 48G 4.9G 40G 11% /
/dev/sr0 71m 71M 0 100% /media/cdrom0
这意味着,对于第一行,您将把 Use
这个词与您的限制进行比较,而 [
将无法很好地处理:
pax> if [ 20 -gt 10 ]; then echo yes; fi
yes
pax> if [ Use -gt 10 ]; then echo yes; fi
bash: [: Use: integer expression expected
修复相当简单。由于您不想对第一行做任何事情,您可以使用 existing awk
将其过滤掉:
df -h | awk 'NR>1{print " "}' | while read output;
do
...
NR>1
只处理第二条记录,以此类推,跳过第一条。
您的 useSp
值为 "Use" [非数字],因此 -ge
试图将字符串与整数进行比较 [并抱怨]。
更新:
根据您的要求,有几种方法可以修复您的脚本。
修复您现有的脚本是其中之一 [参见下面的修复]。但是,您会发现,bash 中的这种字符串操作可能有点冒险。
另一个是,因为您已经在 awk
[在多个地方],重新编写脚本以在 awk
script[=41] 中完成大部分工作=] 文件。例如:
df -h | awk -f myawkscript.awk
但是,awk 有点古老,因此,最终,一种更新的语言,例如 perl
或 python
将是长期发展的方式。强大的字符串操作和计算。它们被编译为 VM,因此它们 运行 更快。而且,他们有很好的诊断信息。 IMO,与其学习更多 awk
,不如开始学习 perl/python
[因为从专业角度来说,需要使用这些语言进行编程]
但是,要立即修复您现有的脚本:
旧:df -h | awk '{ print " " }' | while read output;
新:df -h | tail -n +2 | awk '{ print " " }' | while read output;
我试图修改一个检查磁盘 space 使用情况的小脚本,但我遇到了以下错误:
disk_space.sh: line 32: [: Use: integer expression expected
# Set alert limit, 90% we remove % for comparison
alert=90
# is the partition name
# is the used percentage
# Print the df -h, then loop and read the result
df -h | awk '{ print " " }' | while read output;
do
#echo $output
# Get partition name
partition=$(echo $output | awk '{ print }')
#echo 'Partition: ' $partition
# Used space with percentage
useSpPer=$(echo $output | awk '{ print }')
#echo 'Used space %: ' $useSpPer
#used space (remove percentage)
useSp=$(echo -n $useSpPer | head -c -1)
#echo 'Used space digit: ' $useSp
# Recap
#echo $useSp ' has ' $partition
# -ge is greatter than or equal
if [ $useSp -ge $alert ]; then # THIS LINE 32
echo $partition 'is running out of space with '$useSpPer
#else
#echo 'Down'
fi
done
如果有人有想法,请感激并提前致谢
将 set -x
放在脚本的顶部,以便它在执行前回显每一行是调试 shell 脚本的好方法 - 您几乎肯定会发现其中一个变量 (在 [
命令中使用)未按预期设置。
这是很好的一般性建议,但是,对于您已定位问题的这个问题,将此行放在生成问题的行之前可能就足够了(而且肯定不那么冗长):
echo "DEBUG [$useSp]"
如果这样做,您会发现您检查的值根本不是数值。那是因为 df -h
的输出看起来像这样:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 48G 4.9G 40G 11% /
/dev/sr0 71m 71M 0 100% /media/cdrom0
这意味着,对于第一行,您将把 Use
这个词与您的限制进行比较,而 [
将无法很好地处理:
pax> if [ 20 -gt 10 ]; then echo yes; fi
yes
pax> if [ Use -gt 10 ]; then echo yes; fi
bash: [: Use: integer expression expected
修复相当简单。由于您不想对第一行做任何事情,您可以使用 existing awk
将其过滤掉:
df -h | awk 'NR>1{print " "}' | while read output;
do
...
NR>1
只处理第二条记录,以此类推,跳过第一条。
您的 useSp
值为 "Use" [非数字],因此 -ge
试图将字符串与整数进行比较 [并抱怨]。
更新:
根据您的要求,有几种方法可以修复您的脚本。
修复您现有的脚本是其中之一 [参见下面的修复]。但是,您会发现,bash 中的这种字符串操作可能有点冒险。
另一个是,因为您已经在 awk
[在多个地方],重新编写脚本以在 awk
script[=41] 中完成大部分工作=] 文件。例如:
df -h | awk -f myawkscript.awk
但是,awk 有点古老,因此,最终,一种更新的语言,例如 perl
或 python
将是长期发展的方式。强大的字符串操作和计算。它们被编译为 VM,因此它们 运行 更快。而且,他们有很好的诊断信息。 IMO,与其学习更多 awk
,不如开始学习 perl/python
[因为从专业角度来说,需要使用这些语言进行编程]
但是,要立即修复您现有的脚本:
旧:df -h | awk '{ print " " }' | while read output;
新:df -h | tail -n +2 | awk '{ print " " }' | while read output;