[关闭]
@mrz1 2017-12-23T06:02:20.000000Z 字数 6877 阅读 1493

linux-shell练习题

shell脚本


1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。

  1. [root@centos7 ~]#vim f1.sh
  2. #! /bin/bash
  3. # ------------------------------------------
  4. # Filename: systeminfo.sh
  5. # Revision: 1.0
  6. # Date: 2017-11-24
  7. # Author: Zhang Qi Fei
  8. # Email: 1353250703@qq.com
  9. # Website: www.zhangqifei.top
  10. # Description: This is the first script
  11. # ------------------------------------------
  12. echo Hostname: `hostname`
  13. echo IP address: `ifconfig|egrep -o "[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}"|head -n1`
  14. echo System version: `cat /etc/system-release`
  15. echo kernel version: `uname -r`
  16. echo `lscpu | grep "Model name:" |tr -s ' ' | cut -d: -f2`
  17. echo `cat /proc/meminfo | grep MemTotal`
  18. echo `fdisk -l|grep Disk|head -n1`

运行结果:

  1. Hostname: centos7
  2. IP address: 172.17.108.3
  3. System version: CentOS release 6.8 (Final)
  4. kernel version: 2.6.32-696.6.3.el6.x86_64
  5. Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz
  6. MemTotal: 2054212 kB
  7. Disk /dev/vda: 42.9 GB, 42949672960 bytes

2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中

  1. [root@centos7 ~]#vim bachup.sh
  2. # ------------------------------------------
  3. #! /bin/bash
  4. ...... //省略
  5. # ------------------------------------------
  6. cp -arv /etc /root/etc`date +%F`

3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

  1. #! /bin/bash
  2. # ------------------------------------------
  3. # Filename: disk.sh
  4. ...... //省略
  5. # ------------------------------------------
  6. echo "The max using rate is:`df |grep "/dev/sd"| cut -c 44-46 | sort -nr | head -1`%"

运行结果:

  1. [root@centos7 ~]#./disk.sh
  2. The max using rate is:%

4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序

  1. #! /bin/bash
  2. # ------------------------------------------
  3. # Filename: links.sh
  4. ...... //省略
  5. # ------------------------------------------
  6. echo "the links number is:"
  7. netstat -nt |tr -s ' ' |cut -d ' ' -f5 |cut -d: -f1 |grep [0-9]|sort |uniq -c|sort
  8. -nr

运行结果:

  1. [root@centos7 ~]#./links.sh
  2. the links number is:
  3. 3 123.118.29.6
  4. 1 140.205.140.205
  5. 1 106.11.68.13

5、写一个脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和

  1. #! /bin/bash
  2. # ------------------------------------------
  3. # Filename: links.sh
  4. ...... //省略
  5. # ------------------------------------------
  6. echo 'The sum is'
  7. id1=`cat /etc/passwd | sed -n '10p' | cut -d: -f3`
  8. id2=`cat /etc/passwd | sed -n '20p' | cut -d: -f3`
  9. let idsum=$id1+$id2
  10. echo "The sum is:$idsum"
  11. unset idsum

运行结果:

  1. [root@centos7 ~]#./sumid.sh
  2. The sum is
  3. The sum is:48

6、写一个脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和

  1. #! /bin/bash
  2. # ------------------------------------------
  3. # Filename: sumspace.sh
  4. ...... //省略
  5. # ------------------------------------------
  6. file1_blank=`grep "^[[:space:]]*$" $1 | wc -l`
  7. file2_blank=`grep "^[[:space:]]*$" $2 | wc -l`
  8. sumspace=$[ $file1_blank + $file2_blank ]
  9. echo "The tatal blank line: $sumspace"

运行结果:

  1. [root@centos7 ~]#./sumspace.sh /etc/passwd /etc/rwtab
  2. The tatal blank line: 2

7、写一个脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件

  1. #! /bin/bash
  2. # ------------------------------------------
  3. # Filename: sumfile.sh
  4. ...... //省略
  5. # ------------------------------------------
  6. etcnum=`ls -d /etc/*|wc -l`
  7. varnum=`ls -d /var/*|wc -l`
  8. usrnum=`ls -d /usr/*|wc -l`
  9. echo "the totalfile is $[etcnum+varnum+usrnum]"

运行结果:

  1. [root@centos7 ~]#./sumfile.sh
  2. the totalfile is 192

8、写一个脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数

参考1:

  1. #! /bin/bash
  2. # ------------------------------------------
  3. # Filename: argsnum.sh
  4. ...... //省略
  5. # ------------------------------------------
  6. read -p "Enter a filename:" file
  7. [ -z "$file" ] && echo "please enter a file name" && exit 2
  8. echo -n "The blank line number is:"
  9. echo `grep -c ‘^[[:space:]]*$‘ $file`

运行结果:

  1. [root@centos7 ~]#./argsnum.sh
  2. Enter a filename:/etc/passwd
  3. The blank line number is:0

