Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: BASH script to get the exchange rate of two currencies

  1. #1
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    BASH script to get the exchange rate of two currencies

    Code:
    
    #!/bin/bash
    #by GrouchyGaijin
    # I called this script exrate.
    
    
    echo "This script is for getting the current exchange rate "
    read -p "Enter the symbol of the currency you want to convert from. Ex: SEK " var1
    read -p "Enter the symbol of the currency you want to convert to. Ex: USD  " var2
    read -p "Enter the amount you want to convert: " var3
    
    
    wget -q -O - "http://www.google.com/finance/converter?a=$var3&from=$var1&to=$var2"|grep "<div id=currency_converter_result>"|sed 's/<[^>]*>//g'
    until false; do
        echo "Press R to run the script again  or Q to quit. "
        read x
        if [ "$x" = "R" ]; then
        exrate
        elif [ "$x" = "Q" ]; then
            echo "Going down"
            killall exrate      
            break   
    fi 
        done
    Thank you,
    GG -----------

  2. #2
    Join Date
    Aug 2013
    Location
    Germany
    Beans
    34

    Re: BASH script to get the exchange rate of two currencies

    Might I suggest some slight modifications?
    I noticed the Q and R commands not working properly, so I replaced "exrate" with "bash exrate" and "killall exrate" with "exit 0".
    You also referred to the x variable in the if else statement as "$x" string, which was changed to $x.
    Another suggested change is the use of other variable names, because var1 etc. sounds too generic.
    The rest are just minor formatting changes.

    Not trying to be picky. Hope you are okay with this.

    Code:
    #!/bin/bash
    # by GrouchyGaijin
    # I called this script exrate.
    
    
    echo "This script is for getting the current exchange rate"
    read -p "Enter the abbreviation of the currency you want to convert from. Ex: SEK " convfrom
    read -p "Enter the abbreviation of the currency you want to convert to. Ex: USD " convto
    read -p "Enter the amount you want to convert: " convamount
    
    wget -q -O - "http://www.google.com/finance/converter?a=$convamount&from=$convfrom&to=$convto" | grep "<div id=currency_converter_result>" | sed 's/<[^>]*>//g'
    
    until false; do
        echo "Press R to run the script again or Q to quit."
        read x
        if [ $x = "R" ]; then
            bash exrate
        elif [ $x = "Q" ]; then
            echo "Going down"
            exit 0 #killall exrate
            break
    fi
        done
    Last edited by Christian Heinrichs; October 30th, 2013 at 04:08 PM.

  3. #3
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: BASH script to get the exchange rate of two currencies

    Quote Originally Posted by codingaround View Post
    Might I suggest some slight modifications?
    I noticed the Q and R commands not working properly, so I replaced "exrate" with "bash exrate" and "killall exrate" with "exit 0".
    You also referred to the x variable in the if else statement as "$x" string, which was changed to $x.
    Another suggested change is the use of other variable names, because var1 etc. sounds too generic.
    The rest are just minor formatting changes.

    Not trying to be picky. Hope you are okay with this.
    But of course - thank you!
    Thank you,
    GG -----------

  4. #4
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: BASH script to get the exchange rate of two currencies

    What was not working with the Q and R commands?
    They both work as they should on my system.
    Last edited by GrouchyGaijin; October 30th, 2013 at 08:40 PM. Reason: Fixed typo
    Thank you,
    GG -----------

  5. #5
    Join Date
    Aug 2013
    Location
    Germany
    Beans
    34

    Re: BASH script to get the exchange rate of two currencies

    I start the script with "bash exchange". Entering "r" does nothing and when "R" is entered I get the error:
    Code:
    exchange: line 17: exrate: command not found
    When you input "q" nothing happens. Writing "Q" works and the script exits, but still gives out the message:
    Code:
    exrate: no process found
    Based on those facts I propose some changes.

    Line 16:
    Code:
    if [ "$x" = "R" ]; then
    to
    Code:
    if [[ "$x" = "R" || "$x" = "r" ]]; then
    Line 18:
    Code:
    elif [ "$x" = "Q" ]; then
    to
    Code:
    elif [[ "$x" = "Q" || "$x" = "q" ]]; then
    Last edited by Christian Heinrichs; November 2nd, 2013 at 05:29 PM.

  6. #6
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: BASH script to get the exchange rate of two currencies

    Well obviously if you call the script usingbash exchange trying to killall exrate will result in a no process found error. That is why I mentioned that I called the script exrate. I put the script in my PATH and call it by typing exrate.
    As for the lower case option - OK.
    Thank you,
    GG -----------

  7. #7
    Join Date
    Apr 2012
    Beans
    7,256

    Re: BASH script to get the exchange rate of two currencies

    Some comments

    1) rather than calling the script recursively, it would be more natural to put the thing you want done inside the loop - if you want to preserve the existing script structure you could simply define that as a function

    Code:
    function doit() {
    read -p "Enter the abbreviation of the currency you want to convert from: " -ei "SEK" convfrom
    read -p "Enter the abbreviation of the currency you want to convert to: " -ei "USD" convto
    read -p "Enter the amount you want to convert: " -ei "1" convamount
    
    wget -q -O - "http://www.google.com/finance/converter?a=$convamount&from=$convfrom&to=$convto" | grep "<div id=currency_converter_result>" | sed 's/<[^>]*>//g'
    }
    (I made the reads interactive so that instead of 'Ex. SEK' it actually pre-fills 'SEK' as the default value) and then instead of recursing, have a loop like
    Code:
    echo "This script is for getting the current exchange rate"
    doit
    while true; do
      doit
    done
    2) a case statement makes it easy to handle the x = Q or R input, and even to make it case-insensitive - something like

    Code:
    echo "This script is for getting the current exchange rate"
    doit
    while : ; do
    
    read -p "Press R to run the script again or Q to quit. " -n1 x
      echo
      case "$x" in
        R|r) doit
        ;;
      
        Q|q) exit
        ;;
      
        *) echo "Please enter R or Q"
        ;;
      esac
    done
    (I made another suggestion about the read, using -n1 so it's a true key press rather than waiting for a newline - see 'help read')

  8. #8
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: BASH script to get the exchange rate of two currencies

    you the man Steeldriver
    Thank you,
    GG -----------

  9. #9
    Join Date
    Aug 2013
    Location
    Germany
    Beans
    34

    Re: BASH script to get the exchange rate of two currencies

    Quote Originally Posted by GrouchyGaijin View Post
    Well obviously if you call the script usingbash exchange trying to killall exrate will result in a no process found error. That is why I mentioned that I called the script exrate. I put the script in my PATH and call it by typing exrate.
    Yes, in your initial post you mentioned that you called the script exrate. But you did not say, that you put the script in your PATH environment variable.
    Last edited by Christian Heinrichs; November 2nd, 2013 at 05:40 PM.

  10. #10
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: BASH script to get the exchange rate of two currencies

    Sorry my mistake - I didn't realize that much explanation was required.
    Thank you,
    GG -----------

Page 1 of 2 12 LastLast

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
  •