您的位置:首页 > 其它

对每个用户说hello

2016-08-14 19:56 162 查看
#!/bin/bash
#对每个用户说hello
#用户数
Lines=`wc -l /etc/passwd | cut -d' ' -f1`

for P in `seq 1 $Lines`; do
echo "Hello,`sed -n ""$P"p" /etc/passwd | cut -d: -f1`."
done


扩展:

1、设定变量FILE的值为/etc/passwd
2、依次向/etc/passwd中的每个用户问好,并显示对方的shell,形如:
Hello, root, your shell: /bin/bash
3、统计一共有多少个用户

提示:for I in `seq 1 $LINES`; do echo "Hello, `head -n $I /etc/passwd | tail -1 | cut -d: -f1`"; done

只向默认shell为bash的用户问声好

#!/bin/bash
#对每个用户说hello
#用户数
Lines=`wc -l /etc/passwd | cut -d' ' -f1`

echo "Total user $Lines."
echo "user's shell is /bin/bash:"
for P in `seq 1 $Lines`; do
if [ `sed -n ""$P"p" /etc/passwd | cut -d: -f7` == "/bin/bash" ]; then
echo "$P Hello,`sed -n ""$P"p" /etc/passwd | cut -d: -f1`,your shell is `sed -n ""$P"p" /etc/passwd | cut -d: -f7`."
fi
done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: