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基础知识之RAID

    1、什么是RAID?     多个磁盘合成一个“阵列”来提供更好的性能、冗余,或者两者都提供。2、RAID的优点?     提高IO 能力:         磁盘并行读写   &n…

    Linux干货 2016-09-01
  • Linux文件管理命令与bash的工作特性

    Shell程序在接受到用户执行命令的请求时,在分析完成之后,最左侧的字符串会被当作命令;
    命令查找机制:查找内部命令时,根据PATH环境变量中设定的目录,从左至右逐个搜索目录下的文件名;

    2018-03-11
  • 计算机的组成介绍

    一,什么是计算机?     计算机(computer)俗称电脑,是现代一种用于高速计算的电子计算机器,可以进行数值计算,又可以进行逻辑计算,还具有存储记忆功能。是能够按照程序运行,自动、高速处理海量数据的现代化智能电子设备。 二,发展历史 阶段 时期(年) 主要器件 特征 应用领域发展 第一代 1946—1958 电子管数字机 电子管,机…

    2016-10-29
  • iptables/netfilter网络防火墙:

    iptables/netfilter网络防火墙: FORWORD链上的防火墙规则 路由功能打开[root@localhost ~]# echo 1 > /proc/sys/net/ipv4/ip_forward 网关设置防火墙,让内网可以访问外网网页 iptables -A FORWARD -s 192.168.126.131 -p tcp –dpo…

    2016-10-26
  • CentOS7.3系统上编译安装httpd.2.4.25

    本文所做的所有操作是在一部新安装的CentOS7.3系统上。 1.环境与配置 环境说明:VMware上安装的CentOS7.3系统,7.3完整版光盘做成的yum源 配置:编译生成的所有的文件都存放在/usr/local/httpd24/这个目录下 2.安装GCC编译器 #安装Development tools这个软件包组 [root@pxe68 ~]# yu…

    2017-04-20
  • 文本处理工具作业

    1、找出ifconfig命令结果中本机的所有IPv4地址 root@cenots6.8  ~ #  ifconfig | tr -cs '[0-9]\.' '\n' |sort -u -t&…

    Linux干货 2016-08-07