参考2:

  1. #! /bin/bash
  2. # ------------------------------------------
  3. # Filename: $1
  4. ......省略
  5. # ------------------------------------------
  6. [ $# -lt 1 ] && echo "至少一个参数" && exit 20
  7. [ -e $1 ] && echo `grep -c '^$' $1` || echo "没有这样的文件或目录?"

执行结果:

  1. [root@centos7 ~]# ./srgsnum.sh /etc/passwd
  2. 0
  3. [root@centos7 ~]# ./srgsnum.sh
  4. 至少一个参数

9、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户"该IP地址可访问";如果不可ping通,则提示用户"该IP地址不可访问"

  1. #! /bin/bash
  2. # ------------------------------------------
  3. # Filename: houstping.sh
  4. ...... //省略
  5. # ------------------------------------------
  6. [ $# -ne 1 ] && echo "please input one ipaddr" && exit 2
  7. ping -c1 -w1 $1 &> /dev/null && echo "ping successful" || echo "ping failed"

运行结果:

  1. [root@centos7 ~]#./houstping.sh 47.93.96.256
  2. ping failed
  3. [root@centos7 ~]#./houstping.sh 47.93.96.29
  4. ping successful

10、判断硬盘的每个分区空间和inode的利用率是否大于80,如果是,发邮件通知root磁盘满

参考1:

  1. #! /bin/bash
  2. # ------------------------------------------
  3. # Filename: dist.sh
  4. ...... //省略
  5. # ------------------------------------------
  6. dist=`(df -i /dev/sd*;df /dev/sd*)|egrep -o '[0-9]+%' |egrep -o '[0-9]+'|sort -nr| hea
  7. d -1`
  8. [ "$dist" -gt 10 ] && wall "disk will be full"

运行结果:

  1. [root@centos7 ~]#./dist.sh
  2. Broadcast message from root@centos7.qifei.com (pts/1) (Sun Nov 26 13:33:05 2017):
  3. disk will be full

参考2:

  1. #! /bin/bash
  2. # ------------------------------------------
  3. # Filename: dist.sh
  4. ...... //省略
  5. # ------------------------------------------
  6. dist=`df |grep "/dev/sd" |grep -o '[0-9]*%' |grep -o '[0-9]*'|sort -nr| head -1`
  7. inode=`df -i |grep "/dev/sd" |egrep -o '[0-9]*%' |egrep -o '[0-9]*'|sort -nr| head -1`
  8. sum=80
  9. [ "$dist" -ge "$sum" ] && echo -e "磁盘空间占用率超过"$sum"\a `wall 'The disk space will be full' `"||echo ">
  10. 磁盘利用率为"$dist",可以使用"
  11. [ "$inode" -ge "$sum" ] && echo -e "inode占用率超过"$sum"\a `wall 'The disk space will be full'`" ||echo "ino
  12. de利用率为"$inode",可以使用"

运行结果:

  1. [root@centos7 ~]#./dist.sh
  2. 磁盘利用率为16,可以使用
  3. inode利用率为1,可以使用

参考3:

  1. #! /bin/bash
  2. # ------------------------------------------
  3. # Filename: warning.sh
  4. ...... //省略
  5. # ------------------------------------------
  6. echo `df |grep "/dev/sd" |grep -o "\<[[:digit:]]\{1,3\}%" |grep -o "[[:digit:]]\{1,3\}" |sort -nr`

运行结果:

  1. [root@centos7 ~]#./warning.sh
  2. 16 8 1

11、判断输入的IP是否为合法IP

  1. #! /bin/bash
  2. # ------------------------------------------
  3. # Filename: f1.sh
  4. ...... //省略
  5. # ------------------------------------------
  6. read -p "please input one useful ip:" ip_addr
  7. echo $ip_addr | grep -E "^(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>.){3}\<([0-9]|[1-9]
  8. [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>$" &> /dev/null && echo "this is a useful ip" || echo "this
  9. is not a useful ip"

运行结果:

  1. [root@centos7 ~]#./f1.sh
  2. please input one useful ip:12.23.56.32
  3. this is a useful ip
  4. [root@centos7 ~]#./f1.sh
  5. please input one useful ip:256.235.233.213
  6. this is not a useful ip

12.编写脚本/bin/per.sh,判断当前用户对指定的参数文件,是否不可读并且不可写

13.编写脚本/root/bin/excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件

14.编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统

15.让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin

16.用户root登录时,将命令指示符变成红色,并自动启用如下别名:rm='rm –i'

  1. cdnet='cd /etc/sysconfig/network-scripts/'
  2. editnet='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
  3. editnet='vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或 ifcfg-ens33 ' (如果系统是CentOS7)

17.任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”

18.编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等

19.编写用户的环境初始化脚本reset.sh,包括别名,登录提示符,vim的设置,环境变量等

20.当前这个进程打开了多少个文件?

ls /proc/$$/fd |wc -l

21.判断有用户就提示没有就创建?

  1. #! /bin/bash
  2. # ------------------------------------------
  3. # Filename: user.sh
  4. ...... //省略
  5. # ------------------------------------------
  6. [ -z "$1" ] && read -p "please input a username: " username
  7. id $username &> /dev/null && echo "$username is exist" && exit 10
  8. useradd $username && echo "$username is created"

运行结果:

  1. [root@centos7 ~]#bash nologin.sh
  2. please input a username: zhang1
  3. zhang1 is created
  4. [root@centos7 ~]#bash nologin.sh
  5. please input a username: zhang1
  6. zhang1 is exist
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注