shell脚本练习题

9.shell脚本

9.1 查找当前网段(10.1.1.0/24)内存活的用户,重定向到/tmp/ip.txt文件中(ping)

[root@saber ~]# vim ping1.sh

脚本

 #!/bin/bash
 net=$1 //位置参数,执行脚本时输入的第一个参数
 for i in {1..254}; do //网段中一共有254个可能的用户
 (                                           
     ping -c1 -w1 $net.$i > /dev/null 2>&1  //ping 一次 一秒 输出信息重定向到null
     if [ $? -eq 0 ]; then
       echo "$net.$i is alive" >> /tmp/ip.txt  //重定向到/tmp/ip.txt文件
     else
       echo "$net.$i is dead"
     fi
     )& //模拟并行,加快速度
done
~        

结果

[root@saber ~]# ./ping1.sh 10.1.1
10.1.1.6 is dead
10.1.1.2 is dead
10.1.1.7 is dead
10.1.1.8 is dead
10.1.1.1 is dead
10.1.1.5 is dead
10.1.1.9 is dead
10.1.1.11 is dead
10.1.1.14 is dead
10.1.1.15 is dead
10.1.1.16 is dead
10.1.1.19 is dead
10.1.1.20 is dead
10.1.1.21 is dead
10.1.1.18 is dead
10.1.1.22 is dead
10.1.1.24 is dead
10.1.1.25 is dead
10.1.1.27 is dead
10.1.1.28 is dead
10.1.1.30 is dead
10.1.1.31 is dead
10.1.1.34 is dead
10.1.1.35 is dead
10.1.1.36 is dead
10.1.1.37 is dead
10.1.1.38 is dead
10.1.1.39 is dead
10.1.1.40 is dead
10.1.1.41 is dead
10.1.1.42 is dead
10.1.1.43 is dead
10.1.1.44 is dead
10.1.1.45 is dead
10.1.1.46 is dead
10.1.1.47 is dead

9.2 自动创建用户 student101-150,且密码为password101-150

[root@localhost ~]# vi student.sh //编写脚本

脚本

#!/bin/bash
i=101
while [ $i -lt 151 ];do
      id student$i > /dev/null 2>&1  //显示用户id,命令显示结果重定向到null,用户存在,则命令结果为0,否则非0
      if [ $? -eq 0 ]; then //根据上一条命令结果判断用户是否存在
         echo "user student$i exist" 
     else
        useradd student$i
        echo "user student$i added"
        echo "password$i" | passwd --stdin student$i > /dev/null 2>&1 //设置密码,重定向到null
     fi
 let i++

done
~            

结果

user student101 added
user student102 added
user student103 added
user student104 added
user student105 added
user student106 added
user student107 added
user student108 added
user student109 added
user student110 added
user student111 added
user student112 added
user student113 added
user student114 added
user student115 added
user student116 added
user student117 added
user student118 added
user student119 added
user student120 added
user student121 added
user student122 added
user student123 added
user student124 added
user student125 added
user student126 added
user student127 added
user student128 added
user student129 added
user student130 added
user student131 added
user student132 added
user student133 added
user student134 added
user student135 added
user student136 added
user student137 added
user student138 added
user student139 added
user student140 added
user student141 added
user student142 added
user student143 added
user student144 added
user student145 added
user student146 added
user student147 added
user student148 added
user student149 added
user student150 added

9.3 编写一个shell脚本,检测当前服务器正在运行的网络服务,并输出服务名

[root@saber ~]# vi test.sh

脚本

#!/bin/bash
netstat -tulnp |sed '1,2d'| awk '{print $1'} |sort -u

结果

[root@saber ~]# ./test.sh
tcp
udp

9.4 根据passwd文件内容,输出“The line 行数 is 用户”以此类推

[root@saber ~]# vi read.sh

脚本

 #!/bin/bash
    while read line; do
    let n++
    user=`echo $line | cut -d: -f1`
 echo "the line $n is $user"
 done < /etc/passwd

