#!/bin/bash
############################################
#time: 2020-03-21
#order: binary converse
#auth: MS.zhao
#email: wd.zhao@qq.com
#notes: For positive integers only
############################################
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#打印菜单
function menu ()
{
cat << EOF
--------------------------------------------
|*****************菜单主页*****************|
--------------------------------------------
`echo -e "\033[35m 1)2进制转10进制\033[0m"`
`echo -e "\033[35m 2)8进制转10进制\033[0m"`
`echo -e "\033[35m 3)10进制转其他\033[0m"`
`echo -e "\033[35m 4)16进制转10进制\033[0m"`
`echo -e "\033[35m 5)32进制转10进制\033[0m"`
`echo -e "\033[35m 6)64进制转10进制\033[0m"`
`echo -e "\033[35m h)help\033[0m"`
`echo -e "\033[35m q)exit\033[0m"`
--------------------------------------------
|**********注:只针对于正整数**************|
--------------------------------------------
EOF
read -p "请输入对应的序号:" num
nu=${#num}
if [[ $num =~ [1-6] ]] && [ $nu -eq 1 ];then
read -p "输入需要转换的数值:" n
fi
}
#二进制转十进制
function twoTten(){
if [[ $n =~ ^[01]{1,}$ ]];then
echo "Welcome to 2进制转10进制!"
typeset -i N=2#${n}
echo -e "2进制转10进制结果为:\033[35m $N\033[0m"
sleep 1
else
echo "输入数值有误,请正确输入!"
fi
}
#八进制转十进制
function aightTten(){
if [[ $n =~ ^[0-7]{1,}$ ]];then
echo "Welcome to 8进制转10进制!"
typeset -i N=8#${n}
echo -e "8进制转10进制结果为:\033[35m $N\033[0m"
sleep 1
else
echo "输入数值有误,请正确输入!"
fi
}
#十六进制转十进制
function sixtTten(){
if [[ $n =~ ^[0-9a-fA-\F]{1,}$ ]];then
echo "Welcome to 16进制转10进制!"
typeset -i N=16#${n}
echo -e "16进制转10进制结果为:\033[35m $N\033[0m"
sleep 1
else
echo "输入数值有误,请正确输入!"
fi
}
#三十二进制转十进制
function threetTten(){
if [[ $n =~ ^[0-9A-Ya-\y]{1,}$ ]];then
echo "Welcome to 32进制转10进制!"
typeset -i N=32#${n}
echo -e "32进制转10进制结果为:\033[35m $N\033[0m"
sleep 1
else
echo "输入数值有误,请正确输入!"
fi
}
#六十四进制转十进制
function sixfTten(){
if [[ $n =~ ^[0-9a-zA-\Z+/]{1,}$ ]];then
echo "Welcome to 64进制转10进制!"
typeset -i N=64#${n}
echo -e "64进制转10进制结果为:\033[35m $N\033[0m"
sleep 1
else
echo "输入数值有误,请正确输入!"
fi
}
#十进制转其他进制
function tenTother(){
cat<<EOF
#################十进制转换菜单###################
`echo -e "\033[35m 1)十进制转二进制\033[0m"`
`echo -e "\033[35m 2)十进制转八进制\033[0m"`
`echo -e "\033[35m 3)十进制转十六进制\033[0m"`
`echo -e "\033[35m 4)十进制转三十二进制\033[0m"`
`echo -e "\033[35m 5)十进制转六十四进制\033[0m"`
`echo -e "\033[35m h)help\033[0m"`
##################################################
EOF
read -p "请选择对应的序号:" T
if [[ $T =~ [1-5h] ]] && [ ${#T} -eq 1 ];then
case $T in
1)
two=`echo "obase=2;$n" |bc`
echo -e "10进制转2进制结果为:\033[35m $two\033[0m"
sleep 1
;;
2)
aight=`echo "obase=8;$n" |bc`
echo -e "10进制转8进制结果为:\033[35m $aight\033[0m"
sleep 1
;;
3)
tensix=`echo "obase=16;$n" |bc`
echo -e "10进制转16进制结果为:\033[35m $tensix\033[0m"
sleep 1
;;
4)
threetwo=`echo "obase=32;$n" |bc`
echo -e "10进制转32进制结果为:\033[35m $threetwo\033[0m"
sleep 1
;;
5)
sixfour=`echo "obase=64;$n" |bc`
echo -e "10进制转64进制结果为:\033[35m $sixfour\033[0m"
sleep 1
;;
h)
clear
tenTother
;;
esac
else
echo "输入有误,请正确输入!"
tenTother
fi
}
#进制计算调用
function count(){
if [[ $num =~ [1-6hq] ]] && [ $nu -eq 1 ];then
case $num in
1)
twoTten
;;
2)
aightTten
;;
3)
if [[ $n =~ ^[0-9]{1,}$ ]];then
echo "Welcome to 10进制转其他进制"
tenTother
fi
;;
4)
sixtTten
;;
5)
threetTten
;;
6)
sixfTten
;;
h)
clear
menu
count
;;
q)
exit 0
esac
else
echo "输入有误!请重新输入!"
menu
count
fi
}
#捕捉结束信号
trap "echo Byebye!" EXIT
#循环计算
for i in {1..999}
do
if [ $i -gt 1 ];then
while true
do
echo "请按回车继续!或者Ctrl+c结束!" && read
menu
count
done
else
menu
count
fi
done