5 if printf "%s" "$1" | grep -qE '^[0-9]+$'; then
6 if [ "$m" -eq 0 ]; then
7 printf "Decimal %s → Hex: " "$1"
9 printf "%s%X\n" "$p" "$1"
11 elif printf "%s" "$1" | grep -qE '^0b[0-1]+$'; then
12 if [ "$m" -eq 0 ]; then
13 printf "Binary %s → Hex: " "$1"
15 printf "%s%X\n" "$p" "$1"
18 printf "Error: '%s' is not a valid binary or decimal number\n" "$1" >&2
29 USAGE
="usage: ${0} [-lmnp] ..."
30 if ! [ "$1" = "x" ] && ! [ -t 0 ]; then
31 cat | tr '\n' ' ' | xargs tohex
"x" "$@"
34 elif [ "$1" = "x" ]; then
40 printf "Error: No arguments provided\n%s\n" "$USAGE" >&2
47 while getopts "lmnp" flag
; do
53 *) printf "%s\n" "$USAGE" >&2 ; return 2 ;;
57 shift $(( OPTIND - 1 ))
62 if [ "$n" -eq 1 ] && [ "$m" -eq 1 ]; then
63 t
=$(printf "%s" "$t" | tr '\n' ' ')
65 elif [ "$n" -eq 1 ] && [ "$m" -eq 0 ]; then
66 t
=$(printf "%s" "$t" | tr '\n' ';')
73 if [ "$l" -eq 1 ]; then
74 t
=$(printf "%s" "$t" | tr '[:upper:]' '[:lower:]')