结果

[root@saber ~]# ./read.sh
the line 1 is root
the line 2 is bin
the line 3 is daemon
the line 4 is adm
the line 5 is lp
the line 6 is sync
the line 7 is shutdown
the line 8 is halt
the line 9 is mail
the line 10 is uucp
the line 11 is operator
the line 12 is games
the line 13 is gopher
the line 14 is ftp
the line 15 is nobody
the line 16 is dbus
the line 17 is usbmuxd
the line 18 is rpc
the line 19 is rtkit
the line 20 is avahi-autoipd
the line 21 is vcsa
the line 22 is abrt
the line 23 is rpcuser
the line 24 is nfsnobody
the line 25 is haldaemon
the line 26 is ntp
the line 27 is apache
the line 28 is saslauth
the line 29 is postfix
the line 30 is gdm
the line 31 is pulse
the line 32 is sshd
the line 33 is tcpdump
the line 34 is yanling
the line 35 is nginx
the line 36 is bash
the line 37 is testbash
the line 38 is basher
the line 39 is nologin

9.5 创建shell脚本test6.sh

9.5.1 输入“kernel”参数时,此shell脚本就输出“user”

9.5.2 输入“user”参数时,此shell脚本就输出“kernel”

9.5.3 当此脚本不输入任何参数或者输入的参数是按照要求输入的话,那么就会输出标准错误

[root@saber ~]# vi test6.sh

脚本

#!/bin/bash
read -p "please input:" word
case $word in
 kernel)
   echo "user"
;;
user)
   echo "kernel"
;;
 *)
 echo "Usage:./test6.sh kernel | user"
;;
esac

结果

[root@saber ~]# ./test6.sh
please input:kn^He^H^H
Usage:./test6.sh kernel | user
[root@saber ~]# ./test6.sh
please input:kernel
user
[root@saber ~]# ./test6.sh
please input:user
kernel

9.6 打印无密码用户

[root@saber ~]# vi go.sh

脚本

#!/bin/bash
n=0
while read line; do 
  let n++
  pass=`echo $line | cut -d: -f2` //密码在第二个字段
  user=`echo $line | cut -d: -f1` //用户名在第一个字段
  if [ "$pass" = "!!" -o "$pass" = "*" ]; then //当第二个字段是!!或者*时代表无密码,pass变量代表字符,要加双引号
     echo "$user"
   fi
done < /etc/shadow //读取文件

结果

[root@saber ~]# ./go.sh
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
dbus
usbmuxd
rpc
rtkit
avahi-autoipd
vcsa
abrt
rpcuser
nfsnobody
haldaemon
ntp
apache
saslauth
postfix
gdm
pulse
sshd
tcpdump
nginx
bash
testbash
basher
nologin

9.7 写一个脚本,显示当前系统上shell为-s指定类型的shell,并统计其用户总数。(-s选项后面跟的参数必须是/etc/shell文件中的shell类型,否则不执行此脚本。另外,此脚本还可以接受–help选项,以显示帮助信息。执行及显示结果如下:#./test8.sh -s bash bash,3users,they are:root alice bob)

[root@saber ~]# vi test8.sh

脚本

#!/bin/bash
if [ $# -eq 2 ]; then // 检查参数是否为两个
case $1 in
-s)
type=`grep "$2" /etc/shells | cut -d/ -f3` // 在shells文件中查找出是否有指定shell的类型,类型在最后一个字段,截取
count=`grep "$2$" /etc/passwd | wc -l` // 在passwd中查找是否有以输入shell类型的用户,一行为一个用户,计算行数即为用户数
user=`grep "$2$" /etc/passwd | cut -d: -f1` //在passwd中把用户名截取出来
echo "$type,$count,they are:$user" //输出
;;
--help)
echo "usage:./tset.sh -s bash"
;;
esac
fi

结果

