LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help - Tring to write basic currency Converter as a bash script (https://www.linuxquestions.org/questions/linux-newbie-8/help-tring-to-write-basic-currency-converter-as-a-bash-script-522324/)

Simon_Penney_2003 01-24-2007 12:04 PM


Thanks i'll have a read

nx5000 01-24-2007 12:30 PM

Quote:

Originally Posted by unSpawn
Code:

A=100; B=2; C="0.93"; D=`echo "scale=2;($A-$B)/$C"|bc -l 2>/dev/null`

LOL :)

You have to read the documents that were given. Don't expect to learn bash in 10mn.
Try one of the example and add your little bit, modify the code, see what it gives. It was my way of understanding computer languages. You should practice, not only read.

Meanwhile, try this:
Code:

bc
you are now in an interactive calculator

Code:

100-2
gives you
Code:

98
Code:

98/0.93
gives you
Code:

105
Because bc by default only works on integer

Code:

scale=2
this will tell bc to give you 2 decimals after .

Code:

98/0.93
gives you
Code:

105.37
That's for bc

if you want to quit bc, type on a newline
Code:

quit
You get back to your shell

Now you have to understand the concept of pipes (read the manuals again).
Code:

command1|command2
A pipe is something that will take the result of its command on the left and put it as the input of the command on the right.
In other words, it's like if you were doing command1 which gives result X and then you run command2 and you copy paste X.
For this, command2 has to be a command that is asking for something, like bc, not like ls (when you type ls, your shell doesn't ask you anything)

Now you want to use bc without typing anything. Retype what was above and put a ; between each command
Code:

echo "scale=2;98/0.93" | bc -l
so the output of command1 (echo) which is "scale=2;98/0.93" will be injected in command 2 (bc -l)

and your shell displays the result:
Code:

105.37
That's for specific values, if you want the user to put his values, reread unSpawn line
Code:

A=100; B=2; C="0.93"; D=`echo "scale=2;($A-$B)/$C" | bc -l`
I hope you can understand at least the english part of this post :)

Simon_Penney_2003 01-25-2007 07:14 AM

I've now identified that i need to use bc instead of let, to get the total. But the script still doesnt work.

echo please enter the amount in `$` that you wish to convert
| bc[/COLOR]ou entered converts to $total

acid_kewpie 01-25-2007 07:42 AM

ok you have an opening ` but no closing one. i assume you don't know what `'s do? they run the contents of the quotes, so in your case you would want to exectue the echo to make the mathematical expression, pipe it to bc and capture the output, so the entire thing shoudl be in `'s (backticks) actually, backticks are old now, and you shoudl be using $( command ) style instead, which i think is a lot clearer for learning anyway.

whk 01-25-2007 10:24 AM

total=`echo "scale=2; $amount * $rate" | bc`
# Or use scale=3 to produce the whole amount cause you need to round off.

Simon_Penney_2003 01-25-2007 01:23 PM

i corrected this, but it still wont work.

surely something like this could be written in under 10 lines of code

inspiron_Droid 01-25-2007 02:57 PM

You also need thins called scholar values which are designated by the use of the dollar sign in perl.

examples of scholar values are as follows:

$total
$intrest

acid_kewpie 01-25-2007 03:24 PM

Quote:

Originally Posted by Simon_Penney_2003
i corrected this, but it still wont work.

surely something like this could be written in under 10 lines of code

yes, i could write it in one if i really wanted something that looked awful, but it's *you* doing the learning, not us... you know it'd be really helpful if you actually told us what the errors were, rather than making us guess and assume all the time, you know?

acid_kewpie 01-25-2007 03:25 PM

Quote:

Originally Posted by badboy88
You also need thins called scholar values which are designated by the use of the dollar sign in perl.

examples of scholar values are as follows:

$total
$intrest

what are you talking about?? you mean scalar variables right? and that's really not relevant here. if you'd read the thread, or even the title of it you'd see this is about bash, not perl. :)

Simon_Penney_2003 01-25-2007 05:02 PM

Quote:

Originally Posted by acid_kewpie
if you actually told us what the errors were, rather than making us guess and assume all the time


Sorry the errors are as follows:

line 1: $: command not found

(standard in parse error)



Thats it. It doesnt actually output any numbers at all. From my reading i've gathered that i may need to assign the "Euro & pounds" as some sort of variable. I may be wrong. But i dont seem to have made progress, i cant even get a number to appear as the result, yet alone the correct conversion.

whk 01-25-2007 05:14 PM

Did u add

read rate

right after
echo What is the exchange rate?

unSpawn 01-25-2007 06:34 PM

Quote:

echo please enter the amount in `$` that you wish to convert
line 1: $: command not found

I could point to "A Guide to Unix Shell Quoting" but in short: using backticks in line one without using single quotes encapsulating the echoed string is the problem, since it will have the shell interprete the `$` part as a command. Sorter: do not use backticks unless you know you should. Before you continue with the next problem I suggest you cooperate by rereading this thread and the docs mentioned.

Simon_Penney_2003 01-26-2007 07:51 AM

I've worked out how to do it now. Thanks for everyones help. You may now close this thread.

acid_kewpie 01-26-2007 08:20 AM

can i suggest for your future questions, provide error messages, provide code, provide the answer, not just say it's working. that way other people in your situation can get as much from the thread as you did in the first palce.

Netgh0st 01-26-2007 09:45 AM

Amen to that bro


All times are GMT -5. The time now is 11:25 AM.