shell脚本学习-练习写一个脚本1

时间:2021-02-28 14:54:18

 

# 1.依次展示/etc/passwd中的用户名和UID。格式如:Hello,$USER,your UID is $UID.

# 2.统计一个有多少个用户

#!/bin/bash
#Program Description:   
#Author: Created by ctu_lzq 
#Date: 2018/11/15
FILE="/etc/passwd"
LINES=`cat $FILE |wc -l`
#for i in `seq 1 $LINES`;
for ((i=1;i<=$LINES;i++));
do
    userlist=`sed -n "$i""p" $FILE|awk -F: '{print $1}'`
    uidlist=`sed -n "$i""p" $FILE|awk -F: '{print $3}'`
    echo "Hello,$userlist,your UID is $uidlist"
done
echo  "there are $LINES users"

 

结果如:
[root@study learnshell]# bash test.sh Hello,root,your UID is 0 Hello,bin,your UID is 1 Hello,daemon,your UID is 2 Hello,adm,your UID is 3 Hello,lp,your UID is 4 Hello,sync,your UID is 5 Hello,shutdown,your UID is 6 Hello,halt,your UID is 7 Hello,mail,your UID is 8 Hello,uucp,your UID is 10 Hello,operator,your UID is 11 Hello,games,your UID is 12 Hello,gopher,your UID is 13 Hello,ftp,your UID is 14 Hello,nobody,your UID is 99 Hello,dbus,your UID is 81 Hello,usbmuxd,your UID is 113 Hello,rpc,your UID is 32 Hello,rtkit,your UID is 499 Hello,avahi-autoipd,your UID is 170 Hello,vcsa,your UID is 69 Hello,abrt,your UID is 173 Hello,rpcuser,your UID is 29 Hello,nfsnobody,your UID is 65534 Hello,haldaemon,your UID is 68 Hello,ntp,your UID is 38 Hello,apache,your UID is 48 Hello,saslauth,your UID is 498 Hello,postfix,your UID is 89 Hello,gdm,your UID is 42 Hello,pulse,your UID is 497 Hello,sshd,your UID is 74 Hello,tcpdump,your UID is 72 Hello,mysql,your UID is 27 Hello,nginx,your UID is 496