Results 1 to 8 of 8

Thread: What's wrong with my code?

  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

  2. #2
    Join Date
    Feb 2008
    Location
    Pelican Bay Correctional
    Beans
    Hidden!

    Re: What's wrong with my code?

    maybe quote $resp? ie. "$resp"....?
    I have 3 brain cells left, and two of them have restraining orders.
    Linux is just "DOS on 'steroids"

  3. #3
    Join Date
    Oct 2011
    Beans
    5

    Re: What's wrong with my code?

    Hi

    Thank you for your reply - unfortunately it did not work. Here is the output to the terminal when I run the script (it's called testin):


    hugh@computer:~/Documents/Code$ ./testin
    Is your temperature in celsius or farenheit [C/F]?
    C
    enter number in C followed by <ENTER> ==> 44
    ./testin: line 11: scale=2;9 / 5: No such file or directory
    ./testin: line 12: scale=2; * 44: command not found
    ./testin: line 13: scale2; + 32: command not found

  4. #4
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: What's wrong with my code?

    If you wan't to assign the output of a command or pipeline to a variable, then you have to use command substitution:
    Code:
    part1=$(bc <<< "scale=2;$VAR1 / $VAR2")
    EDIT:
    See what I did there? I used a here string instead of a pipeline.

    Your method should also work:
    Code:
    part1=$(echo "scale=2;$VAR1 / $VAR2" | bc)
    Last edited by sisco311; May 26th, 2012 at 03:29 PM.

  5. #5
    Join Date
    Oct 2011
    Beans
    5

    Re: What's wrong with my code?

    That works perfectly, thank you. Don't suppose you could briefly tell me what it is doing?

    Hugh

    p.s. how do I mark as solved, (and also how do I quote code like you did?)

  6. #6
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: What's wrong with my code?

    Quote Originally Posted by hugh_bristol View Post
    That works perfectly, thank you. Don't suppose you could briefly tell me what it is doing?

    Hugh
    http://mywiki.wooledge.org/CommandSubstitution

    Quote Originally Posted by hugh_bristol View Post
    p.s. how do I mark as solved,
    At the top of the page click Thread Tools -> Mark this thread as solved

    Quote Originally Posted by hugh_bristol View Post
    (and also how do I quote code like you did?)

    [code]your code her[/code]

  7. #7
    Join Date
    Oct 2011
    Beans
    5

    Re: What's wrong with my code?

    Thank you for all your help!

  8. #8
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: What's wrong with my code?

    Quote Originally Posted by hugh_bristol View Post
    Thank you for all your help!
    You are welcome!

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
  •