Reputation: 35
How would I go about writing a while loop that says: While a username is in the /etc/passwd file, do (command)?
I'm trying to use a command such as grep -q "^{usern}:" /etc/passwd
but I'm not sure how to put that as the condition of the while loop.
Upvotes: 0
Views: 1524
Reputation: 274532
To loop over the users in /etc/passwd
and do something with each user, try the following:
cut -d: -f1 /etc/passwd | while IFS= read -r user
do
echo "$user"
# do something with $user
done
If you want to check whether a specific user exists in /etc/passwd
and then do something, use an if-statement:
if grep -q "^username:" /etc/passwd
then
# do something
fi
Upvotes: 3