8.10 shell scripts 作业

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

[root@CentOs6 bin]# systeminfo.sh 
      Hostname:  CentOs6.localdomain;
  IP   address:  10.1.252.175 ;
System version:  CentOS release 6.8 (Final);
kernel version:  2.6.32-642.el6.x86_64;
   CPU   type :  Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz;
  Disk   size :  107.4 GB
  
#############################################################################################

[root@CentOs6 bin]# vim systeminfo.sh 
#!/bin/bash
echo "      Hostname:  `hostname`;"
echo "  IP   address:  ` ifconfig | sed -n '2p' | cut -d: -f2 | cut -d\  -f1` ;"
echo "System version:  `cat /etc/redhat-release`;"
echo "kernel version:  `uname -r`;"
echo "   CPU   type :  `lscpu | sed -n 's/Model name:[[:space:]]\+//p'`;"
echo "  Disk   size : `fdisk -l | sed -nr -e 's@.*:(.*),.*@\1@p' | head -1`"

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

[root@CentOs6 bin]# vim backup.sh 
#!/bin/bash
echo "backup start"
cp -a /etc /root/etc`date +%F`
echo "backup over"

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

[root@CentOs6 bin]# disk.sh 
the disk max using rate is :19%
#############################################################################################
[root@CentOs6 bin]# vim disk.sh 
#!/bin/bash
echo "the disk max using rate is :`df | grep "sd" | tr -s ' ' | cut -d\  -f5  | sort -n | tail -1`"

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

[root@CentOs6 bin]# links.sh 
The links :2 10.1.250.27

###########################################################################################

[root@CentOs6 bin]# vim links.sh 
#!/bin/bash
echo "The links :`netstat -nt |tr -s ' '  |cut -d ' ' -f5 |cut -d: -f1 |grep [0-9]|sort |uniq -c|sort -nr | sed -r 's/^[[:space:]]+//'`"

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

[root@CentOs6 bin]# sumid.sh 
this's user 10 UID:10
this's user 20 UID:32
they are sumID is :42

##############################################################################################

[root@CentOs6 bin]# vim sumid.sh 
#!/bin/bash
user10=`sed -n '10p' /etc/passwd | cut -d: -f3`
echo "this's user 10 UID:$user10"
user20=`sed -n '20p' /etc/passwd | cut -d: -f3`
echo "this's user 20 UID:$user20"
sum=$[user10+user20]
echo "they are sumID is :$sum"

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

[root@CentOs6 bin]# sumspace.sh 
please input filename1 :/etc/issue
please input filename2 :/etc/issue
space sum is :10

#########################################################################################

[root@CentOs6 bin]# vim sumspace.sh 
#!/bin/bash
read -p "please input filename1 :" file
file10=`egrep -c '^$' $file`
read -p "please input filename2 :" file2
file20=`egrep -c '^$' $file2`
sumspace=$(( file10  + file20 ))
echo "space sum is :$sumspace"

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

[root@CentOs6 bin]# sumfile.sh 
file /etc :230
file /var :22
file /usr :13
filesum = 265

##############################################################################################

[root@CentOs6 bin]# vim sumfile.sh 
#!/bin/bash
etc=`ls -1 /etc | wc -l`
echo "file /etc :$etc"
var=`ls -1 /var | wc -l`
echo "file /var :$var"
usr=`ls -1 /usr | wc -l`
echo "file /usr :$usr"
filesum=$[ etc+var+usr ]
echo "filesum = $filesum"

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

[root@CentOs6 bin]# argsnum.sh 
plese input file:/etc/issue
The file space : 5

##############################################################################################

[root@CentOs6 bin]# vim argsnum.sh 
#!/bin/bash
#
read -p "plese input file:" file
if [[ -f $file  ]];then
     echo "The file space : `egrep -c "^$" $file`"
else
    echo "Should at least give args"
fi


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

[root@CentOs6 bin]# hostping.sh 
plase input ipaddr:10.1.0.1
Connect successfully

##############################################################################################

[root@CentOs6 bin]# vim hostping.sh 
#!/bin/bash
read -p "plase input ipaddr:" ip
ping -W1 -c1 $ip &> /dev/null  && echo Connect successfully || echo Connect fail


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

