Last active
March 19, 2019 11:52
-
-
Save macoril/2cd76be7577507296a5f8219fe0904a9 to your computer and use it in GitHub Desktop.
a shell command alias to convert a number to different base easily
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# copy to your bash_profile or somewhere you write aliases, | |
# then you can convert a number to different base by cbase | |
# エイリアスを設定しているあたりにでも以下のコードをコピペすると基数変換がcbaseコマンドで簡単できるようになります | |
function cbase() { | |
function echo_usage() { | |
cat << USAGE | |
usage: cbase [-i num] [-o num] num | |
-i num | |
Set a number of base you want to convert FROM. | |
Default is 10. | |
-o num | |
Set a number of base you want to convert TO. | |
Default is 10. | |
USAGE | |
} | |
local OPTIND OPT OPTERR input output | |
while getopts "i:o:" OPT; do | |
case "$OPT" in | |
i) input="$OPTARG" ;; | |
o) output="$OPTARG" ;; | |
esac | |
done | |
shift $(($OPTIND - 1)) | |
[ -z "$input" -a -z "$output" ] && echo_usage | |
[ -z "$input" ] && input=10 | |
[ -z "$output" ] && output=10 | |
echo "obase=$output; ibase=$input; $1" | bc | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment