Reputation: 31
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 contains group names and GIDs. It could be in any order. Below is the sample file which contains local groups, non=local groups and GIDs as well.
1098052
1098051
domain users
fuse
gdm
haldaemon
and here is the 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 successful execution of the loop it should print success 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'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
Upvotes: 1
Views: 221
Reputation: 189417
This really looks like you want two separate scripts. Finding numbers in a particular range is simple with Awk.
awk '!/[^0-9]/ && ($1 >= 1000)' groups.out
The regular expression selects all-numeric input lines (or more properly, it excludes lines which contain a non-numeric character anywhere within them) and the numeric comparison requires the first field to be 1000 or more. (The default action of Awk is to print the entire line when the conditions in your script are true, so we can omit the implicit {print}
action).
If you also want to extract the numbers which are less than 1000 to a separate file, the change should be obvious.
For the non-numeric values, we can do
grep '[^0-9]' groups.out |
xargs getent |
awk -F : '$3 >= 1000 { print $3 }'
Several of the branches in your pseudocode seem superfluous. It's not clear in what situation ou would expect an error to occur, or how the action you specify in the error situation would help you diagnose or recover from the error (write acc ss denied, disk full?) so I have not spent any energy on trying to implement those parts.
Upvotes: 0
Reputation: 39277
#/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
Here is corrected version of your script. it should get u going.
chmod +x scriptfile
and use bash scriptfile
to run it or schedule it in crontab.
Since your information about how to match group names with gids isnt sufficent I left it out in the script but you should be able to finish it with provided information in other parts of the script.
Upvotes: 1