我是bash脚本的新手,尝试运行下面的脚本,我应该检查系统百分比并使用case命令运行另一个脚本
#!/bin/bash
clear
echo "will able to see the percentage of hard disk"
for line in $(</home/AAA/BBB/CCCC/pre-prod1.txt)
do
ssh $line 'hostname -s; df -P |grep '/XXX/CCCC' | awk '"'"'{print $5}'"'"
echo '--------------------------------------------------------------------------------------------'
usage = $(< 'hostname -s; df -P |grep '/opt/splunk' | awk '"'"'{print $5}'"'" )
#if [$? -gt 65]
# then
# '/home/ BB/DD /remove_old_data.sh'
#
#fi
done
case $usage in
50% - 2880 minutes) echo "when the hard disk is above 50%"
/home/BB/DD/remove_old_data.sh 2880
;;
50-75% - 1440 minutes) echo "when the hard disk space is above 75%"
/home/ BB/DD /remove_old_data.sh 1440
;;
75-90% - 720 minutes) echo "when the hard disk space is above 90%"
/home/ BB/DD /remove_old_data.sh 720
;;
>90% - 360 minutes) echo "when the hard disk space above 91%"
/home/ BB/DD /remove_old_data.sh 360
;;
esac
我收到以下错误:
./checksize1.sh: line 9: hostname -s; df -P |grep /opt/splunk | awk '{print $5}': No such file or directory
./checksize1.sh: line 9: usage: command not found
./checksize1.sh: line 19: syntax error near unexpected token `-'
./checksize1.sh: line 19: ` 50% - 2880 minutes) echo "when the hard disk is above 50%"'
答案 0 :(得分:0)
我在您的脚本中做了一些修改,它应该满足您的要求:
#!/bin/bash
clear
echo "will able to see the percentage of hard disk"
for line in $(cat /home/AAA/BBB/CCCC/pre-prod1.txt)
do
usage=$(ssh $line "df -P |grep '/opt/splunk'" | awk '{print $5}' | cut -d'%' -f1)
echo '-------------------------------------------------------------------------'
#if [$? -gt 65]
# then
# '/home/ BB/DD /remove_old_data.sh'
#
#fi
echo "Machine name: $( echo $line | cut -d'@' -f2 )"
case $usage in
50 ) echo "when the hard disk is above 50%"
/home/BB/DD/remove_old_data.sh 2880
;;
[5-6][0-9]|7[0-5] ) echo "when the hard disk space is above 75%"
/home/BB/DD/remove_old_data.sh 1440
;;
7[6-9]|8[0-9]|90 ) echo "when the hard disk space is above 90%"
/home/BB/DD/remove_old_data.sh 720
;;
9[1-9]|100 ) echo "when the hard disk space above 91%"
/home/BB/DD/remove_old_data.sh 360
;;
* ) echo "Disk space is less than 50%"
;;
esac
done
文件/home/AAA/BBB/CCCC/pre-prod1.txt
应包含服务器的主机名,如下所示:
user@server1
user@server2
user@server3
请检查此信息,如果您的要求不同,请告知我们。
注意:我更新了脚本。以前,我已将分区设为/dev/sda1
,但是,我相信您正在尝试获取分区/opt/splunk
的磁盘使用情况。我已经对我的脚本进行了更改,这应该可以正常工作。如果您尝试获取服务器上任何其他分区的磁盘使用情况,请在脚本中更新以下行:
usage=$(ssh $line "df -P |grep '/opt/splunk'" | awk '{print $5}' | cut -d'%' -f1)
我已经在测试服务器上测试了脚本并找到了以下结果。
[12:41]✓ -> bash test.sh
will able to see the percentage of hard disk
-------------------------------------------------------------------------
Server: server1
82
when the hard disk space is above 90%
-------------------------------------------------------------------------
Server: server2
87
when the hard disk space is above 90%
-------------------------------------------------------------------------
Server: server3
50
when the hard disk is above 50%