1

I'm trying to write a script is suppose to parse a file to find and print the usernames for all non-system accounts. If the account has a non-standard (i.e., non-bash) shell, then the script should mark that username with an asterisk. I am trying to have it ignore all system accounts, which are accounts that have a user ID of less than 1000. If the account has an ID of at least 1000, then you will print the username of that account. If the shell for that account is not /bin/bash, then also print an asterisk after the name to indicate that it is non-standard.

I am new to bash and really struggle with some of the syntax. Any help would be greatly appreciated.

#!/bin/bash
count=$(cat /etc/passwd | awk -F: '{print $3}')

if [[ $count -ge 1000 ]]; then
  cat /etc/passwd | awk -F: '{print $1}'
fi

Is there an easier way to do this?

2 Answers 2

2

Yes, awk has conditions and can do arithmetics :

awk -F: '$3 >= 1000 { print $1 }' /etc/passwd

For each line in /etc/passwd, the first column will be printed if the third is greater than or equal to 1000.

Your current code probably failed with some weird errors because your count isn't a single user id but rather all of them ; you therefore couldn't compare that variable's content to a single integer.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use cut instead of awk to extract columns. You can't compare $count in your snippet because it contains all the numbers, you need to compare them one by one:

#!/bin/bash
while read user id ; do
    if (( id >= 1000 )) ; then
        echo $user
    fi
done < <(cut -d: -f1,3 --output-delimiter=' ' /etc/passwd)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.