Linux三剑客实战2-性能分析与脚本编写

命令行参数传递:lucky a b c d e g h
文件获取清单: lucky < xxx.txt
管道获取清单: echo a b c d | lucky

lucky(){
let count=0
if [ $# -lt 1 ]
then
    pro_name=($(cat $1))
else
    pro_name=$@
fi
while true
do
    echo ${pro_name[@]}
    winner=()
    for i in ${pro_name[@]}
    do
         if [ $((RANDOM%6+1)) -ge 3 ]
         then  winner+=($i)
         fi
    done
    let count++
    if [[  ${#winner[@]} -gt 1 ]]
    then 
        pro_name=(${winner[@]})
        echo "第$count轮胜出人员:"${winner[@]}
    elif  [ ${#winner[@]} -eq 1 ]
    then
        echo "第$count轮胜出人员:"${winner[@]}
        echo "最终获胜者:"${winner[@]}
        break;
     else
         echo "第$count轮无获胜者,重新比赛!"
    fi
done
}

perf_avg () 
{ 
    top -b -n 20 -d 1 | 
            grep --color=auto --line-buffered -i aliyundun$ | 
            awk '
    BEGIN{OFS="\t";print "cpu","mem"}{print $9,$10; cpu+=$9; mem+=$10}END{print 
    "\navg\n";printf "%.2f\t%.2f\n", cpu/NR,mem/NR}
    '
}

##printf "%.2lf" 和 printf "%.2f" 都可以


connections_summary () 
{ 
    netstat -ntp 2> /dev/null | 
        awk 'NR>2{print $4,$6}' | 
        awk 'BEGIN{FS=":"}{ print $2}' | 
        sort | uniq -c | sort -nr | 
    awk 'BEGIN{OFS="\t";print "port","status\t","count"}{print $2,$3,$1}'
}



lucky()
{
	seeds=$(while read line ; do echo ${line// /..} ; done < name_list.txt)
	count=0;
	index=0;
	while((count!=1)); do
		((index+=1));
		seeds=$(for line in $seeds ; do (((RANDOM%6 + 1)>3)) && echo $line ; done);
		echo;
		echo 第 $index 轮 $seeds;
		count=$(echo "$seeds" | wc -l);
	done;
}
lucky(){
Namelist=$(cat name_list.txt)
round=0
while true
do
        let round+=1
        echo "round:$round" "member:${Namelist[*]}"
        i=0
        pass_list=()
        for member in ${Namelist[*]}
        do
                result=$((RANDOM%6+1))
                if [ $result -gt 3 ]
                then
                        pass_list[$i]=$member
                        let i+=1
                fi
        done
        if [ $i == 0 ]
        then
                continue
        elif [ $i == 1 ]
        then
                echo "winner:${pass_list[0]}"
                break
        else
                Namelist=${pass_list[*]}
                continue
        fi
done
}
lucky

lucky(){

seeds[1]=a
seeds[2]=b
seeds[3]=c

count=1
while true
do
winner=()
for i in ${seeds[@]}
do
if [ $((RANDOM%6+1)) -gt 3 ]
then winner+=($i)
fi
done
echo “第“$count”轮胜出的人员是:”${winner[@]}
((count++))
if [ ${#winner[@]} -eq 0 ]
then winner=$seeds
else seeds=$winner
fi
if [ ${#winner[@]} -eq 1 ]
then break
fi
done
echo “幸运中奖者是:”${winner[@]}

}