- #!/bin/bash
- while true
- do
- echo "#############################################################"
- echo "# Author: netcat #"
- echo "# Lable: jinzhi.sh #"
- echo "# QQ: 297020555 #"
- echo "# Create Date: 2011-06-17 #"
- echo "# Version: v1.0 #"
- echo "#############################################################"
- echo ""
- echo "1 2 ---> 8" #二进制转换成八进制
- echo "2 2 ---> 10" #二进制转换成十进制
- echo "3 2 ---> 16" #二进制转换成十六进制
- echo "4 8 ---> 2" #八进制转换成二进制
- echo "5 8 ---> 10" #八进制转换成十进制
- echo "6 8 ---> 16" #八进制转换成十六进制
- echo "7 10 ---> 2" #十进制转换成二进制
- echo "8 10 ---> 8" #十进制转换成八进制
- echo "9 10 ---> 16" #十进制转换成十六进制
- echo "10 16 ---> 2" #十六进制转换成二进制
- echo "11 16 ---> 8" #十六进制转换成八进制
- echo "12 16 ---> 10" #十六进制转换成十进制
- echo "13 exit" #退出
- echo ""
- echo "enter your select:" #输入选择
- read ans #读取选择的值,赋值给ans
- case $ans in #读取$ans的值
- 1) #如果ans为1时
- read -p "enter your num:" num #输入你要转换的数,赋值给num
- a=`echo $num|tr "[2-9][a-z][A-Z]" "[4-5][A-Z][a-z]"` #对num进行判断
- if [ $num != $a ] #如果num不等于a
- then
- echo -e "33[40;31;5m failt 33[0m" #说明输入的num值不符合二进制数的标准(0和1)
- sleep 2 #停顿2s
- else #否则的话,num符合二进制的标准
- result=`echo "obase=8;ibase=2;$num"|bc` #把num转换成八进制,赋值给result
- echo -e "33[40;32;1m result is 33[40;31;5m $result33[0m" #输出result的值
- sleep 4 #停顿4s
- fi
- ;;
- 2)
- read -p "enter your num:" num
- a=`echo $num|tr "[2-9][a-z][A-Z]" "[4-5][A-Z][a-z]"`
- if [ $num != $a ]
- then
- echo -e "33[40;31;5m failt 33[0m"
- sleep 2
- else
- result=`echo "obase=10;ibase=2;$num"|bc` #需要注意:obase在前,ibase在后,不然会出错
- echo -e "33[40;32;1m result is 33[40;31;5m $result33[0m"
- sleep 4
- fi
- ;;
- 3)
- read -p "enter your num:" num
- a=`echo $num|tr "[2-9][a-z][A-Z]" "[4-5][A-Z][a-z]"`
- if [ $num != $a ]
- then
- echo -e "33[40;31;5m failt 33[0m"
- sleep 2
- else
- result=`echo "obase=16;ibase=2;$num"|bc`
- echo -e "33[40;32;1m result is 33[40;31;5m $result33[0m"
- sleep 4
- fi
- ;;
- 4)
- read -p "enter your num:" num
- a=`echo $num|tr "[8-9][a-z][A-Z]" "[4-5][A-Z][a-z]"` #判断是否符合八进制标准
- if [ $num != $a ]
- then
- echo -e "33[40;31;5m failt 33[0m"
- sleep 2
- else
- result=`echo "obase=2;ibase=8;$num"|bc`
- echo -e "33[40;32;1m result is 33[40;31;5m $result33[0m"
- sleep 4
- fi
- ;;
- 5)
- read -p "enter your num:" num
- a=`echo $num|tr "[8-9][a-z][A-Z]" "[4-5][A-Z][a-z]"`
- if [ $num != $a ]
- then
- echo -e "33[40;31;5m failt 33[0m"
- sleep 2
- else
- result=`echo "obase=10;ibase=8;$num"|bc`
- echo -e "33[40;32;1m result is 33[40;31;5m $result33[0m"
- sleep 4
- fi
- ;;
- 6)
- read -p "enter your num:" num
- a=`echo $num|tr "[8-9][a-z][A-Z]" "[4-5][A-Z][a-z]"`
- if [ $num != $a ]
- then
- echo -e "33[40;31;5m failt 33[0m"
- sleep 2
- else
- result=`echo "obase=16;ibase=8;$num"|bc`
- echo -e "33[40;32;1m result is 33[40;31;5m $result33[0m"
- sleep 4
- fi
- ;;
- 7)
- read -p "enter your num:" num
- a=`echo $num|tr "[a-z][A-Z]" "[A-Z][a-z]"` #判断是否符合十进制标准
- if [ $num != $a ]
- then
- echo -e "33[40;31;5m failt 33[0m"
- sleep 2
- else
- result=`echo "obase=2;ibase=10;$num"|bc`
- echo -e "33[40;32;1m result is 33[40;31;5m $result33[0m"
- sleep 4
- fi
- ;;
- 8)
- read -p "enter your num:" num
- a=`echo $num|tr "[a-z][A-Z]" "[A-Z][a-z]"`
- if [ $num != $a ]
- then
- echo -e "33[40;31;5m failt 33[0m"
- sleep 2
- else
- result=`echo "obase=8;ibase=10;$num"|bc`
- echo -e "33[40;32;1m result is 33[40;31;5m $result33[0m"
- sleep 4
- fi
- ;;
- 9)
- read -p "enter your num:" num
- a=`echo $num|tr "[a-z][A-Z]" "[A-Z][a-z]"`
- if [ $num != $a ]
- then
- echo -e "33[40;31;5m failt 33[0m"
- sleep 2
- else
- result=`echo "obase=16;ibase
- esac
- done
下面给大家截个图:
关于进制转换的问题 : http://297020555.blog.51cto.com/1396304/591988
关于echo输出颜色的问题:http://297020555.blog.51cto.com/1396304/591943
这个脚本写的比较粗糙,很多内容都不完善,拿出来,让大家给点建议.
在过程中遇到个问题,感觉解决的不是很好,希望大家给点意见.
怎么判断输入的内容是否符合二进制标准(0和1)?
我是这么解决的:
a=`echo $num|tr "[2-9][a-z][A-Z]" "[4-5][A-Z][a-z]"`
如果num和a的值相同,则符合要求.
希望大家给点意见
|