优秀的DEMO
1. portmap脚本
1 #! /bin/sh 2 # 3 # portmap Start/Stop RPC portmapper 4 # 5 # chkconfig: 345 13 87 6 # description: The portmapper manages RPC connections, which are used by \ 7 # protocols such as NFS and NIS. The portmap server must be \ 8 # running on machines which act as servers for protocols which \ 9 # make use of the RPC mechanism. 10 # processname: portmap 11 12 13 # This is an interactive program, we need the current locale 14 [ -f /etc/profile.d/lang.sh ] && . /etc/profile.d/lang.sh 15 # We can't Japanese on normal console at boot time, so force LANG=C. 16 if [ "$LANG" = "ja" -o "$LANG" = "ja_JP.eucJP" ]; then 17 if [ "$TERM" = "linux" ] ; then 18 LANG=C 19 fi 20 fi 21 22 # Source function library. 23 . /etc/init.d/functions 24 25 # Get config. 26 if [ -f /etc/sysconfig/network ]; then 27 . /etc/sysconfig/network 28 else 29 echo $"Networking not configured - exiting" 30 ...skipping... 31 stop 32 start 33 pmap_set < /var/run/portmap.state 34 rm -f /var/run/portmap.state 35 } 36 37 # See how we were called. 38 case "$1" in 39 start) 40 start 41 ;; 42 stop) 43 stop 44 ;; 45 status) 46 status portmap 47 ;; 48 restart|reload) 49 restart 50 ;; 51 condrestart) 52 [ -f /var/lock/subsys/portmap ] && restart || : 53 ;; 54 *) 55 echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}" 56 exit 1 57 esac
FLT的DEMO
1. 清除日志
1 #!/bin/sh 2 #clear log file 3 LOG_DIR=/var/log 4 ROOT_UID=0 5 #ROOT用户执行 6 if [ "$UID" -ne ROOT_UID ] 7 then 8 echo 'Must be root to run this script' 9 exit 1 10 fi 11 cd $LOG_DIR ||{ 12 echo "Cannot change to $LOG_DIR " >/dev/null 13 exit 1 14 } 15 cat /dev/null > messages 16 echo "Logs cleaned up" 17 exit 0
2. 利用shell数组函数检查多个url地址的案例
#!/bin/sh #use array by 20170911 #use function . /etc/init.d/functions array=( www.baidu.com www.hao123.com www.51cto.com ) function wait(){ echo -n "3mins to start the script..." for((i=0;i<${#array[*]};i++)) do echo -n '.'; sleep 1 done } function check_url(){ for ((i=0;i<${#array[*]};i++)) do judge=(`curl -I ${array[$i]} -s |head -1`) #echo "${array[$i]} ------------> ${judge[2]}" if [ "${judge[1]}"=="200" ] && [ "${judge[2]}"=="OK" ];then action "${array[$i]} ------------> ${judge[2]}" /bin/true else action "${array[$i]} ------------> ${judge[2]}" /bin/false fi done } wait echo -e "\n" check_url
3. 数组的学习
1 #!/bin/sh 2 #use array by 20170911 3 array=( 4 hello 5 world 6 2017 7 I 8 hava 9 a 10 dream) 11 arr=(`ls /home/omc/*.sh`) 12 echo "---------------Array---------------------" 13 for ((i=0;i<=${#array[*]};i++)) 14 do 15 echo "this is $i-------------------->${array[$i]}" 16 done 17 echo "------------------------------------" 18 echo "total : ${#array[*]}" 19 echo "---------------Ls *.sh--------------------" 20 for((i=0;i<${#arr[*]};i++)) 21 do 22 echo "this is $i -------------------> ${arr[$i]}" 23 done 24 echo "------------------------------------" 25 echo "total : ${#arr[*]}"
4. 监控WEB的URL
1 [root@lnmp01 scripts]# cat check_web.sh 2 #!/bin/sh 3 ######################################################## 4 RETVAL=0 5 SCRIPT_PATH="/root/scripts" 6 MAIL_GROUP="[email protected] [email protected]" 7 LOG_FILE="/tmp/web_check.log" 8 FAIL_COUNT=0 9 function getUrlState(){ 10 for (( i = 0; i < 10; i++ )); do 11 wget -T 10 --tries=1 --spider http://${URL} &>/dev/null 12 [ $? -ne 0 ] && let ${FAIL_COUNT}+=1 13 done 14 15 if [ ${FAIL_COUNT} -gt 1 ];then 16 RETVAL=1 17 NowTime=`date +%m-%d %H:%M:%S` 18 TITLE="http://${URL} sevice is error,${NowTime}" 19 echo "send to :${MAIL_USER},Title :${TITLE}" >>${LOG_FILE} 20 for MAIL_USER in `$MAIL_GROUP` 21 do 22 mail -s ${TITLE} ${MAIL_USER} <${LOG_FILE} 23 done 24 else 25 RETVAL=0 26 fi 27 return "$RETVAL" 28 } 29 30 [ ! -d "$SCRIPT_PATH" ] && { 31 mkdir -p "$SCRIPT_PATH" 32 } 33 34 35 [ ! -f "$SCRIPT_PATH/domain.list" ] && { 36 cat > "$SCRIPT_PATH/domain.list" << EOF 37 www.baidu.com 38 www.51cto.com 39 www.http:www.hao123.com 40 EOF 41 } 42 43 for URL in `cat $SCRIPT_PATH/domain.list` 44 do 45 echo -n "checking $URL..." 46 getUrlState $URL && echo OK || echo no 47 done 48 [root@lnmp01 scripts]#
5. for循环的使用
1 方案一:计算某个access_log文件的访问总次数: 2 #!/bin/sh 3 num=`awk '{print $8}' $1 |grep -v eth0|awk -F '.' '{print $4}'` 4 sum=0 5 for line in $num 6 do 7 [ -n $line ] || continue 8 ((sum+=$line)) 9 done 10 echo "Num is $sum" 11 12 13 问题二:打印文件夹 14 ls -F|grep / 15 16 问题三:打印文件夹 17 #!/bin/sh 18 openvpn="" 19 openvpn_locations="/usr/bin/openvpn /usr/local/sbin/openvpn" 20 for location in $openvpn_locations 21 do 22 [ -f $location ] && openvpn=$location 23 done 24 25 26 问题四:打印9*9 27 28 #!/bin/sh 29 for a in `seq 1 9` 30 do 31 for b in `seq 1 9 ` 32 do 33 if [ $a -ge $b ];then 34 echo -en "$a * $b = $(expr $a \* $b)" 35 fi 36 done 37 echo " " 38 done 39 40 问题5:编写连续IP的添加 41 #!/bin/sh 42 43 print_usage(){ 44 echo "USAGE: $0 {up/down}" 45 exit 1 46 } 47 48 [ $# -ne 1 ] && print_usage 49 50 ip_conf(){ 51 for((i=1;i<=16;i++)) 52 do 53 if [ $i -ne 10 ];then 54 ifconfig eth0:$i 192.168.25.$i netmask 255.255.255.0 $1 55 else 56 continue 57 fi 58 done 59 } 60 61 case $1 in 62 up) 63 ip_conf;; 64 down) 65 ip_conf;; 66 *) 67 echo "something wrong..." 68 esac
6. while循环的使用
1 ------------------------------------------------------------------------------------------ 2 方案一: 3 #!/bin/sh 4 #cal 1 + 2 + 3 +...+100 5 i=10 6 while ((i>0)) 7 do 8 echo $i 9 ((i--)) 10 done 11 ------------------------------------------------------------------------------------------ 12 方案二: 13 #!/bin/sh 14 #cal 1 + 2 + 3 +...+100 15 i=10 16 while [[ $i > 0]] 17 do 18 echo $i 19 ((i--)) 20 done 21 22 ------------------------------------------------------------------------------------------ 23 方案三: 24 #!/bin/sh 25 #cal 1 + 2 + 3 +...+100 26 i=10 27 while ((i--)) 28 do 29 echo $i 30 done 31 32 33 ------------------------------------------------------------------------------------------ 34 方案四: 35 #!/bin/sh 36 #cal 1 + 2 + 3 +...+100 37 i=10 38 while [ $i -gt 0] 39 do 40 echo $i 41 ((i--)) 42 done
7. 计算1+2+3+...+100
1 方案1:for循环结构及(())计算式shell脚本 2 #!/bin/sh 3 #cal 1 + 2 + 3 +...+100 4 j=0 5 for ((i=0;i<=100;i++)) 6 do 7 ((j+=i)) 8 done 9 10 echo "1 + 2 + 3 +...+100="$j 11 12 --------------------------------------------------------------------------------------- 13 方案二:使用seq命令加for循环语法及let命令计算式脚本 14 #!/bin/sh 15 #cal 1 + 2 + 3 +...+100 16 sum=0 17 for i in `seq 100` 18 do 19 let sum+=i; 20 done 21 echo "1 + 2 + 3 +...+100="$sum 22 23 --------------------------------------------------------------------------------------- 24 方案三:seq、tr、sed、bc联手完成计算的方法 25 seq 100 | tr '\n' '+'|sed 's#\+$#\n#g'|bc ==>拼接的字符串表达式结尾加号替换为换行(回车)符。 26 --------------------------------------------------------------------------------------- 27 方案四: 28 #!/bin/sh 29 i=1 30 while ((i <=100 )) 31 do 32 ((j=j+i)) 33 ((i++)) 34 done 35 echo $j 36 37 --------------------------------------------------------------------------------------- 38 方案五: 39 seq 100 |awk '{total+=$1} END {print total}'
8. case语句联系
1 案例一: 纯case联系 2 #!/bin/sh 3 read -p "Please input the number of two:" num 4 case $num in 5 "1" ) 6 echo "the number you put is 1" ;; 7 "2") 8 echo "the number you put is 2";; 9 [3-9]) 10 echo "the number you put is $num";; 11 *) 12 echo "something must be wrong ..."; 13 esac 14 15 16 案例二:case-if联系 17 #!/bin/sh 18 read -p "Please input the number of two:" num 19 if [ $num -eq 1 ];then 20 echo "1" 21 elif [ $num -eq 2 ];then 22 echo "2" 23 elif [ $num -eq 3 ];then 24 echo "3" 25 else 26 echo "$num" 27 fi 28 29 案例三: 30 #/bin/sh 31 read -p "Please input the number of two:" fruit 32 case "$fruit" in 33 apple|APPLE) 34 echo -e "Apple";;
8.1 if语句
1 #!/bin/sh 2 #Author: ftl 3 #Date: 20170908 4 5 print_usage(){ 6 printf "please input 2 numbers:\n" 7 echo -e "$0 num1 num2" 8 exit 1 9 } 10 11 #judge num 12 if [ $# -ne 2 ] 13 then 14 print_usage 15 fi 16 17 18 #judge num 19 [ -n "`echo $1|sed 's/[0-9]//g' `" -a -n "`echo $2|sed 's/[0-9]//g'`" ] &&\ 20 { 21 echo "$1 $2 must be number" ; 22 exit 1 23 } 24 25 #judge body 26 if [ $1 -gt $2 ] 27 then 28 echo "$1 > $2" 29 elif [ $1 -eq $2 ] 30 then 31 echo "$1 = $2" 32 else 33 echo "$1 < $2" 34 fi
9. nginx测试demo
View Code
10. 远程端口监控
View Code
11. Mysql服务监控
1 生成环境监控Mysqsl 2 方案1:过滤3306端口,查看是否启动正常 3 ps -ef|grep 3306 |grep -v grep -->不建议,因为不太准,有的vi也是有显示结果的 4 netstat -lnput|grep 3306 5 netstat -lnput|grep 3306|wc -l -->最常用的,如果为1,则表示开启,然后判读是否为1 if [ $port -eq 1 ] 6 7 方案2;Mysql端口和进程同时存在,则服务正常 8 PORT=`netstat -lnput|grep 3306|wc -l'` 9 PROCESS=`ps -ef|grep mysqld|grep -v grep|wc -l` 10 11 方案1: 12 #!/bin/sh 13 #Author: ftl 14 #Mysql Monitor 15 16 PORT=`netstat -lnput|grep 3306|awk -F '[ :]+' '{print $5}'` -->思路不是最佳 17 if [ $PORT -eq 3306 ];then -->最还用字符串判断 if [ "$PORT" == "3306" ] 18 echo "Mysql is Running ...." 19 else 20 service mysqld start 21 fi 22 23 24 方案2: 25 #!/bin/sh 26 #Author: ftl 27 #Mysql Monitor 28 29 PORT=`netstat -lnput|grep 3306|wc -l` 30 PROCESS=`ps -ef|grep mysqld|grep -v grep|wc -l` 31 #if [ $PORT -eq 1 -a $PROCESS -eq 1 ];then -->相等的 32 if [ $PORT -eq 1 ] && [ $PROCESS -eq 2 ];then 33 echo "Mysql is Running ...." 34 else 35 service mysqld start 36 fi 37 38 39 实际的解决: 40 #!/bin/sh 41 #Author: ftl 42 #Mysql Monitor 43 44 PORT=`netstat -lnput|grep 3306|wc -l` 45 PROCESS=`ps -ef|grep mysqld|grep -v grep|wc -l` 46 #if [ $PORT -eq 1 -a $PROCESS -eq 1 ];then -->相等的 47 if [ $PORT -eq 1 ] && [ $PROCESS -eq 2 ];then 48 echo "Mysql is Running ...." 49 else 50 service mysqld start 51 sleep 10 52 PORT=`netstat -lnput|grep 3306|wc -l` 53 PROCESS=`ps -ef|grep mysqld|grep -v grep|wc -l` 54 if [ $PORT -eq 1 ] && [ $PROCESS -eq 2 ];then 55 echo "Mysql is Running ...." 56 else 57 while true 58 do 59 pkill mysqld >/dev/null 2>&1 60 sleep 1 61 [ $? -ne 0 ] && break 62 done 63 fi 64 service mysqld start && echo 'mysql is running....' 65 fi 66 67 68 69 方案3:模拟web服务器,根据mysql账户进行连接,然后根据返回在状态判断mysql是否启动 70 mysql -uroot -proot -h localhost -e "select version()"; -->查看mysql版本 71 #!/bin/sh 72 #Author: ftl 73 #Mysql Monitor 74 75 MYSQL_VERSION=` mysql -uroot -proot -e "select version()" >/dev/null` 76 if [ $? -eq 0 ];then 77 echo "Mysql is Running ...." 78 else 79 service mysqld start 80 fi 81 82 83 方案4:更专业的写法 84 用变量定义路径, 85 执行的脚本前面加上x判断 [ -x $MYSQL_SHELL ] >$LOG_DIR 86 mysql -uroot -proot -h localhost -e "select version()"; -->注意-h,用于远端的监控 87 88 方案5:最佳的,利用php/java来进行监控 89 90 <?php 91 $link_id=mysql_connect('db_etiantian','bbs','root') or mysql_error(); 92 if($link_id){ 93 echo "mysql is Ok ,Congratulation"; 94 }else{ 95 echo "Sorry,you can see logs of mysql"; 96 echo mysql_error(); 97 } 98 ?> 99
12. 菜单联系
1 #!/bin/sh 2 # menu list for ftl by ftl 20170906 3 4 menu(){ 5 cat <<END 6 1.[install lamp] 7 2.[install lnmp] 8 3.[install mysql] 9 4.[install nfs] 10 0.[exit] 11 END 12 } 13 menu 14 read a 15 echo "you choose $a"
13. epxr小技巧
1 1.判断扩展名: 2 3 if expr "hello.txt" : ".*\.txt" -->判断文件拓展名是否为.txt 注意空格 4 ->为真,则输出 9 显示匹配后的字符数,包括.txt 5 ->为假,则输出非 0 6 7 8 2.判断是否是整数 9 read -p "Please input" 10 expr $a + 0 &>/dev/null 11 [ $? -eq 0 ] && echo int || echo char
14. let监听服务
View Code
15. 用source解析命令的使用
1 [root@lamp01 omc]# echo 'dir=`date +%F`'>qq.sh 2 [root@lamp01 omc]# sh qq.sh 3 [root@lamp01 omc]# echo $dir 4 5 [root@lamp01 omc]# bash qq.sh 6 [root@lamp01 omc]# echo $dir 7 8 [root@lamp01 omc]# source qq.sh 9 [root@lamp01 omc]# echo $dir 10 2017-09-05 11 [root@lamp01 omc]# 12 说明:source将原来shell里面的东西作为结果传递给当前的shell去显示