linux上shell脚本,30个LinuxShell脚本经典案例(上)
在学习Linux运维时,普遍反馈是:Linux Shell是⼀个很难的知识板块。虽然⼤家都认真学,基本的语法也都掌握了,但有需求时,很难直接上⼿编程,要么写了很久,要么写不好!
也有很多做运维很多年的朋友也是如此,Shell脚本⼀直写的不6!在⽹上看例⼦能照猫画虎写出来,完全独⽴写就困难了。对于初学者⽽⾔,因为没有实战经验,写不出来Shell脚本很正常,如果⼯作了⼏年的运维⽼年还是写不出来,那就是没主动需求,缺乏练习,缺乏经验。
针对以上问题,总结了30个⽣产环境中经典的Shell脚本,通过这些需求案例,希望能帮助⼤家提升Shell编写思路,掌握编写技巧。
先了解下编写Shell过程中注**意事项:**
开头加解释器:#!/bin/bash
语法缩进,使⽤四个空格;多加注释说明。
命名建议规则:变量名⼤写、局部变量⼩写,函数名⼩写,名字体现出实际作⽤。
默认变量是全局的,在函数中变量local指定为局部变量,避免污染其他作⽤域。
有两个命令能帮助我调试脚本:set -e 遇到执⾏⾮0时退出脚本,set-x 打印执⾏过程。
写脚本⼀定先测试再到⽣产上。
1、获取随机字符串或数字
获取随机8位字符串:
⽅法1:
echo $RANDOM |md5sum |cut -c 1-8
471b94f2
⽅法2:
openssl rand -base64 4
vg3BEg==
⽅法3:
cat /proc/sys/kernel/random/uuid |cut -c 1-8
ed9e032c
获取随机8位数字:
⽅法1:
echo $RANDOM |cksum |cut -c 1-8
23648321
⽅法2:
openssl rand -base64 4 |cksum |cut -c 1-8
38571131
⽅法3:
date +%N |cut -c 1-8
69024815
cksum:打印CRC效验和统计字节
2、定义⼀个颜⾊输出字符串函数
⽅法1:
function echo_color() {
if [ $1 == "green" ]; then
echo -e "33[32;40m$233[0m"
elif [ $1 == "red" ]; then
echo -e "33[31;40m$233[0m"
fi
}
⽅法2:
function echo_color() {
case $1 in
green)
echo -e "[32;40m$2[0m"
;;
red)
echo -e "[31;40m$2[0m"
;;
*)
echo "Example: echo_color red string" esac
}
使⽤⽅法:echo_color green "test" function关键字定义⼀个函数,可加或不加。
3、批量创建⽤户
!/bin/bash
DATE=$(date +%F_%T)
USER_
echo_color(){
if [ $1 == "green" ]; then
echo -e "[32;40m$2[0m"
elif [ $1 == "red" ]; then
echo -e "[31;40m$2[0m"
}
如果⽤户⽂件存在并且⼤⼩⼤于0就备份
if [ -s $USER_FILE ]; then
mv $USER_FILE ${USER_FILE}-${DATE}.bak
echo_color green "$USER_FILE exist, rename ${USER_FILE}-${DATE}.bak" fi
echo -e "User Password" >> $USER_FILE
echo "----------------" >> $USER_FILE
for USER in user{1..10}; do
if ! id $USER &>/dev/null; then
PASS=$(echo $RANDOM |md5sum |cut -c 1-8)
useradd $USER
echo $PASS |passwd --stdin $USER &>/dev/null
echo -e "$USER $PASS" >> $USER_FILE
echo "$USER User create successful."
else
echo_color red "$USER User already exists!"
fi
done
4、检查软件包是否安装
!/bin/bash
if rpm -q sysstat &>/dev/null; then
echo "sysstat is already installed."
else
echo "sysstat is not installed!"
fi
5、检查服务状态
!/bin/bash
PORT_C=$(ss -anu |grep -c 123)
PS_C=$(ps -ef |grep ntpd |grep -vc grep)
if [ $PORT_C -eq 0 -o $PS_C -eq 0 ]; then
echo "内容" | mail -s "主题" dst@example
fi
6、检查主机存活状态
⽅法1:将错误IP放到数组⾥⾯判断是否ping失败三次
!/bin/bash
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in $IP_LIST; do
NUM=1
while [ $NUM -le 3 ]; do
if ping -c 1 $IP > /dev/null; then
echo "$IP Ping is successful."
break
else
echo "$IP Ping is failure $NUM"
FAIL_COUNT[$NUM]=$IP
let NUM++
fi
done
if [ ${#FAILCOUNT[]} -eq 3 ];then
echo "${FAIL_COUNT[1]} Ping is failure!"
unset FAILCOUNT[]
fi
done
⽅法2:将错误次数放到FAIL_COUNT变量⾥⾯判断是否ping失败三次!/bin/bash
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in $IP_LIST; do
FAIL_COUNT=0
for ((i=1;i<=3;i++)); do
if ping -c 1 $IP >/dev/null; then
echo "$IP Ping is successful."
break
else
echo "$IP Ping is failure $i"
let FAIL_COUNT++
done
if [ $FAIL_COUNT -eq 3 ]; then
echo "$IP Ping is failure!"
fi
done
⽅法3:利⽤for循环将ping通就跳出循环继续,如果不跳出就会⾛到打印ping失败!/bin/bash
ping_success_status() {
if ping -c 1 $IP >/dev/null; then
echo "$IP Ping is successful."
linuxshell脚本怎么运行continue
fi
}
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in $IP_LIST; do
ping_success_status
ping_success_status
ping_success_status
echo "$IP Ping is failure!"
done
****
7、监控CPU、内存和硬盘利⽤率
1)CPU
借助vmstat⼯具来分析CPU统计信息。
!/bin/bash
DATE=$(date +%F" "%H:%M)
IP=$(ifconfig eth0 |awk -F [ :]+ /inet addr/{print $4} ) # 只⽀持CentOS6 MAIL="example@mail"
if ! which vmstat &>/dev/null; then
echo "vmstat command no found, Please install procps package."
exit 1
fi
US=$(vmstat |awk NR==3{print $13} )
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论