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

相关推荐

  • grep,find等相关命令

    Q1:显示当前系统上root、fedora或user1用户的默认shell; ~]# grep -E "^root|^fedora|^user1" /etc/passwd | awk -F: '{print $1,$NF}' ro…

    Linux干货 2016-11-27
  • 正则表达式基础知识及grep、egrep详解

    什么是正则表达式     在计算机科学中,正则表达式是这样解释的:它是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。在很多文本编辑器或其他工具里,正则表达式通常被用来检索和/或替换那些符合某个模式的文本内容。许多程序设计语言都支持利用正则表达式进行字符串操作。对于系统管理员来讲,正则表达式贯穿在我们…

    Linux干货 2016-08-08
  • 软硬链接区别及tr转换命令

    软硬链接的区别 (1)软连接可以 跨文件系统 ,硬连接不可以 (2)关于 I节点的问题 。硬连接不管有多少个,都指向的是同一个I节点,会把 结点连接数增加,只要结点的连接数不是 0,文件就一直存在 ,不管你删除的是源文件还是连接的文件。只         要有一个存在,文件就 存…

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

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

    Linux干货 2017-03-06
  • 第九周作业

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; 2、写一个脚本     (1) 获取当前主机的主机名,保存于hostname变量中;     (2) 判断此变量的值是否为loc…

    2017-04-20
  • 内核参数修改 内核编译 第14天

    Linux内核:单内核,模块化 内核的某些模块 编译进内核本体 [*] 编译成内核模块 [M] 不选择使用     [ ] 内核的组成部分 /boot/vmlinuz-VERSION /lib/modules/VERSION/ *.ko 模块间有可能有依赖关系 内核模块管理 lsmod:显…

    Linux干货 2016-01-18