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
下一篇 2016-08-12

相关推荐

  • 如何用SHELL写好网络爬虫

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nolinux.blog.51cto.com/4824967/1550976        上周,老大压下来任务,让写一个网络爬虫,负责爬某一个行业网站的数据信息。由于本人只会 sh…

    Linux干货 2016-08-15
  • DNS详解

    概述     互联网访问绝大多数都是基于域名的访问,互联网访问的基础是基于IP来实现的,因此,需要有一种将域名解析成IP的机制,让用户在利用域名访问时,自动将域名转换成为对应的IP,这就是DNS的功能,本章将介绍一些DNS相关的内容,具体分为:     1、DNS的基础概念 &n…

    Linux干货 2016-10-08
  • 浅谈筛选日志中的IP地址信息

    作为运维人员,经常会需要会对日志中的某些重要信息进行筛选,比如说ip等参数。 案例一:筛选出IP地址信息 日志信息如下: [root@C67-X64-A1 hanghang]# cat test.txt  Jul 13 08:13:09 localhost sshd[14678]…

    系统运维 2016-07-22
  • mysql基础概念笔记 part1

    mysql基础概念笔记     part1#wmd-preview h1 { color: #0077bb; /* 将标题改为蓝色 */} mysql基础概念笔记     part1 mysql 基础概念 基础原理,逻辑架构,事务,并发控制,读写锁 1、前言     作为一个运维…

    Linux干货 2016-09-19
  • linux树状结构

    linux 目录结构   /:根目录,一般根目录下只存放目录,不要存放文件,/etc、/bin、/dev、/lib、/sbin应该和根目录放置在一个分区中 /bin:/usr/bin:可执行二进制文件的目录,如常用的命令ls、tar、mv、cat等。 /boot:放置linux系统启动时用到的一些文件。/boot/vmlinuz为linux的内核文…

    Linux干货 2016-08-05
  • N26 第五周博客作业

    1、显示当前系统上root、fedora或user1用户的默认shell; 演示:     [root@263821a05cd9 /]# grep -E “^(root|fedora|user1)\>” /etc/passwd    root:x:0:0:root:/r…

    Linux干货 2017-03-05