shell实现的数字转换脚本(用-h选项查看help信息)

#!/bin/bash
check_opts(){
	if [ -z "$src" ]; then 
		echo "use -s to specify the original radix"
		exit 1
	fi 
	if [ -z "$des" ]; then 
		echo "use -d to specify the final radix"
		exit 1
	fi 
}
if [ "$#" -lt 1 ]; then 
cat <<HELPEOF
use option -h to get more information .
HELPEOF
exit 0
fi 
while getopts "s:d:h" opt
do
	case $opt in
	s)
	src=$OPTARG
	;;
	d)
	des=$OPTARG
	;;
	h)
cat <<HELPEOF
NAME
	baseconv.sh - convert number to a different radix
SYNOPSIS
	basecon.sh [OPTION]... [NUMBER]...
DESCRIPTION
	baseconv.sh is used to convert number to a different radix, NUMBER specify the number 
	which desire convertion .
	-s
		specify the original radix
	-d
		specify the final radix
HELPEOF
	exit 0
	;;
	esac
done
check_opts 
shift $((OPTIND-1))
if [ $# -lt 1 ]; then 
	echo "please input at least one number !"
fi
i=0
while [ $# -gt 0 ] 
do
	num=$1
	shift 1
	if [ $src -eq $des ]; then
		echo $num
		continue 
	fi
	if [ ! $src -eq "10" ]; then
		((num=$src#$num))
		#echo $num
	fi	
	if [ $des -eq "10" ]; then 
		echo $num
	else 
		echo $(echo "obase=$des;$num" | bc)
	fi 
done
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值