2017-07-01 10 views
0

私はBashで始まりますが、私はこれをやろうとしますが、できません。forループ内の変数内のネストされた配列

例:

array=("/dev/sda1" "/dev/sdb1") 

for i in "${array[@]";do 
    space=$(df -H | grep ${array[1]}) 
done 

またはこの:

i=0 

for i in ("/dev/sda1" "/dev/sdb1") 
    space=$(df -h | grep (($i++))) 
done 

これは可能ですか?

答えて

1

あなたが直接これはbashでfor-loopを研究するベンチャーであるならば、あなたは

以下のように同じことを行うことができ、以下のように dfに、配列を供給し、しかし for-loop

よう
df -H "${array[@]}" 

の使用を避けることができます

for i in "${array[@]}" 
do 
df -H "$i" 
# Well, "${array[@]}" expands to everything in array 
# and i takes each value in each turn. 
done 

そして、あなたは、インデックスを使って配列にアクセスしたい場合は、

for ((i = 0; i < ${#array[@]}; i++)) # This is a c-style for loop 
do 
    df -H "${array[i]}" # Note the index i is not prefixed with $ 
done 

編集

使用量はたとえば、10ギガバイト

# We will usage file which we would use to fill the body of mail 
# This file will contain the file system usage 
# But first make sure the file is empty before every run 
cat /dev/null > /tmp/diskusagereport 
for i in "${array[@]}" 
do 
usage=$(df -H "$i" | awk 'END{sub(/G$/,"",$3);print $3}') 
if [ "${usage:-0}" -gt 10 ] 
then 
    echo "Filesystem : ${i} usage : ${usage}GB is greater than 10GB" >> /tmp/diskusagereport 
fi 
done 
#finally use the `mailx` client like below 

mailx -v -r "[email protected]" -s "Disk usage report" \ 
     -S smtp="smtp.yourserver.org:port" -S smtp-auth=login \ 
     -S smtp-auth-user="your_auth_user_here" \ 
     -S smtp-auth-password='your_auth_password' \ 
         [email protected] </tmp/diskusagereport 
+1

が同様に '((I = 0のために運動を締めくくりかもしれないが、より多くのであるかどうかを確認するには、 i <$ {#array [@]}; i ++)); df -H "$ {array [i]}";を実行します。 done' ... ':)' –

+0

しかし、dfの結果はどのように変数になりましたか?考え方は、結果を変数に保存して、実行できるようにすることです。 – Ceac

+0

@Cacこの変数で何をやっているのか分かりますか?しばしば、それに基づいて計算を行うために 'df'から特定の値が必要になります。 – sjsam

関連する問題