1 #!/bin/bash2 #对每个用户说hello3 #用户数4 Lines=`wc -l /etc/passwd | cut -d' ' -f1`5 6 for P in `seq 1 $Lines`; do7 echo "Hello,`sed -n ""$P"p" /etc/passwd | cut -d: -f1`."8 done
扩展:
1、设定变量FILE的值为/etc/passwd
2、依次向/etc/passwd中的每个用户问好,并显示对方的shell,形如: Hello, root, your shell: /bin/bash3、统计一共有多少个用户提示: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`." fidone