您的位置:首页 > 运维架构 > Shell

bash 脚本工具练习以及遇到的问题。

2015-03-02 22:39 281 查看
要求 :

分别统计,默认shell 各有多少用户,别分别列出用户:
格式:
/bin/bash : 3users user1 user2 user3
最前面是此类shell 共有多少个用户 后面分别是谁
/sbin/nologin
/sbin/halt
/bin/sync
/sbin/shutdown

先 touch /xbash/1.txt

#!/bin/bash
#
> /xbash/1.txt
Number=`wc -l /etc/passwd | awk -F " " '{print $1}'`

for I in `seq "$Number"`;do
N=`head -$I /etc/passwd | awk -F : '{print $NF}' | sed "$"p -n`
M=`head -$I /etc/passwd | awk -F : '{print $1}' | sed "$"p -n`
if [[ $N == "/bin/bash" ]];then
echo "/bin/bash: $M" >>/xbash/1.txt
elif [[ $N == "/sbin/nologin" ]];then
echo "/sbin/nologin: $M" >>/xbash/1.txt
elif [[ $N == "/sbin/halt" ]];then
echo "/sbin/halt: $M" >>/xbash/1.txt
elif [[ $N == "/bin/sync" ]];then
echo "/bin/sync: $M" >>/xbash/1.txt
elif [[ $N == "/sbin/shutdown" ]];then
echo "/sbin/shutdown: $M" >>/xbash/1.txt
else
echo "no"
fi
done

#bin/bash
shu1=0
#sbin/nologin
shu2=0
#sbin/nologin
shu3=0
#sbin/halt
shu4=0
#bin/sunc
shu5=0
#sbin/shutdown
shu6=0
NB=`wc -l /xbash/1.txt | awk -F " " '{print $1}'`
for P in `seq "$NB"`;do
N1=`head -$P /xbash/1.txt | sed "$"p -n | awk -F ":" '{print $1}'`

if [[ $N1 == "/bin/bash" ]];then
shu1+=$P
elif [[ $N1 == "/sbin/nologin" ]];then
shu2+=$P
elif [[ $N1 == "/sbin/halt" ]];then
shu3+=$P
elif [[ $N1 == "/bin/sunc" ]];then
shu4+=$P
elif [[ $N == "/sbin/shutdown" ]];then
shu5+=$P
else
echo "no2"
fi
done
echo "/bin/bash: "$shu1"users"
echo "/sbin/nologin: "$shu2"users"
echo "/sbin/halt: "$shu3"users"
echo "/bin/sync: "$shu4"users"
echo "/sbin/shutdown: "$shu5"users"

本文出自 “你我同行” 博客,请务必保留此出处http://ondali.blog.51cto.com/6650368/1616651
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: