| 1 | #!/bin/bash |
| 2 | # hexconvert.sh: Convert a decimal number to hexadecimal. |
| 3 | |
| 4 | E_NOARGS=85 # Command-line arg missing. |
| 5 | BASE=16 # Hexadecimal. |
| 6 | |
| 7 | if [ -z "$1" ] |
| 8 | then # Need a command-line argument. |
| 9 | echo "Usage: $0 number" |
| 10 | exit $E_NOARGS |
| 11 | fi # Exercise: add argument validity checking. |
| 12 | |
| 13 | |
| 14 | hexcvt () |
| 15 | { |
| 16 | if [ -z "$1" ] |
| 17 | then |
| 18 | echo 0 |
| 19 | return # "Return" 0 if no arg passed to function. |
| 20 | fi |
| 21 | |
| 22 | echo ""$1" "$BASE" o p" | dc |
| 23 | # o sets radix (numerical base) of output. |
| 24 | # p prints the top of stack. |
| 25 | # For other options: 'man dc' ... |
| 26 | return |
| 27 | } |
| 28 | |
| 29 | hexcvt "$1" |
| 30 | |
| 31 | exit |