すべての配列メンバーを繰り返します。

すべての配列メンバーを繰り返します。

私は最近、スクリプトで遊んだ。 @Casの助けを借りてこれを書きました。

#!/bin/bash


## Variables ##

host="`/bin/hostname`";


    ## Limits ##

OneMin="1";
FiveMin="6";
FifteenMin="6";

    ## Mail IDs ##

To="[email protected], [email protected]";
Fr="root@"$host;


    ## Load Averages ##

LA=(`uptime | grep -Eo '[0-9]+\.[0-9]+' | cut -d"." -f1`)


    ## Top Process List ##

tp=(`ps -ef | sort -nrk 3,3 | grep -E "(php|httpd)" | grep -v root | head -n30 | awk '{print $2}'`)


## Actions ##

if [ ${LA[0]} -ge $OneMin ]; then


    ## Send Mail ##

echo -e "From: $Fr
To: $To
Subject: *ALERT* - Current Load on '$host' Is High
Load Averages Are:  \n\n
1:Min\t5:Min\t15:Min   \n
${LA[0]}\t${LA[1]}\t${LA[2]}  \n\n

List Of Processes That Were Killed \n" | sendmail -t


    ## Kill Top Pocesses ##


for i in $tp ; do
    kill -9 $i
done



fi

配列が有効かどうかはわかりません。特に、最上位プロセスを終了するためにループを追加した最後の部分は、その電子メールのプロセスリストを印刷しないためです。理由はわかりません。ただし、スクリプトにエラーは発生しません。

確認##解決策##

しかし、これは食べますか?

#!/bin/bash


## Variables ##

host="`/bin/hostname`";


    ## Limits ##

OneMin="7";
FiveMin="6";
FifteenMin="6";


    ## Load Averages ##

LA=(`uptime | grep -Eo '[0-9]+\.[0-9]+' | cut -d"." -f1`)



## Actions ##


    ## One Minut Action ##

if [ ${LA[0]} -ge $OneMin ]; then


    ## Send Mail ##

echo -e "From: $Fr
To: $To
Subject: *ALERT* - Current Load on '$host' Is High
Load Averages Are:  \n\n
1:Min\t5:Min\t15:Min   \n
${LA[0]}\t${LA[1]}\t${LA[2]}  \n\n

List Of Processes That Were Killed \n
`ps -ef | sort -nrk 3,3 | grep -E "(php|httpd)" | grep -v root | head -n30 | awk '{print $2}'`" | sendmail -t


    ## Kill Top Pocesses ##


for i in `ps -ef | sort -nrk 3,3 | grep -E "(php|httpd)" | grep -v root | head -n30 | awk '{print $2}'` ; do
    kill -9 $i
done


fi

私の言葉はこれがすべてのPiDを殺すのだろうか?

ベストアンサー1

問題は、tp=(...配列が定義されているが配列$tpの最初の要素のみが参照されることです。

あなたはする必要があります

for i in "${tp[@]}" ; do

おすすめ記事