[root@saber ~]# ./test8.sh -s bash
bash,5,they are:root
yanling
bash
testbash
basher

9.8 监控磁盘的使用率,大于90%给root发邮件

#!/bin/bash
df -P |sed '1d' | while read line; do // df查看磁盘信息,第一行无用删除
usage=`echo $line | awk '{print $5'}` //usage在第五个字段,awk默认空格为分隔符,输出第五个字段给变量usage
usage1=${usage%\%*} //变换单位
disk=`echo $line | awk '{print $6}'` //disk的路径名
if [ $usage1 -gt 90 ]; then 
 echo "$disk is hihg usage" | mail -s "warning" root@saber
fi
done

9.9 ldd是用来查看二进制程序的共享库文件的,现在通过脚本实现把库文件自动的复制到固定目录(/newroot)的相对应的目录中,如源库文件:/lib/a.so.1,应复制到/newroot/lib/a.so.1

9.10 查看主机网卡流量,每2秒显示一次当前速率(sleep 2程序睡眠两秒)

[root@saber ~]# vi test9.sh

脚本

#!/bin/bash 
while :; do // :=ture 一直循环
rx=`ifconfig eth0 | sed -n '8p' | awk '{print $2}'| cut -c7- ` //第八行是速率,bytes表示收发的字节数
tx=`ifconfig eth0 | sed -n '8p' | awk '{print $6}'| cut -c7- `
echo "RX byttes=$rx,TX bytes=$tx"
sleep 2
done
#!/bin/bash
while :; do
ifconfig eth0 | sed -n '8p'
sleep 2
done
~              

结果

[root@saber ~]# ./test9.sh
RX byttes=97996390,TX bytes=7424760
RX byttes=97996450,TX bytes=7424910
RX byttes=97996510,TX bytes=7425060
RX byttes=97996570,TX bytes=7425210
^C

参数说明:

1.IFACE:LAN接口

2.rxpck/s:每秒钟接收的数据包

3.txpck/s:每秒钟发送的数据包

4.rxbyt/s:每秒钟接收的字节数

5.txbyt/s:每秒钟发送的字节数

6.rxcmp/s:每秒钟接收的压缩数据包

7.txcmp/s:每秒钟发送的压缩数据包

8.rxmcst/s:每秒钟接收的多播数据包

9.rxerr/s:每秒钟接收的坏数据包

10.txerr/s:每秒钟发送的坏数据包

11.coll/s:每秒冲突数

12.rxdrop/s:因为缓冲充满,每秒钟丢弃的已接收数据包数

13.txdrop/s:因为缓冲充满,每秒钟丢弃的已发送数据包数

14.txcarr/s:发送数据包时,每秒载波错误数

15.rxfram/s:每秒接收数据包的帧对齐错误数

16.rxfifo/s:接收的数据包每秒FIFO过速的错误数

es=97996570,TX bytes=7425210
^C


参数说明:

1.IFACE:LAN接口

2.rxpck/s:每秒钟接收的数据包

3.txpck/s:每秒钟发送的数据包

4.rxbyt/s:每秒钟接收的字节数

5.txbyt/s:每秒钟发送的字节数

6.rxcmp/s:每秒钟接收的压缩数据包

7.txcmp/s:每秒钟发送的压缩数据包

8.rxmcst/s:每秒钟接收的多播数据包

9.rxerr/s:每秒钟接收的坏数据包

10.txerr/s:每秒钟发送的坏数据包

11.coll/s:每秒冲突数

12.rxdrop/s:因为缓冲充满,每秒钟丢弃的已接收数据包数

13.txdrop/s:因为缓冲充满,每秒钟丢弃的已发送数据包数

14.txcarr/s:发送数据包时,每秒载波错误数

15.rxfram/s:每秒接收数据包的帧对齐错误数

16.rxfifo/s:接收的数据包每秒FIFO过速的错误数

17.txfifo/s:发送的数据包每秒FIFO过速的错误数
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值