企业实用脚本16
企业案例:写网络服务独立进程模式下rsync的系统启动脚本
例如:/etc/init.d/rsyncd{start|stop|restart} 。
要求:
1.要使用系统函数库技巧。
2.要用函数,不能一坨SHI的方式。
3.可被chkconfig管理
#!/bin/bash [ -f /etc/init.d/functions ]&& . /etc/init.d/functions||exit 2 Start(){ if [ `lsof -i:873|wc -l` -gt 0 ];then echo "rsync is running..." else rsync --daemon [ `lsof -i:873|wc -l` -gt 0 ]&&action "rsync start..." /bin/true fi return $RETVAL } Stop(){ if [ `lsof -i:873|wc -l` -eq 0 ];then echo "rsync is not running..." else killall rsync sleep 3 [ `lsof -i:873|wc -l` -eq 0 ]&&action "rsync stop..." /bin/true fi return $RETVAL } main(){ case "$1" in start|sTART) Start ;; stop|STOP) Stop ;; restart|RESTART) Stop sleep 2 Start ;; *) echo -e "USAGE:$0 {start|stop|restart}" exit 1 esac } main $1 exit $RETVAL
企业实用脚本17
1、执行脚本后,输入用户英文名字全拼,产生随机数01-99之间的数字,前面已经出现的数字,下次不能再出现
2、第一个输入名字后,屏幕输出信息,并将名字和数字记录到文件里,程序不能退出继续等待输入。
#!/bin/bash Name(){ while true do read -p "Input you name:" a [ ! -z "$a" ]&&break done } check_num(){ exec < random_num.txt while read line do A=`echo $line|awk ‘{print $2}‘` [ "$num" = "$A" ]&&result=1&&break done result=0 } random_num(){ for((i=1;i<5;i++)) do Name num=`echo $RANDOM|cut -c 1-2` [ `cat random_num.txt|grep $num|wc -l` -gt 0 ]&&result=1||result=0 #check_num #echo $result [ $result -eq 1 ]&&continue echo "you name is $a,num is $num,done." echo $a $num >> random_num.txt done } main(){ echo "Name num" > random_num.txt random_num } main
企业实用脚本18
已知下面的字符串是通过RANDOM随机数变量md5sum|cut-c 1-8截取后的结果,请破解这些字符串对应的md5sum前的RANDOM对应数字?
21029299
00205d1c
a3da1677
1f6d12dd
cf6a5205
#!/bin/sh array=( 21029299 00205d1c a3da1677 1f6d12dd cf6a5205 ) for((m=1;m<100000;m++)) do A=`echo $m|md5sum|cut -c 1-8` for((n=0;n<${#array[@]};n++)) do if [ "$A" = "${array[$n]}" ];then echo "$m = ${array[$n]}" echo "$m = ${array[$n]}">>crack.txt else continue fi done [ $(($m%100)) -eq 0 ]&&echo "$m...done" done echo "all done."
企业实用脚本19
批量检查多个网站地址是否正常
要求:shell数组方法实现,检测策略尽量模拟用户访问思路
http://www.etiantian.org
http://www.taobao.com
http://oldboy.blog.51cto.com
http://10.0.0.7
#!/bin/sh array=( http://www.etiantian.org https://www.taobao.com http://oldboy.blog.51cto.com http://10.0.0.7 http://www.lichengbing.com http://www.lichengbing.cn ) for((i=0;i<${#array[@]};i++)) do status=`curl -I -s -o /dev/null -w "%{http_code}\n" ${array[$i]}` if [ $status -eq 200 -o $status -eq 301 ];then echo "${array[$i]} OK" else echo "${array[$i]} NO" fi done
企业实用脚本20
1、按单词出现频率降序排序!
2、按字母出现频率降序排序!
the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation
#!/bin/sh word="the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation" #A=`echo $word|egrep -o ‘[a-zA-Z]+‘|sort|uniq -c|sort -r` #A=`echo $word|sed ‘s#[,.]##g‘|tr " " "\n"|sort|uniq -c|sort -r` #A=`echo $word|sed ‘s#[,. ]#\n#g‘|grep -v "^$"|awk ‘{array[$1]++}END{for(key in array)print key,array[key]}‘|sort -k2rn` A=`echo $word|awk ‘BEGIN{RS=""}{for(i=1;i<=NF;i++)if($i !~ /[ ,.]/)count[$i]++}END{for(key in count)print key,count[key]}‘|sort -k2rn` B=‘echo $word|tr "[,. ]" " "|sed ‘s# ##g‘|grep -o "\w"|sort|uniq -c|sort -rn‘ B=`echo $word|tr "[,. ]" " "|sed ‘s# ##g‘|sed -r ‘s#(.)#\1\n#g‘|sort -r|uniq -c|sort -rn ` echo $A echo $B
企业实用脚本21
请用shell或Python编写一个正方形接收用户输入的数字
#!/bin/sh read -p "Input A Number:" num for ((i=1;i<=$num;i++)) do for((m=1;m<=$((2*$num));m++)) do printf "*" done printf "\n" done [root@db02 shell]# sh shell_21.sh Input A Number:5 ********** ********** ********** ********** #!/bin/sh read -p "Input A Number:" num for ((i=1;i<=$num;i++)) do for((m=1;m<=$num;m++)) do printf "■" done printf "\n" done [root@db02 shell]# sh shell_21_02.sh Input A Number:6 ■■■■■■ ■■■■■■ ■■■■■■ ■■■■■■ ■■■■■■ ■■■■■■ #!/bin/sh read -p "Input a num:" num for((i=1;i<=$num;i++)) do for((n=$num;n>$i;n--)) do printf " " done for((m=1;m<=((2*$i-1));m++)) do printf "*" done printf "\n" done [root@db02 shell]# sh shell_21_03.sh Input a num:7 * *** ***** ******* ********* *********** *************
原文地址:http://lilongzi.blog.51cto.com/5519072/1837860