5 if printf "%s" "$1" | grep -qE '^[0-9]+$'; then
6 if [ "$m" -eq 0 ]; then
7 printf "Decimal %s → Binary: " "$1"
10 printf "obase=2; %s" "$1" | bc
12 elif printf "%s" "$1" | grep -qE '^([0-9A-Fa-f]+|0x[0-9A-Fa-f]+)$'; then
13 if [ "$m" -eq 0 ]; then
14 printf "Hex %s → Binary: " "$1"
17 printf "ibase=16; obase=2; %s" "$(printf "%s" "$1" | sed 'y/abcdef/ABCDEF/; s/^0x//')" | bc
20 printf "Error: '%s' is not a valid decimal or hexadecimal number\n" "$1" >&2
31 USAGE
="usage: ${0} [-mnp] ..."
32 if ! [ "$1" = "x" ] && ! [ -t 0 ]; then
33 cat | tr '\n' ' ' | xargs tobin
"x" "$@"
36 elif [ "$1" = "x" ]; then
42 printf "Error: No arguments provided\n%s\n" "$USAGE" >&2
49 while getopts "lmnp" flag
; do
54 *) printf "%s\n" "$USAGE" >&2 ; return 2 ;;
58 shift $(( OPTIND - 1 ))
63 if [ "$n" -eq 1 ] && [ "$m" -eq 1 ]; then
64 t
=$(printf "%s" "$t" | tr '\n' ' ')
66 elif [ "$n" -eq 1 ] && [ "$m" -eq 0 ]; then
67 t
=$(printf "%s" "$t" | tr '\n' ';')