教程集 www.jiaochengji.com
教程集 >  脚本编程  >  shell  >  正文 shell数字转换脚本(用-h选项查看help信息)

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

发布时间:2014-12-12   编辑:jiaochengji.com
分享一例shell脚本,实现数字转换,并实现了帮助信息功能,用-h选项查看help信息,有需要的朋友参考下。

一例实现数字转换的shell脚本代码。

例子:
 

复制代码 代码示例:
#!/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  # www.jiaochengji.com
    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

以上shell脚本实现数字转换,并可以接收参数选项,是一例不错的shell代码。

您可能感兴趣的文章:
shell数字转换脚本(用-h选项查看help信息)
php命令行参数详解及应用PHP学习网
inux shell初级入门教程
python命令行参数是什么
shell获取系统时间的方法
linux shell变量解析
PHP 常用命令行
psql常用命令收集
sed单行命令大全 值得收藏
shell脚本命令行参数用法简介

关键词: 数字转换   
[关闭]
~ ~