Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 检查文件系统百分比,并使用case命令运行脚本_Linux_Bash_Shell_Switch Statement - Fatal编程技术网

Linux 检查文件系统百分比,并使用case命令运行脚本

Linux 检查文件系统百分比,并使用case命令运行脚本,linux,bash,shell,switch-statement,Linux,Bash,Shell,Switch Statement,我不熟悉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 '

我不熟悉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

我对您的脚本进行了一些修改,它应该满足您的要求:

#!/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%

请看一看:引用破折号被解释的案例感谢脚本,请查找以下错误:将能够看到硬盘的百分比-------------------------------------------------------------------./checksize1.sh:第14行:abc.sec.xyz.com:未找到命令主机名:磁盘空间小于50%我的系统名是abcd.zxc.xzy.com,我在其中有许多系统,当我检查系统的磁盘百分比时,其中一个高于50%,但我仍然得到了上面的错误。对不起,第14行的脚本中有一个小错误。我已经修改了脚本,它现在应该可以工作了。感谢脚本,我能够运行脚本,但如果硬盘使用率高于50%或低于50%,我会得到“磁盘空间小于50%”的输出,它还应该根据我们得到的输出运行第二个脚本。还有什么建议吗
[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%