计算两数值之间的和-第二版 ``` #!/bin/bash #Version:0.3.1 #Author:stone #Discription:get sum. a=1 #给变量赋初始值,用于case选择 function cho() #用于选择返回的输入值的位置 { while :; do case $1 in 2) echo -e "\033[32m1-返回最小值输入.\033[0m" echo -e "\033[32m2-返回最大值输入.\033[0m" echo -e "\033[32m直接回车返回当前默认层.\033[0m" echo -n -e "\033[31m请输入返回哪一个层级:\033[0m" read n #从键盘获取一个返回层级值 if [ ! -z $n ]; then n1=$(echo $n |sed 's/[0-9]//g') if [ -z $n1 ]; then if [ $n -le 2 -a $n -ge 1 ]; then a=$n continue 2 else echo -e "\033[31m请输入1或者2!\033[0m" fi else echo -e "\033[31m输入不是纯数字!\033[0m" fi else a=$a continue 2 fi ;; 3) echo -e "\033[32m1-返回最小值输入.\033[0m" echo -e "\033[32m2-返回最大值输入.\033[0m" echo -e "\033[32m3-返回增量值输入.\033[0m" echo -n -e "\033[31m请输入返回哪一个层级:\033[0m" read n #从键盘获取一个返回层级值 if [ ! -z $n ]; then #对获取的值进行判断 n1=$(echo $n |sed 's/[0-9]//g') if [ -z $n1 ]; then if [ $n -le 3 -a $n -ge 1 ]; then a=$n continue 2 else echo -e "\033[31m请输入1或者2或者3!\033[0m" fi else echo -e "\033[31m输入不是纯数字!\033[0m" fi else a=$a continue 2 fi ;; esac done } while :; do case $a in #case选择语句使用变量进行修正每一次的值 1) echo -n -e "\033[31m请输入累加的初始值:\033[0m" read num1 ;; 2) echo -n -e "\033[31m请输入累加的最大值:\033[0m" read num2 ;; 3) echo -n -e "\033[31m请输入累加的自增量:\033[0m" read num3 ;; esac tnum=num${a} #变量的嵌套 eval number=$(echo \$$tnum) #变量的嵌套 if [ ! -z "$number" ]; then #判断输入值是否不空,空则执行下一个循环 n=$(echo $number|sed 's/[0-9]//g') #判断输入是否是纯数字 if [ -z $n ]; then ar[$a]=$number #将输入的值赋值给数组 if [ $a -eq 2 ]; then #判断最大值是否已经赋值,如果已经赋值则判断大小 if [ ${ar[1]} -gt ${ar[2]} ]; then #判断起始数值是否大于结束数值 echo -e "\033[31m你输入的起始数字大于结束数字!\033[0m" a=2 cho $a #函数的调用 fi fi if [ $a -eq 3 ]; then #判断赋值是否结束,结束则退出循环 price=$((${ar[2]}-${ar[1]})) if [ ${ar[3]} -gt $price ]; then #判断增量值是否符合标准 a=3 echo -e "\033[31m你输入的增量值不符合要求!\033[0m" cho $a #函数的调用 else break #满足所有条件退出循环 fi fi let a++ #变量用于数组下标,同时也用于case判断 continue else a=$a #任何一次输入不为纯数字时返回相应的case语句 echo -e "\033[31m你输入的不是纯数字\033[0m" continue fi else a=$a #任何一次输入为空时返回执行相应的case语句 echo -e "\033[31m输入不能为空!\033[0m" continue fi done sum=0 #用于累加 #以下为调用数组for计算循环 for ((i=${ar[1]};i<=${ar[2]};i+=${ar[3]})); do sum=$(($sum+$i)) done echo $sum ``` shell脚本 2017-03-15 评论 1759 次浏览
计算两数值之间的和-第一版 ``` #!/bin/bash #Version:0.1.1 #Author:stone #Discription:get sum. a=1 while :; do case $a in 1) echo -n -e "\033[31m请输入累加的初始值:\033[0m" read num1 ;; 2) echo -n -e "\033[31m请输入累加的最大值:\033[0m" read num2 ;; 3) echo -n -e "\033[31m请输入累加的自增量:\033[0m" read num3 ;; esac #此处为变量的嵌套 tnum=num${a} eval number=$(echo \$$tnum) if [ ! -z "$number" ]; then n=$(echo $number|sed 's/[0-9]//g') if [ -z $n ]; then ar[$a]=$number if [ $a -eq 3 ]; then break fi let a++ continue else a=$a echo -e "\033[31m你输入的不是纯数字\033[0m" continue fi else a=$a echo -e "\033[31m输入不能为空!\033[0m" continue fi done sum=0 for ((i=${ar[1]};i<=${ar[2]};i+=${ar[3]})); do sum=$(($sum+$i)) done echo $sum ``` shell脚本 2017-03-14 评论 1652 次浏览
获取CPU、内存状态 ``` #!/bin/bash #Version:0.0.1 #Author:stone #Discription:get MEM & CPU stat from command ps. mem=($(ps aux |awk 'NR>1{print $4}')) cpu=($(ps aux |awk 'NR>1{print $3}')) sum1=0 sum2=0 case "$0" in mem) for i in ${mem[*]}; do #此处数组变量不能加引号,否则会当做一个整体 sum1=$(echo $sum1+$i |bc) done if [ $(echo "$sum1>50" |bc) -eq 1 ]; then echo -e "\033[31;1mmemory total $sum1\033[0m" else echo -e "\033[32;2mmemory total $sum1\033[0m" fi ;; cpu) for j in ${cpu[*]}; do sum2=$(echo $sum2+$j |bc) done if [ $(echo "$sum2>50" |bc) -eq 1 ]; then echo -e "\033[31;1mcpu total $sum2\033[0m" else echo -e "\033[32;2mcpu total $sum2\033[0m" fi ;; esac ``` shell脚本 2017-03-13 评论 1696 次浏览
提取已建立的TCP连接 ``` #!/bin/sh #Version:0.0.1 #Author:stone #Discription:count netstat declare -i established=0 declare -i listen=0 declare -i other=0 #提取netstat中状态 for i in $(netstat -ant |grep "tcp\>" |awk '{print $NF}'); do if [ "$i" == "ESTABLISHED" ]; then let established++ elif [ "$i" == "LISTEN" ]; then let listen++ else let other++ fi done echo "ESTABLISHED:$established" echo "LISTEN:$listen" echo "OTHER:$other" netstat -ant |grep "tcp\>" |awk '{print $NF}' |sort |uniq -c ``` shell脚本 2017-03-12 评论 1703 次浏览
生成随机时间,用于测试正则语法 ``` #!/bin/bash #Version:0.0.1 #Author:stone #Discription:get random date. #获取指定的数字用于循环 while :; do read -p "please input number:" number if [ "$(echo $number |sed "s/[0-9]//g")" != "" ]; then echo -e "\033[31minput not number!please try again!\033[0m" continue #不是数字就继续下次输入循环 else break #为数字时退出循环 fi done i=1 while [ $i -le $number ]; do #随机的年 dateY=$((${RANDOM}%19)) #随机的月 dateM=$((${RANDOM}%13)) #随机的日,大月 dateD1=$((${RANDOM}%32)) #随机的日,小月 dateD2=$((${RANDOM}%31)) #随机的日,闰二月 dateD3=$((${RANDOM}%30)) #随机的日,平二月 dateD4=$((${RANDOM}%29)) #随机的小时 dateh=$((${RANDOM}%24)) #随机的分钟 datem=$((${RANDOM}%60)) #随机的秒 dates=$((${RANDOM}%60)) #判断月份决定日期,大月 if [ $dateM -eq 1 -o $dateM -eq 3 -o $dateM -eq 5 -o $dateM -eq 7 -o $dateM -eq 8 -o $dateM -eq 10 -o $dateM -eq 12 ]; then if [ $dateY -lt 10 ]; then dateY="0$dateY" fi if [ $dateM -eq 0 ]; then dateM=1 #月份如果结果为0,赋值为1 fi if [ $dateM -lt 10 ]; then dateM="0${dateM}" #月份如果小于10,在数值前加0 fi if [ $dateD1 -eq 0 ]; then dateD1=1 #日子如果等于0,赋值为1 fi if [ $dateD1 -lt 10 ]; then dateD1="0$dateD1" fi #时分秒的随机 if [ $dateh -eq 0 ]; then dateh=1 fi if [ $dateh -lt 10 ]; then dateh="0$dateh" fi if [ $datem -lt 10 ]; then datem="0$datem" fi if [ $dates -lt 10 ]; then dates="0$dates" fi date -s "${dateh}:${datem}:${dates}" date -s "20${dateY}${dateM}${dateD1}" date +%F-%T >>./date.txt date +%D-%T >>./date.txt date >>./date.txt fi #判断月份决定日期,小月 if [ $dateM -eq 4 -o $dateM -eq 6 -o $dateM -eq 9 -o $dateM -eq 11 ]; then if [ $dateY -lt 10 ]; then dateY="0$dateY" fi if [ $dateM -eq 0 ]; then dateM=1 #月份如果结果为0,赋值为1 fi if [ $dateM -lt 10 ]; then dateM="0${dateM}" #月份如果小于10,在数值前加0 fi if [ $dateD2 -eq 0 ]; then dateD1=2 #日子如果等于0,赋值为1 fi if [ $dateD2 -lt 10 ]; then dateD2="0$dateD2" fi #时分秒的随机 if [ $dateh -eq 0 ]; then dateh=1 fi if [ $dateh -lt 10 ]; then dateh="0$dateh" fi if [ $datem -lt 10 ]; then datem="0$datem" fi if [ $dates -lt 10 ]; then dates="0$dates" fi date -s "${dateh}:${datem}:${dates}" date -s "20${dateY}${dateM}${dateD2}" date +%F-%T >>./date.txt date +%D-%T >>./date.txt date >>./date.txt fi #判断二月份决定日期 if [ $dateM -eq 2 ]; then if [ $dateY -lt 10 ]; then dateY="0$dateY" fi year="20$dateY" if [ $(($year%4)) -ne 0 ]; then if [ $dateM -eq 0 ]; then dateM=1 #月份如果结果为0,赋值为1 fi if [ $dateM -lt 10 ]; then dateM="0${dateM}" #月份如果小于10,在数值前加0 fi if [ $dateD3 -eq 0 ]; then dateD3=1 #日子如果等于0,赋值为1 fi if [ $dateD3 -lt 10 ]; then dateD3="0$dateD3" fi #时分秒的随机 if [ $dateh -eq 0 ]; then dateh=1 fi if [ $dateh -lt 10 ]; then dateh="0$dateh" fi if [ $datem -lt 10 ]; then datem="0$datem" fi if [ $dates -lt 10 ]; then dates="0$dates" fi date -s "${dateh}:${datem}:${dates}" date -s "20${dateY}${dateM}${dateD3}" date +%F-%T >>./date.txt date +%D-%T >>./date.txt date >>./date.txt else if [ $dateM -eq 0 ]; then dateM=1 #月份如果结果为0,赋值为1 fi if [ $dateM -lt 10 ]; then dateM="0${dateM}" #月份如果小于10,在数值前加0 fi if [ $dateD4 -eq 0 ]; then dateD4=1 #日子如果等于0,赋值为1 fi if [ $dateD4 -lt 10 ]; then dateD4="0$dateD4" fi #时分秒的随机 if [ $dateh -eq 0 ]; then dateh=1 fi if [ $dateh -lt 10 ]; then dateh="0$dateh" fi if [ $datem -lt 10 ]; then datem="0$datem" fi if [ $dates -lt 10 ]; then dates="0$dates" fi date -s "${dateh}:${datem}:${dates}" date -s "20${dateY}${dateM}${dateD4}" date +%F-%T >>./date.txt date +%D-%T >>./date.txt date >>./date.txt fi fi let i++ done ``` shell脚本 2017-03-11 评论 1729 次浏览
copy command and lib 2 ``` #!/bin/bash #copy command and lib #Auther:stone ROOTDIR=/mnt/sysroot cplib() { for lib in `ldd $1 |grep -o "/.*lib\(64\)\{0,1\}.*[[:space:]]"`; do libdir=${lib%/*} [ ! -d $ROOTDIR${libdir} ] && mkdir -p $ROOTDIR${libdir} [ ! -f $ROOTDIR${lib} ] && cp $lib $ROOTDIR${libdir} && echo "copy $lib is finish." done } cpcommand() { dir=${1%/*} [ ! -d $ROOTDIR${dir} ] && mkdir -p $ROOTDIR${dir} [ ! -f $ROOTDIR${1} ] && cp $1 $ROOTDIR${dir} && echo "$1 copy finish." cplib $1 } while :; do read -p "please input command:" command [ $command == "q" ] && echo "exit." && exit which $command >&/dev/null [ $? == "1" ] && echo "$command not exisit,please try again!" && continue cmddir=`which $command |grep -v "alias" |grep -o "[^[:space:]]*"` cpcommand $cmddir echo "$command copy finish." done ``` shell脚本 2017-03-10 评论 1713 次浏览