gpt4 book ai didi

linux - while循环,用户名在/etc/passwd

转载 作者:太空宇宙 更新时间:2023-11-04 09:03:39 24 4
gpt4 key购买 nike

我将如何编写一个 while 循环来表示:当用户名在/etc/passwd 文件中时,执行(命令)?我正在尝试使用诸如 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

关于linux - while循环,用户名在/etc/passwd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15506834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com