Linux While循环,/etc/passwd中的用户名

Linux While循环,/etc/passwd中的用户名,linux,bash,scripting,Linux,Bash,Scripting,我如何编写一个while循环,该循环说:当用户名在/etc/passwd文件中时,do(命令)? 我试图使用一个命令,比如grep-q“^{usern}:“/etc/passwd,但我不确定如何将其作为while循环的条件 要在/etc/passwd中循环用户并对每个用户执行操作,请尝试以下操作: cut -d: -f1 /etc/passwd | while IFS= read -r user do echo "$user" # do something with $user

我如何编写一个while循环,该循环说:当用户名在/etc/passwd文件中时,do(命令)?
我试图使用一个命令,比如
grep-q“^{usern}:“/etc/passwd
,但我不确定如何将其作为while循环的条件

要在
/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

要在
/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
此问题的副本:此问题的副本: