dukejunior
——未来程式工作室

总结

方法

01、while循环语句:

while <条件表达式>;do
	指令...
done

02、until循环语句:

until <条件表达式>;do
	指令...
done

03、每隔2秒在屏幕上输出一次负载值:cat 10_1_1.sh

#!/bin/sh
while true;do
	uptime
	sleep 2
done

04、负载值追加到log:cat 10_1_2.sh

#!/bin/sh
while [ 1 ];do
	uptime >>/tmp/uptime.log
	usleep 2000000
done
----------
sh 10_1_2.sh &
tail -f /tmp/uptime.log

05、脚本运行

06、sh 10_1_2.sh &

07、fg

08、bg

09、jobs

10、fg1

11、kill %2

12、进程管理的相关命令:

kill、killall、pkill:杀掉进程。
ps:查看进程。
pstree:显示进程状态树。
top:显示进程。
renice:改变优先权。
nohup:用户退出系统之后继续工作。
pgrep:查找匹配条件的进程。
strace:跟踪一个进程的系统调用情况。
ltrace:跟踪进程调用库函数的情况。

13、猜数字:cat 10_4_1.sh

#!/bin/bash
total=0
export LANG="zh_CN.UTF-8"
NUM=$((RANDOM%61))
echo "当前苹果的价格是每斤$NUM元"
echo "========================"
usleep 1000000
clear
echo '这苹果多少钱一斤啊?
请猜0~60的数字'
apple(){
	read -p "请输入你的价格:" PRICE
	expr $PRICE + 1 &>/dev/null
	if [ $? -ne 0 ];then
		echo "别逗我了,快猜数字"
		apple
	fi
}
guess(){
	((total++))
	if [ $PRICE -eq $NUM ];then
		echo "猜对了,就是$NUM元"
		if [ $total -le 3 ];then
			echo "一共猜了$total次,太牛了。"
		elif [ $total -gt 3 -a $total -le 6 ];then
			echo "一共猜了$total次,次数有点多,加油啊。"
		elif [ $total -gt 6 ];then
			echo "一共猜了$total次,行不行,猜了这么多次"
		fi
			exit 0
	elif [ $PRICE -gt $NUM ];then
		echo "嘿嘿,要不你用这个价买?"
		echo "再给你一次机会,请继续猜:"
		apple
	elif [ $PRICE -lt $NUM ];then
		echo "太低太低"
		echo "再给你一次机会,请继续猜:"
		apple
	fi
}
main(){
	apple
	while true;do
		guess
	done
}
main

14、手机使用:cat 10_5_1.sh

#!/bin/sh
export LANG="zh_CN.UTF-8"
sum=15
msg_fee=15
msg_count=0
menu(){
	cat <<END
当前余额为${sum}分,每条短信需要${msg_fee}分
==========================
	1.充值
	2.发消息
	3.退出
==========================
END
}
recharge(){
	read -p "请输入金额充值:" money
	expr $money + 1 &>/dev/null
	if [ $ -ne 0 ];then
		echo "then money your input is error,must be int."
	else
		sum=$(($sum+$money))
		echo "当前余额为:$sum"
	fi
}
sendInfo(){
	if [ ${sum} -lt $msg_fee ];then
		printf "余额不足:$sum ,请充值。\n"
	else
		while true;do
			read -p "请输入短信内容(不能有空格):" msg
			sum=$(($sum-$msg-fee))
			printf "Send "$msg" successfully!\n"
			printf "当前余额为:$sum\n"
			if [ $sum -lt $msg_fee ];then
				printf "余额不足,剩余$sum分\n"
				return  1
			fi
		done
	fi
}
main(){
	while true;do
		menu
		read -p "请输入数字选择:" men
		case "$men" in
			1)
				recharge
				;;
			2)
				sendInfo
				;;
			3)
				exit 1
				;;
			*)
				printf "选择错误,必须是{1|2|3}\n"
		esac
	done
}
main

15、守护进程方式监控网站:cat 10_7_1.sh

#!/bin/sh
. /etc/init.d/functions
if [ $# -ne 1 ];then
	echo $"usage $0 url"
	exit 1
fi
while true;do
	if [ `curl -o /dev/null --connect-timeout 5 -s -w "%{http_code}"  $1| egrep -w "200|301|302"|wc -l` -ne 1 ];then
	action "$1 is error." /bin/false
	#echo "$1 is error."|mail -s "$1 is error." 31333741--@qq.com
	else
		action "$1 is ok" /bin/true
	fi
	sleep 10
done

16、Shell数组同时检测多个URL是否正常:cat 10_7_2.sh

#!/bin/bash
# this script is created by oldboy.
# e_mail:31333741@qq.com
# function:case example
# version:1.3
. /etc/init.d/functions
check_count=0
url_list=(
http://blog.oldboyedu.com
http://blog.etiantian.org
http://oldboy.blog.51cto.com
http://10.0.0.7
)
function wait(){
	echo -n '3秒后,执行检查URL操作.';
	for ((i=0;i<3;i++));do
		echo -n ".";sleep 1
	done
	echo
}
function check_url(){
	wait
	for ((i=0; i<`echo ${#url_list[*]}`; i++));do
		wget -o /dev/null -T 3 --tries=1 --spider ${url_list[$i]} >/dev/null 2>&1
		if [ $? -eq 0 ];then
			action "${url_list[$i]}" /bin/true
		else
			action "${url_list[$i]}" /bin/false
		fi
	done
	((check_count++))
}
main(){
	while true;do
		check_url
		echo "-------check count:${check_count}---------"
		sleep 10
	done
}
main

17、分析Apache访问日志(exec内置命令功能):cat 10_8_1.sh

#!/bin/bash
sum=0
exec <$1
while read line;do
	size=`echo $line|awk '{print $10}'`
	expr $size + 1 &>/dev/null
	if [ $? -ne 0 ];then
		continue
	fi
	((sum=sum+$size))
done
echo "${1}:total:${sum}bytes =`echo $((${sum}/1024))`KB"

18、while通过输入重定向读取文件:

while read line
do
    cmd
done

19、解决类DDos攻击:cat 10_10_3.sh

#!/bin/sh
file=$1
JudgeExt(){
	if expr "$1" : ".*\.log" &>/dev/null;then:

	else
		echo $"usage:$0 xxx.log"
		exit 1
	fi
}
IpCount(){
	grep "ESTABLISHED" $1|awk -F "[ :]+" '{ ++S[$(NF-3)]}END {for(key in S) print S[key], key}'|sort -rn -k1|head -5 >/tmp/tmp.log
}
ipt(){
	local ip=$1
	if [ `iptables -L -n|grep "$ip"|wc -l` -lt 1 ];then
		iptables -I INPUT -s $ip -j DROP
		echo "$line is dropped" >>/tmp/droplist_$(date +%F).log
	fi
}
main(){
	JudgeExt $file
	while true;do
		IpCount $file
		while read line;do
			ip=`echo $line|awk '{print $2}'`
			count=`echo $line|awk '{print $1}'`
			if [ $count -gt 3 ];then
				ipt $ip
			fi
		done
	done
}
main
----------
bash 10_10_3.sh access.log
posted on 2020-05-03 16:13  公爵二世  阅读(269)  评论(0)    收藏  举报