Skip to content

Instantly share code, notes, and snippets.

@macoril
Last active March 19, 2019 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macoril/2cd76be7577507296a5f8219fe0904a9 to your computer and use it in GitHub Desktop.
Save macoril/2cd76be7577507296a5f8219fe0904a9 to your computer and use it in GitHub Desktop.
a shell command alias to convert a number to different base easily
# 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