生データでこの3つのファイルを作成します。
quota username bytes
20480000 [email protected] 896
30720000 [email protected] 3002766
20480000 [email protected] 20472940
20480000 [email protected] 2351
Here I want that if bytes if any user is equal or greater than to quota then the username should be printed or I'll perform another task to block the user.
次の3つのファイルがあります。
users.txt
Output:
[email protected]
[email protected]
consumed_quota.txt
Output:
20
5
allowed_quota.txt
Output:
20
10
[email protected]
ファイルのユーザーが 。users.txt
consumed_quota
より大きいか等しい場合は、ユーザーを印刷したいと思いますallowed_quota
。
バッシュプログラムとは何ですか?
助けてください。
以下のコードを試していますが、一致するユーザーの代わりにすべてのユーザーが印刷されます。
#!/bin/bash
mysql -e 'select postfix.m.quota, q.* from postfix.mailbox m, postfix.quota2 q where m.username = q.username' > /tmp/all_users_query
username=`cat /tmp/all_users_query | cut -f2 | grep -v username > /tmp/usernf`
quota=`cat /tmp/all_users_query | cut -f1 | grep -v quota > /tmp/quotaf`
consumed=`cat /tmp/all_users_query | cut -f3 | grep -v bytes > /tmp/consumedf`
function show_users()
{
username=`cat /tmp/usernf`
for i in $username
do
echo $i
done
}
function actual_quota()
{
quota=`cat /tmp/quotaf`
for i in $quota
do
akb=`echo $i/1000 | bc`
amb=`echo $akb/1000 | bc`
echo $amb
done
}
function used_quota()
{
consumed=`cat /tmp/consumedf`
for i in $consumed
do
ukb=`echo "$i/1000" | bc`
umb=`echo "$ukb/1000" | bc`
echo "$umb"
done
}
declare -a arr_users="$(show_users)"
declare -a arr_act_quota="$(actual_quota)"
declare -a arr_use_quota="$(used_quota)"
for u in ${arr_users[@]}
do
for i in ${arr_use_quota[@]}
do
# echo $i;
for j in ${arr_act_quota[@]}
do
if [ "$j" == "$i" ]
then
echo $u;
break;
fi
done
done
done
ベストアンサー1
スクリプトを自分で作成し、期待した結果を得ました。
#!/bin/bash
rm -rf /tmp/over_quota_users
mysql -e 'select postfix.m.quota, q.* from postfix.mailbox m, postfix.quota2 q where m.username = q.username and q.bytes >= m.quota-bytes;' | cut -f2 | grep -v username > /tmp/over_quota_users
#Function to get users;
function show_users()
{
username=`cat /tmp/over_quota_users`
for i in $username
do
echo $i
done
}
#Function to get active users;
function check_active()
{
for i in $(show_users); do
mysql -e 'select username, active from postfix.mailbox where username = "'$i'" and active = 1;' | grep -v username
done
result=`echo $?`
echo $result
}
active_results=$(check_active $(show_users))
if [ "$active_results" == 1 ];
then
exit 0;
else
for j in $(show_users); do
#changing over quota users to inactive.
mysql -e 'update postfix.mailbox set active = '0' where username = "'$j'"';
done
rm -rf /tmp/oqa_ul_2_admin.txt
echo "" > /tmp/oqa_ul_2_admin.txt
echo "These users were over quota and active " >> /tmp/oqa_ul_2_admin.txt
echo "" >> /tmp/oqa_ul_2_admin.txt
echo "---------------------------------------" >> /tmp/oqa_ul_2_admin.txt
echo "$(show_users)" >> /tmp/oqa_ul_2_admin.txt
echo "" >> /tmp/oqa_ul_2_admin.txt
echo "---------------------------------------" >> /tmp/oqa_ul_2_admin.txt
echo "" >> /tmp/oqa_ul_2_admin.txt
/usr/bin/mail -s "Blocked Over quota and active users" [email protected] < /tmp/oqa_ul_2_admin.txt
fi