对每个用户说hello

时间:2023-03-09 03:27:45
对每个用户说hello
 #!/bin/bash
#对每个用户说hello
#用户数
Lines=`wc -l /etc/passwd | cut -d' ' -f1` for P in `seq $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