初窥门径之脚本书写

一、几个脚本的编写:

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

#!/bin/bash
echo "HOSTNAME: $HOSTNAME"
echo "IPV4 ADDRESS: `ip a |grep "\<inet\>[ ].*brd"|tr -s " "|cut -d" " -f 3|tr '\n' ' '`"
echo "OS RELEASE: `cat /etc/centos-release`"
echo "CPU INFORMATION: `lscpu | grep "Model name"|tr -s " "|cut -d: -f2 |sed -r 's@[ ](.*)@\1@'`"
echo "KERNEL RELEASE:`uname -r`"
echo "MEMORY INFORMATION: ` cat /proc/meminfo|head -n 1|cut -d: -f 2|tr -d " "`"
echo "HARD DISK INFOMATION:"` fdisk -l |grep "Disk.*sd"|cut -d: -f2|cut -d ' ' -f2,3|tr -d ','`

测试运行:

HOSTNAME: centos7.localdomain
IPV4 ADDRESS: 10.1.255.177/16 192.168.122.1/24
OS RELEASE: CentOS Linux release 7.2.1511 (Core)
CPU INFORMATION: AMD E2-1800 APU with Radeon(tm) HD Graphics
KERNEL RELEASE:3.10.0-327.el7.x86_64
MEMORY INFORMATION: 1001336kB
HARD DISK INFOMATION:107.4 GB

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

#!/bin/bash
#author jack_cui
#version 1.0
#description: backup directory
read -p "please input backup directory: " backdir
read -p "please input destination directory: " desdir
cp -r $backdir $desdir/backup`date +%F`
echo "backup successful!"

测试运行:

[root@centos7 bin]# backup.sh
please input backup directory: /root/bin/
please input destination directory: /testdir/
backup successful!
[root@centos7 bin]# ls /testdir
backup2016-08-12  fs.txt  info.txt  info.txt.bak  info.txt.bak.backup  vimtu

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

[root@centos7 bin]# cat disk.sh
#!/bin/bash
#author:jack_cui
#description=show the system disk use
disk_use=`df |grep sd|tr -s " "|cut -d " " -f5|sed -n 's/%//p'|sort -rn|head -n 1`
echo $disk_use
unset disk_use

测试运行:

[root@centos7 bin]# bash disk.sh
63

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

[root@centos7 bin]# cat link.sh
#!/bin/bash
#author:jackcui
#description: disk use infomation
netstat -nt |tr -s ' '|cut -d ' ' -f5 |cut -d: -f1 |grep [0-9]|sort |uniq -c|sort -nr
[root@centos7 bin]# link.sh
      5 10.1.1.88

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

#!/bin/bash
#author:jackcui
#description:sum two user's id
numbera=`sed -n '10p' /etc/passwd |cut -d: -f3`
numberb=`sed -n '20p' /etc/passwd |cut -d: -f3`
let suma_b=numbera+numberb
echo "user1 uig is $numbera"
echo "user2 uid is $numberb"
echo "two users id sum is $suma_b"
unset numberb  suma_b numbera
 [root@centos7 bin]# sumid.sh
user1 uig is 11
user2 uid is 59
two users id sum is 70

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

两个测试文本:test.txt test2.txt 中间有6个空白行或全是空白字符的行

 [root@centos7 ~]# cat test.txt
aaaaaaaaaaaaaaaaaaaaaaaa
 
bbbbbbbbbbbbbbbbbbbbbbbbbb
     ccccccccccccccccccc
 
     
[root@centos7 ~]# cat test2.txtt
cat: test2.txtt: No such file or directory
[root@centos7 ~]# cat test2.txt
cccccccccccccccccccccccc
 
bbbbbbbbbbbbbbbbbbbbbbbbbb
  
cccccccccccccccc

   脚本:

[root@centos7 bin]# cat sumspace.sh
#!/bin/bash
#author:jackcui
#description:calculate two files blank line
read -p "Input first path of files: " first_path
read -p "Input second path of files:" second_path
a=`cat $first_path |grep -e "^$" -e "[[:space:]]\+$" |wc -l`
b=`cat $second_path |grep -e "^$" -e "[[:space:]]\+$" |wc -l`
let sum=a+b
echo "The total blank lines is: $sum"
unset a b sum

