增加一个功能,输入不正确不退出脚本,只是重新输入
#!/bin/bash
#====================================================================
#
# 文件名:countdown_Graphical.sh
#
# 用途:倒计时
#
# 用法:sh /root/countdown_Graphical.sh--->不输入时间默认为0
#
# 创建时间:2019-09-20
#
# 修改时间:2019-09-30
#
# 作者:xxx
#
# 坐标如下:00 10 20
# 01 11 21
# 02 12 22
# 03 13 23
# 04 14 24
#
#===================================================================
#++++++++++++++++++++++++++++显示颜色的函数+++++++++++++++++++++++++++++#
function f_colour_31
{
echo -e "\033[1;31m$1\033[0m"
}
function f_colour_32
{
echo -e "\033[1;31m$1\033[0m"
}
function f_colour_33
{
echo -ne "\033[1;33m$1\033[0m"
}
#+++++++++++++++++++++++++++++判断倒计时结果并且输出图形+++++++++++++++++++++++++++#
function f_print_coordinate
{
num0=(0 0 1 0 2 0 0 1 2 1 0 2 2 2 0 3 2 3 0 4 1 4 2 4)
num1=(1 0 1 1 1 2 1 3 1 4)
num2=(0 0 1 0 2 0 2 1 2 2 1 2 0 2 0 3 0 4 1 4 2 4)
num3=(0 0 1 0 2 0 2 1 2 2 1 2 0 2 2 3 2 4 1 4 0 4)
num4=(0 0 0 1 0 2 1 2 2 2 2 0 2 1 2 3 2 4)
num5=(2 0 1 0 0 0 0 1 0 2 1 2 2 2 2 3 2 4 1 4 0 4)
num6=(2 0 1 0 0 0 0 1 0 2 0 3 0 4 1 2 2 2 2 3 2 4 1 4)
num7=(0 0 1 0 2 0 2 1 2 2 2 3 2 4)
num8=(0 0 0 1 0 2 0 3 0 4 1 0 2 0 2 1 1 2 2 2 2 3 1 4 2 4)
num9=(0 0 0 1 0 2 1 0 2 0 2 1 2 2 1 2 2 3 2 4 1 4 0 4)
colon=(1 1 1 3 )
}
function f_print_judge
{
echo ""
echo -e "\033[1;31m[ FAIL ]\033[0m \033[1;31m$1\033[0m \n"
}
##确定k的值
function f_if_k
{
k1=($1)
if [ "${z[*]}" == "${k1[*]}" ];then
k=$2
k=${k}${k}
fi
}
function f_judge_k
{
f_if_k "${num0[*]}" 0
f_if_k "${num1[*]}" 1
f_if_k "${num2[*]}" 2
f_if_k "${num3[*]}" 3
f_if_k "${num4[*]}" 4
f_if_k "${num5[*]}" 5
f_if_k "${num6[*]}" 6
f_if_k "${num7[*]}" 7
f_if_k "${num8[*]}" 8
f_if_k "${num9[*]}" 9
}
##输出图形的函数
function f_print_num1
{
z=($1)
f_judge_k
##显示颜色
echo -e "\033[1;31m"
for((i = 0,j = 1;i < ${#z[@]};i=i+2,j=j+2))
do
((x = left+2*${z[i]}))
((y = top+${z[j]}))
echo -e "\033[${y};${x}H${k}"
done
##恢复颜色
echo -e "\033[0m"
echo ""
}
function f_print_num2
{
z=($1)
f_judge_k
##显示颜色
echo -e "\033[1;32m"
for((i = 0,j = 1;i < ${#z[@]};i=i+2,j=j+2))
do
((x = left+2*${z[i]}))
((y = top+${z[j]}))
echo -e "\033[${y};${x}H${k}"
done
##恢复颜色
echo -e "\033[0m"
echo ""
}
##判断输出时间的数字的函数
function f_judge_min0
{
h0=($1)
if [ "${min0}" == "$2" ];then
left=93
top=28
f_print_num1 "${h0[*]}"
fi
}
function f_judge_min1
{
h1=($1)
if [ "${min1}" == "$2" ];then
left=100
top=28
f_print_num1 "${h1[*]}"
fi
}
function f_judge_min2
{
h2=($1)
if [ "${min2}" == "$2" ];then
left=107
top=28
f_print_num1 "${h2[*]}"
fi
}
function f_colon
{
left=114
top=28
h3=($1)
##显示颜色
echo -e "\033[1;35m"
for((i = 0,j = 1;i < ${#h3[@]};i=i+2,j=j+2))
do
((x = left+2*${h3[i]}))
((y = top+${h3[j]}))
echo -e "\033[${y};${x}H**"
done
##恢复颜色
echo -e "\033[0m"
echo ""
}
function f_judge_sec1
{
h4=($1)
if [ "${sec1}" == "$2" ];then
left=120
top=28
f_print_num2 "${h4[*]}"
fi
}
function f_judge_sec2
{
h5=($1)
if [ "${sec2}" == "$2" ];then
left=127
top=28
f_print_num2 "${h5[*]}"
fi
}
##判断时间并且输出图形的函数
function f_judge_time
{
echo ${time} | gawk -F: '{print $1}' | grep -oqE [[:digit:]]{3}
if [ $? -eq 0 ];then
##切割时间
total=($(echo ${time} | sed -r 's/(.)(.)(.)(.)(.)(.)/\1 \2 \3 \4 \5 \6/'))
min0=${total[0]}
min1=${total[1]}
min2=${total[2]}
col=${total[3]}
sec1=${total[4]}
sec2=${total[5]}
##判断min0
f_judge_min0 "${num0[*]}" 0
f_judge_min0 "${num1[*]}" 1
f_judge_min0 "${num2[*]}" 2
f_judge_min0 "${num3[*]}" 3
f_judge_min0 "${num4[*]}" 4
f_judge_min0 "${num5[*]}" 5
f_judge_min0 "${num6[*]}" 6
f_judge_min0 "${num7[*]}" 7
f_judge_min0 "${num8[*]}" 8
f_judge_min0 "${num9[*]}" 9
else
##切割时间
total=($(echo ${time} | sed -r 's/(.)(.)(.)(.)(.)/\1 \2 \3 \4 \5/'))
min1=${total[0]}
min2=${total[1]}
col=${total[2]}
sec1=${total[3]}
sec2=${total[4]}
fi
##判断min1
f_judge_min1 "${num0[*]}" 0
f_judge_min1 "${num1[*]}" 1
f_judge_min1 "${num2[*]}" 2
f_judge_min1 "${num3[*]}" 3
f_judge_min1 "${num4[*]}" 4
f_judge_min1 "${num5[*]}" 5
f_judge_min1 "${num6[*]}" 6
f_judge_min1 "${num7[*]}" 7
f_judge_min1 "${num8[*]}" 8
f_judge_min1 "${num9[*]}" 9
##判断min2
f_judge_min2 "${num0[*]}" 0
f_judge_min2 "${num1[*]}" 1
f_judge_min2 "${num2[*]}" 2
f_judge_min2 "${num3[*]}" 3
f_judge_min2 "${num4[*]}" 4
f_judge_min2 "${num5[*]}" 5
f_judge_min2 "${num6[*]}" 6
f_judge_min2 "${num7[*]}" 7
f_judge_min2 "${num8[*]}" 8
f_judge_min2 "${num9[*]}" 9
##输出冒号
f_colon "${colon[*]}"
##判断sec1
f_judge_sec1 "${num0[*]}" 0
f_judge_sec1 "${num1[*]}" 1
f_judge_sec1 "${num2[*]}" 2
f_judge_sec1 "${num3[*]}" 3
f_judge_sec1 "${num4[*]}" 4
f_judge_sec1 "${num5[*]}" 5
f_judge_sec1 "${num6[*]}" 6
f_judge_sec1 "${num7[*]}" 7
f_judge_sec1 "${num8[*]}" 8
f_judge_sec1 "${num9[*]}" 9
##判断sec2
f_judge_sec2 "${num0[*]}" 0
f_judge_sec2 "${num1[*]}" 1
f_judge_sec2 "${num2[*]}" 2
f_judge_sec2 "${num3[*]}" 3
f_judge_sec2 "${num4[*]}" 4
f_judge_sec2 "${num5[*]}" 5
f_judge_sec2 "${num6[*]}" 6
f_judge_sec2 "${num7[*]}" 7
f_judge_sec2 "${num8[*]}" 8
f_judge_sec2 "${num9[*]}" 9
}
##最后倒计时完毕输出00:00
function f_end
{
left=100
top=28
f_print_num1 "${num0[*]}"
left=107
top=28
f_print_num1 "${num0[*]}"
f_colon "${colon[*]}"
left=120
top=28
f_print_num2 "${num0[*]}"
left=127
top=28
f_print_num2 "${num0[*]}"
}
#++++++++++++++++++++++++++++判断输入的参数的函数+++++++++++++++++++++++#
function f_grep_judge_str
{
TF=$1
echo ${TF} | grep -qE [[:alpha:]]
st1=$?
echo ${TF} | grep -qE [[:punct:]]
st2=$?
echo ${TF} | grep -qE [[:cntrl:]]
st3=$?
}
function f_judge_minute
{
f_colour_33 "输入倒计时时间(分钟):"
read minute
minute=${minute:-0}
f_grep_judge_str ${minute}
while [ -z "${TF}" -o "${st1}" -eq 0 -o "${st2}" -eq 0 -o "${st3}" -eq 0 ]
do
echo ""
f_colour_31 "不能输入英文,标点符号或者控制字符 \n"
f_colour_33 "输入倒计时时间(分钟):"
read minute
minute=${minute:-0}
f_grep_judge_str ${minute}
if [ -n "${TF}" -a "${st1}" -ne 0 -a "${st2}" -ne 0 -a "${st3}" -ne 0 ]
then
break
fi
done
minute=${TF}
}
function f_judge_second
{
f_colour_33 "输入倒计时时间(秒钟):"
read second
second=${second:-0}
f_grep_judge_str ${second}
while [ -z "${TF}" -o "${st1}" -eq 0 -o "${st2}" -eq 0 -o "${st3}" -eq 0 ]
do
echo ""
f_colour_31 "不能输入英文,标点符号或者控制字符 \n"
f_colour_33 "输入倒计时时间(秒钟):"
read second
second=${second:-0}
f_grep_judge_str ${second}
if [ -n "${TF}" -a "${st1}" -ne 0 -a "${st2}" -ne 0 -a "${st3}" -ne 0 ]
then
break
fi
done
second=${TF}
}
function f_judge_range
{
[ "$1" -gt 999 ]
st4=$?
[[ "$1" -gt 900 && "$2" -gt 60 ]]
st5=$?
[[ "$1" -le 900 && "$2" -gt 5940 ]]
st6=$?
}
#+++++++++++++++++++++++++++++脚本从这里开始运行++++++++++++++++++++++++++++#
##输入并且判断参数
f_judge_minute
f_judge_second
f_judge_range "${minute}" "${second}"
while [ "${st4}" -eq 0 -o "${st5}" -eq 0 -o "${st6}" -eq 0 ]
do
echo ""
f_colour_31 "1.最高支持999分钟!!!"
f_colour_31 "2.当分钟数大于900分钟时,输入的秒数不能超过60!!!"
f_colour_31 "3.当分钟小于或者等于900时,输入的秒数不能超过5940!!! \n"
f_judge_minute
f_judge_second
f_judge_range "${minute}" ${second}
if [ "${st4}" -ne 0 -a "${st5}" -ne 0 "${st6}" -ne 0 ]
then
break
fi
done
##计算总秒数
second=$[${minute}*60+${second}]
##调用坐标图
f_print_coordinate
##倒计时循环
while [ ${second} -gt 0 ]
do
min=$[${second}/60]
sec=$[${second}%60]
if [ ${min} -lt 10 ];then
clear
min=$(printf %02d ${min})
time="${min}:${sec}"
if [ ${sec} -lt 10 ];then
clear
time="${min}:0${sec}"
fi
else
clear
time="${min}:${sec}"
if [ ${sec} -lt 10 ];then
clear
time="${min}:0${sec}"
fi
fi
f_judge_time
sleep 1
let second-=1
done
clear
f_end
echo ""