对每个用户说hello

本文介绍了一个简单的Shell脚本,该脚本能够遍历/etc/passwd文件中的所有用户,并向使用bash shell的用户发送问候消息。此外,还提供了一个扩展版本的脚本,用于展示如何向所有用户问候并显示他们的shell类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 #!/bin/bash
2 #对每个用户说hello
3 #用户数
4 Lines=`wc -l /etc/passwd | cut -d' ' -f1`
5 
6 for P in `seq 1 $Lines`; do
7   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/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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值