脚本运行:

 [root@centos7 bin]# sumspace.sh
Input first path of files: /root/test.txt
Input second path of files:/root/test2.txt
The total blank lines is: 6

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

[root@centos7 bin]# cat sumfile.sh
#!/bin/bash
#author:jackcui
#description:sum directorys's files and directorys 
a=`ls -A /etc/|wc -l`
b=`ls -A /var/|wc -l`
c=`ls -A /usr/|wc -l`
let sumfile=a+b+c
echo "three directory have $sumfile files and directorys"
unset a b c sumfile

脚本运行:

[root@centos7 bin]# sumfile.sh
three directory have 296 files and directorys

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

[root@centos7 bin]# cat argsnum.sh
#!/bin/bash
#author:jackcui
#description:dispaly file blank lines count
[ "$#" -lt 1 ]&& echo "You should give one parameter!" && exit
line=`cat $1|grep -c "^[[:space:]]*$" 2> /dev/null`
echo $line
unset line
exit
[root@centos7 bin]# argsnum.sh /root/test.txt
3
 [root@centos7 bin]# argsnum.sh //没有参数时直接退出,并打印提示信息
You should give one parameter!

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

[root@centos7 bin]# cat hostping.sh
#!/bin/bash
#author:jackcui
#description:Judge a ip address whether access
ping -W 1 -c3 $1 > /dev/null 2>&1   //指定ping超时时间1s,ping3次
suss=`echo $?`
[ $suss -eq 0 ] && echo "IP address can access!"|| echo "IP address can not access!"
[root@centos7 bin]# bash  hostping.sh 10.1.0.2 //ping 不存在的地址报错
IP address can not access!
[root@centos7 bin]# bash  hostping.sh 10.1.0.1 //ping 存在的地址提示地址可以访问
IP address can access!

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

[root@centos7 bin]# cat diskAlarm.sh

#!/bin/bash
#author:jackcui
#description:show the disk use
diskuse=`df |grep 'sd'|tr -s " " |cut -d " " -f 5|sed -n 's/%//p'|sort -rn|head -n1`
inodeuse=` df -i|grep 'sd'|tr -s " "|cut -d" " -f 5|sed  -n 's/%//p'|sort -rn|head -n1`
#[ $diskuse -ge 80 ]||[ $inodeuse -ge 80 ]&&echo "Dear root, disk has used too much!!!"|mail -s "Disk alarm!" root
[ $diskuse -ge 80 -o $inodeuse -ge 80 ]&&echo  "Dear root, disk has used too much!!!"|mail -s "Disk alarm!" root

测试运行:

[root@centos7 bin]# diskAlarm.sh
[root@centos7 bin]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Sat Aug 13 12:49  18/626   "Disk alarm!"
& q
Held 1 message in /var/spool/mail/root

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

[root@centos7 bin]# cat ifsh.sh
#!/bin/bash
#author:jack_cui
#description:show the file whether ended “.sh”
[[ "$1" =~ .*\.sh$ ]]&&chmod a+x $1||echo "file is not ended '.sh'"
[root@centos7 bin]# touch bb.sh
[root@centos7 bin]# ifsh.sh bb.sh
[root@centos7 bin]# ll bb.sh
-rwxr-xr-x. 1 root root 0 Aug 13 13:59 bb.sh
[root@centos7 bin]#

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

[root@centos7 bin]# cat ipcorrect.sh
#!/bin/bash
#author:jack_cui
#description:display input ip whether correct!"
corr=`echo $1 |grep -E "(\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>.){3}\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"|wc -l`
[ $corr -ne 0  ]&&echo "The IP address is correct!"||echo "The IP is not correct!"

执行结果:

[root@centos7 bin]# ipcorrect.sh 2.2.2.2
The IP address is correct!
[root@centos7 bin]# ipcorrect.sh 233.2333.333.2
The IP is not correct!

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

