初窥门径之脚本书写

一、几个脚本的编写:

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

相关推荐

  • 实验:配置静态路由2

    实验:配置静态路由2 IP地址规划如下: VMware实验环境下给网卡分配不同的VMnet以实现物理分隔广播域: Client1-VMnet1: R1-VMnet1-VMnet2: R2-VMnet2-VMnet3: R3-VMnet3-VMnet4: Client2-VMnet4: 注意,环境准备: 1、MAC地址不要有冲突,如果是复制的虚拟机,对于cen…

    2017-03-26
  • 笔记整理:权限管理1-基础权限管理&默认权限

    权限管理: 权限的分配根据owner和group来进行分配的   对于文件,各个权限的意义: r:可以使用工具查看内容 w:往里写 x:运行,提请内核发起一个进程 对于目录,各个权限的意义: r:用ls 查看目录列表   w:可以创建或删除目录中的文件   x:可以使用ls -l查看文件列表,也可cd进去   管理命令…

    Linux干货 2016-08-05
  • rmp 程 序 包 管 理 介 绍-20160819

    rmp 程 序 包 管 理 介 绍 §·学习大纲 ※包管理器 ※rmp常用命令介绍 *安装 *升级: *卸载: *查询: *校验: *数据库: ※rmp使用实例介绍 ※包管理器 ◎二进制应用程序的组成部分: 二进制文件、库文件、配置文件、帮助文件 ◎程序包管理器: debian:deb文件, dpkg包管理器 redhat: rpm文件, rpm包管理器 r…

    Linux干货 2016-08-21
  • shell脚本编程之循环语句

      在运行脚本时重复执行一系列的命令是很常见的,这时我们就需要使用循环语句来达到这个目的。 一、for命令   格式:for 变量 in 列表;do          循环体       done   for命令会遍历列表中的每一个值,并且在遍历完成…

    Linux干货 2016-01-03
  • 进程管理常用命令

    Linux系统上的进程查看及管理工具:pstree,ps,pidof,pgrep,top,htop,glances,pmap,vmstat,dstat,kill,pkill,job,bg,fg,nohup,nice,renice,killall,…CentOS 5 :SysV initCentOS 6 :upstartCentOS 7 :syst…

    Linux干货 2017-05-17
  • bash特性、bash编程

    bash基础特性: 命令行展开:~,{} 命令别名:alias,unalias 命令历史:history 命令和路径补全:$PATH glob通配符:*,?,[],[^], 快捷键:Ctrl+{a,e,l,c,u,k} 命令hash:   bash通配符及特殊符号: 通配符: ?:任意一个字符; *:匹配任意个任意字符; []:匹配括号内的任意一个…

    Linux干货 2018-03-21