shell-03

07if条件语句的语法和实战

语法:
实战:
01.监控web和数据库的企业案例

如何获取数据:

端口监控:netstat、ss、lsof
	本地:netstat -lnt | grep 3306 | awk -F ":" '{print $2}' | awk '{print $1}' | wc -l
        ss -lntup | grep mysql | wc -l
        lsof -i tcp:3306   | wc -l
        ss比netstat快的主要原因是:
        	netstat是遍历/proc下面每个PID目录,ss直接读/proc/net下面的统计信息。所以ss执行的时候消耗资源以及消耗的时间都比netstat少很多。
	远端:nmap 127.0.0.1 -p 3306 | grep open | wc -l
				echo -e "
" | telnet 127.0.0.1 3306 2>/dev/null  | grep -a Connected | wc -l
				nc #测试未成功
进程监控:ps -ef|grep nginx | wc -l
模拟用户访问:curl、wget;判断返回值、返回的内容、header等
	curl:curl -s -o /dev/null http://www.baidu.com;echo $?  
		-s:--slient
		-o:--output
	wget:wget --spider --timeout=10 --tries=2 www.baidu.com  &>/dev/null;echo $?
		--spider Wget will behave as a Web spider, which means that it will not download the pages, just check that they are there.
		--timeout Set the network timeout to seconds seconds.
		--tries=2 Set number of retries to number.
	
登陆mysql数据库进行判断:mysql -uroot -pcarrinet -e "select version()" &> /dev/null;echo $?

02.判断字符串是否为数字:
	1.expr $string +1 &>/dev/null;echo $?
	2.[ -z ${string//[0-9]/} ] && echo "int"  || echo "char"
	3.[ -z "`echo $string | sed 's/[0-9]//g'`" ]  && echo "int"  || echo "char"
	4.[ -n "$string" -a "${char}" = "${char//[^0-9]/}" ] && echo "int" || echo "char"
03.判断字符串长度是否为0:
	1.-z、-n
	2.${#char}
	3.expr length "$char"
	4.echo $char |wc -L
	5.echo "oldboy" | awk 'BEGIN{}{print length($0)}END{}'

  

原文地址:https://www.cnblogs.com/wanyp/p/7520664.html