0

I can't get my function to do what I want. When I call my function get_from_A_to_F I give it an argument $remainder. I want my function to substitute a number higher than 9 to a specific letter. If the argument is equal to 10 than it should change it to "A". However it still leaves it as a 10. What am I doing wrong here?

#!/bin/sh

get_from_A_to_F() 
{
    case $1 in
        10) $1="A"
        ;;
        11) $1="B"
        ;;
        12) $1="C"
        ;;
        13) $1="D"
        ;;
        14) $1="E"
        ;;
        15) $1="F"     
        ;; 
        [0-9]) $1=$1
        ;;  
    esac
    echo $1
}

read number
string=""
index=`expr index $number "."`
if [ $index -eq 0 ]
then
    integer=$number
    fraction=0
else
    integer=`expr substr $number 1 $(expr $index - 1)`
    fraction=`expr "$number - $integer" | bc`
fi
result=$integer
while [ $result -ne 0 ] 
do
    remainder=`expr $result % 16`
    get_from_A_to_F $remainder
    result=`expr $result / 16`
    string=$remainder$string
done

Current output(if number read is 634):

634
test: 43: 10=A: not found
10
test: 43: 7=7: not found
7
test: 43: 2=2: not found
2
4
  • Tried it, still doesn't work. Does anyone have any other ideas? Commented Nov 2, 2012 at 17:27
  • turn on your shell debugging feature, set -x will show cmds with substituted variable values. set -vx shows current command or block (can be confusing at first) + the substituted variable values. Put one of those just at the top of your function, and you can turn the same off with set +x OR set +vx. Then you'll be able to see what is happening inside your script. (try eval again, either I don't understand what you're trying to achieve, or something is wonky). Also please edit you Q to include sample usage with expected output and current output. Good luck. Commented Nov 2, 2012 at 17:50
  • If the number read is 634 the output of my script is: 634 test: 43: 10=A: not found 10 test: 43: 7=7: not found 7 test: 43: 2=2: not found 2 Commented Nov 2, 2012 at 17:57
  • sorry, impossible to read this as a comment. Please edit your question with this info. Good luck. Commented Nov 2, 2012 at 18:20

1 Answer 1

1

To assign to $1, you do: set -- value

get_from_A_to_F is not a great function name for converting to hex. Try this:

to_hex() { printf "%X\n" "$1"; }

read number

regex1='^[+-]?[0-9]+(\.[0-9]*)?$'
regex2='^[+-]?[0-9]*\.[0-9]+$'
if ! [[ $number =~ $regex1 || $number =~ $regex2 ]]; then
    echo "doesn't look like a number: $number"
else
    integer=${number%.*}
    fraction=${number#*.}
    (( integer == fraction )) && fraction=0   # no decimal point
    hex=$(to_hex $integer)
    # you don't use $fraction anywhere?
fi
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.