@mrz1
2017-12-23T06:02:20.000000Z
字数 6877
阅读 1658
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: centos7
IP address: 172.17.108.3
System version: CentOS release 6.8 (Final)
kernel version: 2.6.32-696.6.3.el6.x86_64
Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz
MemTotal: 2054212 kB
Disk /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.sh
The 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.sh
the links number is:
3 123.118.29.6
1 140.205.140.205
1 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+$id2
echo "The sum is:$idsum"
unset idsum
运行结果:
[root@centos7 ~]#./sumid.sh
The sum is
The 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/rwtab
The 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.sh
the totalfile is 192
参考1:
#! /bin/bash
# ------------------------------------------
# Filename: argsnum.sh
...... //省略
# ------------------------------------------
read -p "Enter a filename:" file
[ -z "$file" ] && echo "please enter a file name" && exit 2
echo -n "The blank line number is:"
echo `grep -c ‘^[[:space:]]*$‘ $file`
运行结果:
[root@centos7 ~]#./argsnum.sh
Enter a filename:/etc/passwd
The 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/passwd
0
[root@centos7 ~]# ./srgsnum.sh
至少一个参数
#! /bin/bash
# ------------------------------------------
# Filename: houstping.sh
...... //省略
# ------------------------------------------
[ $# -ne 1 ] && echo "please input one ipaddr" && exit 2
ping -c1 -w1 $1 &> /dev/null && echo "ping successful" || echo "ping failed"
运行结果:
[root@centos7 ~]#./houstping.sh 47.93.96.256
ping failed
[root@centos7 ~]#./houstping.sh 47.93.96.29
ping 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| hea
d -1`
[ "$dist" -gt 10 ] && wall "disk will be full"
运行结果:
[root@centos7 ~]#./dist.sh
Broadcast 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 "ino
de利用率为"$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.sh
16 8 1
#! /bin/bash
# ------------------------------------------
# Filename: f1.sh
...... //省略
# ------------------------------------------
read -p "please input one useful ip:" ip_addr
echo $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 "this
is not a useful ip"
运行结果:
[root@centos7 ~]#./f1.sh
please input one useful ip:12.23.56.32
this is a useful ip
[root@centos7 ~]#./f1.sh
please input one useful ip:256.235.233.213
this 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: " username
id $username &> /dev/null && echo "$username is exist" && exit 10
useradd $username && echo "$username is created"
运行结果:
[root@centos7 ~]#bash nologin.sh
please input a username: zhang1
zhang1 is created
[root@centos7 ~]#bash nologin.sh
please input a username: zhang1
zhang1 is exist