2016-09-08 8 views
1

私は、1000より大きいGIDと1000以下のGIDをフィルタリングするスクリプトを書いています。目的は、ローカルグループと非ローカルグループ(ADから来るグループ)をフィルタリングしてファイル..Bashスクリプト:Whileループとif文

グループ名とGIDを含むgroups.outというファイルがあります。それはどのような順序でもよい。以下は、ローカルグループ、非=ローカルグループ、およびGIDも含むサンプルファイルです。

1098052 
1098051 
domain users 
fuse 
gdm 
haldaemon 

とここで私はそれが成功を印刷しなければならないか、何らかのエラーが発生した場合、それが終了し、ファイルにエラーを追加する必要があり、ループの実行が成功した後

Read line by line from the file, 
if the line is a number then check 
     if number greater than or equal to 1000 then check 
      if greater than or equal to 1000, append it to the file 
     else if number less than 1000 then dump it 
     else if erorr occurs append error to file and break the loop and exit 

if the line is a string then check the gid of the string/group 
    if number greater than or equal to 1000 then append to file 
    else if gid less than 1000 then dump it 
    else if error occurs append error to file and break the loop and exit 
want to repeat it in the loop line by line and if anywhere the error occurs loop should break and exit the entire script 

を適用するロジックがあります。 以下は、多くの部分が欠けている私の調理されていないコードです。 gtまたはeqエラーには多くのエラーもあります。あなたはここで

fileA="groups.out" 
value=1000 
re='[a-z]' 
num='[0-9]' 
while IFS= read lineA 
do 
group=$(getent group "$lineA" | awk -F: '{print $3}') 
# ------Don't know how to check if a number or string ----- 
    if [ "$group" -gt "$value" ]; then 
     echo "$lineA" >> ldapgroups.out 2>> error.out 
     elif [ "$group" -lt "$value" ]; then 
     echo "$lineA" >> /dev/null 2>> error.out 

     else 
     echo " FAILED" 
     exit 1 
     fi 
+0

ここでは、bashの数値または文字列をチェックする方法を示します。http://stackoverflow.com/a/806923/524743 – Samuel

答えて

1
#/bin/bash 
fileA="groups.out" 
value=1000 
num='^[0-9]+$' 
while IFS= read lineA 
do 
    #check if the line is numbers only 
    if [[ $lineA =~ $num ]];then 
     echo "This is a number" 
     echo $lineA 
     #check if $line is greater than 1000 
     if [[ $lineA -gt $value ]];then 
      #write it to file named numbers.out 
      echo "number is greater than 1000 writing to file" 
      echo $lineA >> numbers.out 
     else 
      echo "less than, Skipping" 
     fi 
    #if its not number, its group names right? so no need to check if with regex 
    else 
     #do what ever u want with group names here ... 
     echo "string" 
     echo $lineA 
    fi 
# This is where you feed the file to the while loop 
done < $fileA 

それを無視することができますスクリプトのバージョンを補正しています。それはあなたに行く必要があります。 chmod +x scriptfileを使用して実行したり、crontabでスケジュールするにはbash scriptfileを使用してください。

gidとグループ名を一致させる方法についての情報は十分ではありませんので、私はスクリプトでそれを残しましたが、スクリプトの他の部分で提供された情報で終了する必要があります。

0

これは本当に2つの別々のスクリプトが必要なようです。特定の範囲内の数字を見つけることはAwkで簡単です。

awk '!/[^0-9]/ && ($1 >= 1000)' groups.out 

正規表現は、すべての数値の入力ラインを選択(正しく以上、それはどこでもそれらの中に数字以外の文字を含む行を除く)と数値比較は、1000個以上であることを最初のフィールドが必要です。 (Awkのデフォルトの動作は、スクリプト内の条件が真であるときに行全体を出力することで、暗黙の{print}アクションを省略することができます)。

また、1000未満の数字を別のファイルに抽出する場合は、変更を明確にする必要があります。非数値の

、我々はあなたの擬似コードに支店の数が余分に見える

grep '[^0-9]' groups.out | 
xargs getent | 
awk -F : '$3 >= 1000 { print $3 }' 

を行うことができます。エラーが発生すると予想される状況や、エラー状況で指定したアクションがエラーの診断や復旧にどのように役立つかはわかりません(write accs denied、disk full?)それらの部分を実装しようとしています。