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

相关推荐

  • find命令简单总结

     find     字符串类查询:            -type   //根据文件类型查找       &…

    Linux干货 2015-09-14
  • 马哥教育网络班21期+第12周课程练习

    1、请描述一次完整的http请求处理过程; 建立或处理请求:接受请求或拒绝请求; 接收请求:接收来自于网络的请求报文中对某资源的一次请求的过程; 处理请求:对请求报文进行解析,并获取请求的资源及请求方法等相关信息; 访问资源:获取请求报文中请求的资源; 构建相应报文; 发送响应报文; 记录日志 2、httpd所支持的处理模型有哪些,他们分别适用于哪些环境。 …

    Linux干货 2016-10-24
  • 马哥教育21期网络班—第五周课程+练习

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; grep "^[[:space:]]\+" /boot/grub/grub.conf 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行; grep "…

    Linux干货 2016-07-29
  • 过滤语句和复制,权限的一些事例

    1.复制/etc/skel 目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限 ~]# cp -r /etc/skel/ /home/tuser1/ ~]# chmod go= /home/tuser1/ 2.编辑/etc/group文件,添加组hadoop ~]# echo "hadoop:…

    Linux干货 2016-10-14
  • 一个“蝇量级” C 语言协程库

    协程(coroutine)顾名思义就是“协作的例程”(co-operative routines)。跟具有操作系统概念的线程不一样,协程是在用户空间利用程序语言的语法语义就能实现逻辑上类似多任务的编程技巧。实际上协程的概念比线程还要早,按照 Knuth 的说法“子例程是协程的特例”,一个子例程就是一次子函数调用,那么实际上协程就是类函数一样的程序组件,你可以…

    Linux干货 2016-08-15
  • n28 第二周作业

    n28 第二周作业

    Linux干货 2017-12-09