@mrz1
2017-12-23T06:02:20.000000Z
字数 6877
阅读 1755
shell脚本
[root@centos7 ~]#vim f1.sh#! /bin/bash# ------------------------------------------# Filename: systeminfo.sh# Revision: 1.0# Date: 2017-11-24# Author: Zhang Qi Fei# Email: 1353250703@qq.com# Website: www.zhangqifei.top# Description: This is the first script# ------------------------------------------echo Hostname: `hostname`echo IP address: `ifconfig|egrep -o "[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}"|head -n1`echo System version: `cat /etc/system-release`echo kernel version: `uname -r`echo `lscpu | grep "Model name:" |tr -s ' ' | cut -d: -f2`echo `cat /proc/meminfo | grep MemTotal`echo `fdisk -l|grep Disk|head -n1`
运行结果:
Hostname: centos7IP address: 172.17.108.3System version: CentOS release 6.8 (Final)kernel version: 2.6.32-696.6.3.el6.x86_64Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHzMemTotal: 2054212 kBDisk /dev/vda: 42.9 GB, 42949672960 bytes
[root@centos7 ~]#vim bachup.sh# ------------------------------------------#! /bin/bash...... //省略# ------------------------------------------cp -arv /etc /root/etc`date +%F`
#! /bin/bash# ------------------------------------------# Filename: disk.sh...... //省略# ------------------------------------------echo "The max using rate is:`df |grep "/dev/sd"| cut -c 44-46 | sort -nr | head -1`%"
运行结果:
[root@centos7 ~]#./disk.shThe max using rate is:%
#! /bin/bash# ------------------------------------------# Filename: links.sh...... //省略# ------------------------------------------echo "the links number is:"netstat -nt |tr -s ' ' |cut -d ' ' -f5 |cut -d: -f1 |grep [0-9]|sort |uniq -c|sort-nr
运行结果:
[root@centos7 ~]#./links.shthe links number is:3 123.118.29.61 140.205.140.2051 106.11.68.13
#! /bin/bash# ------------------------------------------# Filename: links.sh...... //省略# ------------------------------------------echo 'The sum is'id1=`cat /etc/passwd | sed -n '10p' | cut -d: -f3`id2=`cat /etc/passwd | sed -n '20p' | cut -d: -f3`let idsum=$id1+$id2echo "The sum is:$idsum"unset idsum
运行结果:
[root@centos7 ~]#./sumid.shThe sum isThe sum is:48
#! /bin/bash# ------------------------------------------# Filename: sumspace.sh...... //省略# ------------------------------------------file1_blank=`grep "^[[:space:]]*$" $1 | wc -l`file2_blank=`grep "^[[:space:]]*$" $2 | wc -l`sumspace=$[ $file1_blank + $file2_blank ]echo "The tatal blank line: $sumspace"
运行结果:
[root@centos7 ~]#./sumspace.sh /etc/passwd /etc/rwtabThe tatal blank line: 2
#! /bin/bash# ------------------------------------------# Filename: sumfile.sh...... //省略# ------------------------------------------etcnum=`ls -d /etc/*|wc -l`varnum=`ls -d /var/*|wc -l`usrnum=`ls -d /usr/*|wc -l`echo "the totalfile is $[etcnum+varnum+usrnum]"
运行结果:
[root@centos7 ~]#./sumfile.shthe totalfile is 192
参考1:
#! /bin/bash# ------------------------------------------# Filename: argsnum.sh...... //省略# ------------------------------------------read -p "Enter a filename:" file[ -z "$file" ] && echo "please enter a file name" && exit 2echo -n "The blank line number is:"echo `grep -c ‘^[[:space:]]*$‘ $file`
运行结果:
[root@centos7 ~]#./argsnum.shEnter a filename:/etc/passwdThe blank line number is:0
参考2:
#! /bin/bash# ------------------------------------------# Filename: $1......省略# ------------------------------------------[ $# -lt 1 ] && echo "至少一个参数" && exit 20[ -e $1 ] && echo `grep -c '^$' $1` || echo "没有这样的文件或目录?"
执行结果:
[root@centos7 ~]# ./srgsnum.sh /etc/passwd0[root@centos7 ~]# ./srgsnum.sh至少一个参数
#! /bin/bash# ------------------------------------------# Filename: houstping.sh...... //省略# ------------------------------------------[ $# -ne 1 ] && echo "please input one ipaddr" && exit 2ping -c1 -w1 $1 &> /dev/null && echo "ping successful" || echo "ping failed"
运行结果:
[root@centos7 ~]#./houstping.sh 47.93.96.256ping failed[root@centos7 ~]#./houstping.sh 47.93.96.29ping successful
参考1:
#! /bin/bash# ------------------------------------------# Filename: dist.sh...... //省略# ------------------------------------------dist=`(df -i /dev/sd*;df /dev/sd*)|egrep -o '[0-9]+%' |egrep -o '[0-9]+'|sort -nr| head -1`[ "$dist" -gt 10 ] && wall "disk will be full"
运行结果:
[root@centos7 ~]#./dist.shBroadcast message from root@centos7.qifei.com (pts/1) (Sun Nov 26 13:33:05 2017):disk will be full
参考2:
#! /bin/bash# ------------------------------------------# Filename: dist.sh...... //省略# ------------------------------------------dist=`df |grep "/dev/sd" |grep -o '[0-9]*%' |grep -o '[0-9]*'|sort -nr| head -1`inode=`df -i |grep "/dev/sd" |egrep -o '[0-9]*%' |egrep -o '[0-9]*'|sort -nr| head -1`sum=80[ "$dist" -ge "$sum" ] && echo -e "磁盘空间占用率超过"$sum"\a `wall 'The disk space will be full' `"||echo ">磁盘利用率为"$dist",可以使用"[ "$inode" -ge "$sum" ] && echo -e "inode占用率超过"$sum"\a `wall 'The disk space will be full'`" ||echo "inode利用率为"$inode",可以使用"
运行结果:
[root@centos7 ~]#./dist.sh磁盘利用率为16,可以使用inode利用率为1,可以使用
参考3:
#! /bin/bash# ------------------------------------------# Filename: warning.sh...... //省略# ------------------------------------------echo `df |grep "/dev/sd" |grep -o "\<[[:digit:]]\{1,3\}%" |grep -o "[[:digit:]]\{1,3\}" |sort -nr`
运行结果:
[root@centos7 ~]#./warning.sh16 8 1
#! /bin/bash# ------------------------------------------# Filename: f1.sh...... //省略# ------------------------------------------read -p "please input one useful ip:" ip_addrecho $ip_addr | grep -E "^(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>$" &> /dev/null && echo "this is a useful ip" || echo "thisis not a useful ip"
运行结果:
[root@centos7 ~]#./f1.shplease input one useful ip:12.23.56.32this is a useful ip[root@centos7 ~]#./f1.shplease input one useful ip:256.235.233.213this is not a useful ip
cdnet='cd /etc/sysconfig/network-scripts/'editnet='vim /etc/sysconfig/network-scripts/ifcfg-eth0'editnet='vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或 ifcfg-ens33 ' (如果系统是CentOS7)
ls /proc/$$/fd |wc -l
#! /bin/bash# ------------------------------------------# Filename: user.sh...... //省略# ------------------------------------------[ -z "$1" ] && read -p "please input a username: " usernameid $username &> /dev/null && echo "$username is exist" && exit 10useradd $username && echo "$username is created"
运行结果:
[root@centos7 ~]#bash nologin.shplease input a username: zhang1zhang1 is created[root@centos7 ~]#bash nologin.shplease input a username: zhang1zhang1 is exist