mercredi 7 septembre 2016

Bash Script : While loop and if statment

Help required plz I am beginner I am writing a script to filter GID greater than 1000 and GID less than or equal to 1000. Purpose is to filter out local groups and non-local groups (groups coming from AD) from a file.. There is a file called groups.out which contain group names , GIDs. it could be in any order. Below is the sample file which contains local groups, Non Local Groups and GID's as well.

1098052
1098051
domain users
fuse
gdm
haldaemon

and here is my logic.i want to apply

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

After sucessful execution of the loop it shoud print sucess or if any error occurs , it should exit and append errors to the file. below is my uncooked code with many parts missing. Many errors are there as well for gt or eq errors. so you can ignore it

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'tt 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

Aucun commentaire:

Enregistrer un commentaire