[root@centos7 bin]# cat sum1_100
#!/bin/bash
#author:jackcui
#description:Add 1 to 100
sum=`seq -s "+" 1 100 |bc`
echo "Add 1 to 100 sum is : $sum"
[root@centos7 bin]# sum1_100
Add 1 to 100 sum is : 5050

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

[root@centos7 bin]# cat A2Bsum.sh
#!/bin/bash
#author:jackcui
#description:calculator A to B (A and B is two number),INCREMENT can be given,for eg:A=2,B=6,INCREMENT is 2 ,the sum=2+4+6=12;
read -p "Input first number: " A
read -p "Input second number: " B
read -p "Iput INCREMENT number: " I
echo "The sum is : `seq -s '+' $A $I $B|bc`"
unset A B I
[root@centos7 bin]# A2Bsum.sh
Input first number: 2
Input second number: 4
Iput INCREMENT number: 1 //递增值为1
The sum is : 9
[root@centos7 bin]# A2Bsum.sh
Input first number2  //计算2至6递增值为2的数字的和
Input second number6
Iput INCREMENT number2  //递增值为2
The sum is : 12

注释:seq –s 指定分割符为” + ” ,seq可以产生递增性的数字,之后传值给bc命令进行计算

二、位置变量$0 $1,$2,$# $@ $*的区别:

 

$0 :表示执行命令时,命令本身;

$1:表示命令的第一个参数

$#:表示命令的参数的个数

$@,$*:这两个只有在命令的参数用引号括起来时,前者表示在引号中以空格为分隔符,将引号中的多段分别当成一个参数传递值脚本,后者表示将引号中字符串当成一个参数传递至脚本

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

(0)
jack_cuijack_cui
上一篇 2016-08-15
下一篇 2016-08-15

相关推荐

  • nginx配置文件中文文档

    Nginx配置参数中文说明。 #定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数。worker_processes 8; #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]error_log /var/log/nginx/error…

    Linux干货 2017-08-08
  • 马哥教育网络班21期第11周课程练习

    1、详细描述一次加密通讯的过程,结合图示最佳。 发送方Bob:自上至下 1、生成数据 2、Bob用单向加密算法对数据提取特征码 3、Bob用自己的私钥加密特征码,并附加在数据后面 4、Bob使用对称加密算法生成临时会话密钥加密特征码和数据 5、Bob用Alice的公钥加密临时会话密钥,并附加在数据后 接收方Alice:自下至上 1、Alice收到Bob数据,…

    Linux干货 2016-10-31
  • Linux 系统启动流程与grub的应用

    一 Linux 系统启动流程    POST –> BIOS(Boot Sequence) –> MBR(bootloader) –> kernel + initramfs(或initrd) –> rootfs (ro) –> /sbin/init …

    Linux干货 2016-03-07
  • 磁盘分区

    磁盘分区 一、分区方式两种 MBR分区不能超过2个T按柱面分区主引导记录存放在,0磁道0扇区 512bytes前446字节存放bootloader (软件程序)64字节:分区表16个字节表示一个有效的分区 故只能分4个主分区。大于4个需要创建扩展分区,并在其中创建逻辑分区。2字节 55AABPT分区GPT:GUID(Globals Unique Identi…

    Linux干货 2016-08-29
  • Linux程序包管理(一)RPM使用

    Linux程序包管理 在早期我们使用源代码的方式安装软件时,都需要先把源程序代码编译成可执行的二进制应用程序,然后进行安装。意味着每次安装软件都需要经过 预处理 –> 编译 –> 汇编–> 链接, 这个复杂的过程。为简化安装步骤,程序提供商就在特定的系统上面编译好相关程序的安装文件并进行打包,提…

    Linux干货 2016-06-01
  • 文件搜索者-find命令详解

    1. 文件查找:          在linux系统中由于文件的众多,往往需要在众多的文件当中查找某一个文件,如果时间一长,很难记得文件存放至何处,不过,这一点,你不比担心,因为开发人员为我们提供了强大的文件搜索工具,下面将介绍两款常用的文件查找工具locate,和find,这两…

    Linux干货 2016-08-15