[root@CentOs6 bin]# using.sh 
disk use normal

##############################################################################################

[root@CentOs6 bin]# vim using.sh 
#!/bin/bash
inode=`df -i | egrep -o "[1-9][0-9]%" | sed -r 's@([0-9]+)%@\1@'`
disk=`df -P | egrep -o "[2-9][0-9]%" | sed -r 's@([0-9]+)%@\1@'`
[ $inode -ge 80 ] || [ $disk -ge 80 ] && mail -s 'disk over' root < /root/bin/mail || echo "disk use normal";exit

11、指定文件做为参数,判断文件是否为.sh后缀,如果是,添加x权限

[root@CentOs6 bin]# chfile.sh 
please input filename:test.sh         
test.sh

##############################################################################################

[root@CentOs6 bin]# vim chfile.sh 
read -p "please input filename:" file
[ -f $file ] && echo "$file" | grep "\.sh$" && chmod +x $file;exit || exit

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

[root@CentOs6 bin]# ipok.sh 
plese input ip address:192.168.1.0     
You input ipaddr OK 

##############################################################################################

[root@CentOs6 bin]# vim ipok.sh 
read -p "plese input ip address:" ipaddr
echo $ipaddr |  egrep "^(\b([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-2][0-3])\b\.)(\b([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b\.){2}(\b([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])\b)$" &> /dev/null && echo "You input ipaddr OK " || echo "You input ipaddr don't OK"

13、计算1+2+3+…+100

[root@CentOs6 bin]# digitsum.sh 
digit sum is :5050

##############################################################################################

[root@CentOs6 bin]# vim digitsum.sh 
#!/bin/bash
#
sum=`echo {1..100} | tr " " "+" | bc`
echo "digit sum is :$sum"

14、输入起始值A和最后值B,计算从A+(A+1)…+(B-1)+B的总和

[root@CentOs6 bin]# ABsum.sh 
please input value A:100
please input value B:1
            A + B = :5050
            
##############################################################################################

[root@CentOs6 bin]# vim ABsum.sh 
#!/bin/bash
read -p "please input value A:" A
read -p "please input value B:" B
[ $A -lt $B ] && echo "          A + B sum :`seq -s+ $A $B| bc`" || echo "            A + B = :`seq -s+ $B $A| bc`"

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

(0)
LiiLii
上一篇 2016-08-12 17:30
下一篇 2016-08-12 20:43

相关推荐

  • 马哥教育第一天记

    一天学习心得和记录

    Linux干货 2018-03-26
  • Linux-文件系统挂载:mount的用法

     文件系统挂载  本章详细介绍文件系统挂载、卸载(mount,umount);  如何通过mount命令挂载使用文件系统、挂载常用参数、以及开机自动挂载(/etc/fstab)。    挂载描述:             挂载:将额外文件系…

    2017-06-19
  • LVM练习及quota和dd命令

    练习 1、创建一个至少有两个PV组成的大小为20G的名为testvg的 VG;要求PE大小为16MB, 而后在卷组中创建大小为5G的逻 辑卷testlv;挂载至/users目录 第一步!首先准备足够大的硬盘来实验 [root@Cloud /]#lsblk  #添加了/dev/sdb/c硬盘  NAME &nb…

    Linux干货 2016-09-09
  • SHELL编程之数组运用及YUM软件包管理

    SHELL编程中,当要引用到多个值的时候,一个一个地进行变量赋值会让我们的脚本变得繁琐,不利于代码的优化,所以,就需要通过数组进行定义,优化代码,减少不必要的定义和命令操作。 SHELL中的数组:存储多个元素的连续内存空间 数组名:整个属组只有一个名字 数组索引: 编号从0开始   数组名[索引]   ${array_name[index…

    Linux干货 2016-08-24
  • 111-puppet

    一.运维层次分类 OS Provision:

    2016-12-04
  • HTTP详解(3)-http1.0 和http1.1 区别

    翻了下HTTP1.1的协议标准RFC2616,下面是看到的一些它跟HTTP1.0的差别。 1. Persistent Connection持久连接        在HTTP1.0中,每对Request/Response都使用一个新的连接。      …

    Linux干货 2015-04-04