使用shell脚本实现二进制、十进制、十六进制转换

脚本如下

#!/bin/bash
read -p "输入进制数:" A
read -p "输入数字:" B
if [ $A = 10 ]
    then
       echo "二进制数为:$(echo "obase=2;$B"|bc)"
       echo "十六进制数为:$(echo "obase=16;$B"|bc)"
elif [ $A = 2 ]
    then
       echo "十进制数为:$(echo $((2#$B)))"
       echo "十六进制数为:$(echo "obase=16;$(echo $((2#$B)))"|bc)"
elif [ $A = 16 ]
    then
       echo "二进制数为:$(echo "obase=2;$(echo "ibase=16;obase=1010;$B"|bc)"|bc)"
       echo "十进制数为:$(echo "ibase=16;obase=1010;$B"|bc)"
else
       echo "暂不支持转换"
fi

你可能感兴趣的:(shell脚本)