Results 1 to 8 of 8

Thread: What's wrong with my code?

Threaded View

  1. #1
    Join Date
    Oct 2011
    Beans
    5

    Unhappy What's wrong with my code?

    Hi

    Really sorry to bother but if anyone can help that would be amazing. I am trying to learn bash programming and functions, and so I have written a simple celcius/farenheit converter. But it doesn't work for some reason - even though all the echo to bc commmands work just fine in the terminal with the same syntax.

    Code:
    #!/bin/bash
    
    VAR1=9
    VAR2=5
    VAR3=32
    
    
    cel2far ()
    {
    read -p "enter number in C followed by <ENTER> ==> " resp
    part1=echo "scale=2;$VAR1 / $VAR2" | bc
    part2=echo "scale=2;$part1 * $resp" | bc
    answer_c2f=echo "scale2;$part2 + $VAR3" | bc
    echo "$answer_c2f"
    }
    
    far2cel ()
    {
    read -p "enter number in F followed by <ENTER> ==> " resp
    part1=echo "scale=5;$resp - $VAR3" | bc
    part2=echo "scale=5;$VAR2 * $VAR1" | bc
    answer_f2c=echo "scale=2;$part2 * $part1" | bc
    echo "$answer_f2c"
    }
    
    echo "Is your temperature in celsius or farenheit [C/F]?"
    
    read temp_type
    
    if [ $temp_type == C ]; then
         cel2far
    elif [ $temp_type == F ]; then
         far2cel
    else echo "unrecognized temp format; please type either C or F [caps]"
    fi
    Last edited by sisco311; May 26th, 2012 at 03:16 PM. Reason: accidentally posted earlier draft, sisco311: added [code] tags

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •