shell习题-统计普通用户

1.要求:

写个shell,看看你的Linux系统中是否有自定义用户(普通用户),

若是有,一共有几个?

2.模拟环境:

centos7ID范围:
超级用户:0
虚拟用户:1-999(Centos6 1-499)

普通用户:1000-65535(Centos6 500-65535


[root@liang 2018-06-18]# cat /etc/redhat-release 

CentOS Linux release 7.2.1511 (Core) 


创建20个普通用户:(原先没有普通用户)

[root@liang 2018-06-18]# for i in `seq 20`;do useradd user_${i} ;done


用户密码文件


[root@liang 2018-06-18]# tail -1 /etc/passwd
user_20:x:1019:1019::/home/user_20:/bin/bash


3.脚本答案:

[root@liang 2018-06-18]# cat count_user.sh 
#!/bin/bash
Num=0
user_id=1000
File=/test/user.txt
/usr/bin/awk -F ":" '{print $3}' /etc/passwd >> ${File}
for id in `cat ${File}`
do
        if [ "$id" -ge "$user_id" ];then
                Num=`expr $Num + 1`
        fi

done

echo "The average user num: $Num"

> ${File}


4.运行脚本:

[root@liang 2018-06-18]# bash count_user.sh 
The average user num: 20


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值