0

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.

4

1 回答 1

3

要遍历用户/etc/passwd并对每个用户执行某些操作,请尝试以下操作:

cut -d: -f1 /etc/passwd | while IFS= read -r user
do
    echo "$user"
    # do something with $user
done

如果要检查特定用户是否存在/etc/passwd然后执行某些操作,请使用 if 语句:

if grep -q "^username:" /etc/passwd
then
    # do something
fi
于 2013-03-19T17:58:15.497 回答