shell脚本作业

1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,
IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。
脚本代码
#!/bin/bash
#author:wzc
        echo Hostname: `hostname`
       
        echo IP address:`ifconfig |head -n2 |tail -n1|cut -d. -f1-4
        |cut -dt -f2|cut -dn -f1`
       
        echo System version: `cat /etc/system-release`
     
        echo kernel version: `uname -r`
    
        echo CPU Type:`lscpu | grep "Model name"
        |cut -d: -f2|tr -s " "`
     
        echo Disk size :`fdisk -l|grep "sda:"|sed 's/.*:'//|sed 's/,.*'//
        |sed 's/^[[:space:]]\+'//`
        
        echo Memory size : `free -m|grep "Mem"|tr -s [[:space:]]
        |cut -d" " -f 2` MB
脚本测试结果:
Hostname: wzc.localdomain
IP address: 10.1.253.22
System version: CentOS Linux release 7.2.1511 (Core)
kernel version: 3.10.0-327.el7.x86_64
CPU Type: Intel(R) Core(TM) i5-4200M CPU @ 2.50GHz
Disk size :128.8 GB
Memory size : 1824 MB
2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到
/root/etcYYYY-mm-dd中
脚本代码
#!/bin/bash
#author:wzc
    cp -a /etc /root/etc`date +%F`
脚本测试结果:
[root@wzc date]# ./backup.sh 
[root@wzc date]# ls /root/etc2016-08-15/
3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
脚本代码:
#!/bin/bash
#author:wzc
    echo "The max using rate is:`df |grep "/dev/sd"| cut -c 44-46 
    | sort -nr | head -1`%"
脚本测试结果
[root@wzc date]# ./disk.sh 
The max using rate is: 51%
4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的
IPv4地址和连接数,并按连接数从大到小排序
脚本代码
#!/bin/bash
#author:wzc
    echo the links number is:`netstat -nt |tr -s ' '  |cut -d ' ' -f5 
    |cut -d: -f1 |grep [0-9]|sort |uniq -c|sort -nr`
脚本测试结果
[root@wzc date]# ./link.sh 
the links number is: 1 10.1.250.38
5、写一个脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户
和第20用户的ID之和
脚本代码:  
#!/bin/bash
#author:wzc
        number1=`cat /etc/passwd|sed -n "10p" |cut -d: -f3`
        number2=`cat /etc/passwd|sed -n "20p" |cut -d: -f3`
        let number=$number1+$number2    
        echo $number
脚本测试结果
[root@wzc date]# ./sumid.sh 
70
6、写一个脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,
计算这两个文件中所有空白行之和
脚本代码
#!/bin/bash
#author:wzc
        file1=`grep "^$" $1 | wc -l`            
        file2=`grep "^$" $2 | wc -l`
        file3=$[ $file1+$file2 ]
        echo "space is $file3个"
脚本测试结果
[root@wzc date]# ./sumspace.sh  /etc/issue /etc/passwd
space is 1个
7、写一个脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少
个一级子目录和文件
脚本代码
[root@wzc date]# vim sumfile.sh
#!/bin/bash
#author:wzc
        etcnum=`ls  /etc/|wc -l`
        varnum=`ls  /var/|wc -l`
        usrnum=`ls  /usr/|wc -l`
        echo the totalfile is "$[$etcnum+$varnum+$usrnum]"

脚本测试结果
[root@wzc date]# ./sumfile.sh 
the totalfile is 293
8、写一个脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;
如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;
如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
脚本代码
#!/bin/bash
#author:wzc
  [[ $# -lt 1 ]] && echo "argnum less than 1" || grep -c  '^[[:space:]]*$' $1
脚本测试结果
[root@wzc date]# ./argsnum.sh 
argnum less than 1
9、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,
测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;
如果不可ping通,则提示用户“该IP地址不可访问”
脚本代码
#!/bin/bash
#author:wzc
  ping -c1 -W1 $1 &> /dev/null && echo ping successfull || echo  ping failture
脚本测试结果
[root@wzc date]# ./hostping.sh 
ping failture
10、判断硬盘的每个分区空间和inode的利用率是否大于80,如果是,
发邮件通知root磁盘满
脚本代码
#!/bin/bash
#author:wzc
    dl=`df -hT|tr -s " "|cut -d" " -f6|grep -o "[[:digit:]]\+"|sort -nr 
| df -hT|tr -s " "|cut -d" " -f6|grep -o "[[:digit:]]\+"|sort -nr|sed -n '1p'`
[ $dl -ge 80 ] && echo "Disk load is high, please timely manner!"|mail -s 
"Disk load warning" root
脚本测试结果
    这里会发一封邮件给root,告知磁盘使用率过高,这是因为挂载光盘的原因

原创文章,作者:RecallWzc,如若转载,请注明出处:http://www.178linux.com/35702

(0)
RecallWzcRecallWzc
上一篇 2016-08-15 11:54
下一篇 2016-08-15 11:58

相关推荐

  • Linux中的包管理

    1. RPM介绍 rpm是linux中的包管理软件,通过rpm用户可以对rpm包进行查询、安装、卸载、升级和校验等操作。 1.1 查询 查询 -q:可以查看某个包是否已经安装 -qa:查看系统已经安装的所有包 -qi:查询某个安装包的详细情况 -ql:查询安装某个包之后会生成哪些文件 -qc:查询某个软件的配置文件 -qd:查询某个软件的所有文档 &#821…

    Linux干货 2017-04-17
  • RAID

    RAID:       Redunant ARRAYS OF Inexpensive Disks       廉价磁盘阵列 Independent        Berkeley: A case for Redundent Arrays of Inexpens…

    Linux干货 2016-12-23
  • 1.初识linux

    1.计算机组成 计算机五大部件:运算器、控制器、存储器、输入设备、输出设备。对应实际情况大致如下: CPU CPU(Central Processing Unit)主要包含运算器、控制器;也包含寄存器、缓存用于辅助: CPU在同一时间能并行处理的二进制数的位数称为CPU字长。字长由CPU对外数据通路的数据总线条数决定。 运算器 运算器也称…

    Linux干货 2016-08-22
  • 编译安装httpd

    编译安装httpd   1 去官网下载源码包                                      为避免非法软件,一定要去官网下载http…

    2017-04-24
  • 搭建一套LVS-DR模型的高性能集群(LVS-DR+Nginx+Mariadb+Nfs+php-fpm+wordpress)

    架构简介 参加马哥linux培训一周了,自己感觉每天都进步很多,现在来说说这个架构,也是架构师第一周的作业第二题。搭建一套LVS-DR模型的高性能集群,做了两天终于完成并实现了这个架构,我设计的架构图如下所示: 此架构主要是用ipvsadm来实现高性能集群,Realserver(RS1,RS2)安装配置nginx,RS服务器分别配置了一个内网,一个外网。正常…

    Linux干货 2016-12-06
  • Linux 基础 (1)

    su nano shell type hash alias date cal screen echo $ tab (df bc rz(sz) ifconfig ping tty who whoami w) 1.用户  root为超级用户 1) useradd oracle  创建一个oracle用户     su &#…

    2017-07-13