LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-24-2007, 01:04 PM   #16
Simon_Penney_2003
LQ Newbie
 
Registered: Jan 2007
Posts: 11

Original Poster
Rep: Reputation: 0


Thanks i'll have a read
 
Old 01-24-2007, 01:30 PM   #17
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
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
 
Old 01-25-2007, 08:14 AM   #18
Simon_Penney_2003
LQ Newbie
 
Registered: Jan 2007
Posts: 11

Original Poster
Rep: Reputation: 0
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

Last edited by Simon_Penney_2003; 01-28-2007 at 02:07 PM.
 
Old 01-25-2007, 08:42 AM   #19
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 01-25-2007, 11:24 AM   #20
whk
Member
 
Registered: Jun 2005
Posts: 202

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

Last edited by whk; 01-25-2007 at 11:51 AM.
 
Old 01-25-2007, 02:23 PM   #21
Simon_Penney_2003
LQ Newbie
 
Registered: Jan 2007
Posts: 11

Original Poster
Rep: Reputation: 0
i corrected this, but it still wont work.

surely something like this could be written in under 10 lines of code
 
Old 01-25-2007, 03:57 PM   #22
inspiron_Droid
Member
 
Registered: Dec 2006
Distribution: Debian (Wheeze)
Posts: 391

Rep: Reputation: Disabled
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
 
Old 01-25-2007, 04:24 PM   #23
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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?

Last edited by acid_kewpie; 01-25-2007 at 04:26 PM.
 
Old 01-25-2007, 04:25 PM   #24
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 01-25-2007, 06:02 PM   #25
Simon_Penney_2003
LQ Newbie
 
Registered: Jan 2007
Posts: 11

Original Poster
Rep: Reputation: 0
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.
 
Old 01-25-2007, 06:14 PM   #26
whk
Member
 
Registered: Jun 2005
Posts: 202

Rep: Reputation: 35
Did u add

read rate

right after
echo What is the exchange rate?
 
Old 01-25-2007, 07:34 PM   #27
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
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.
 
Old 01-26-2007, 08:51 AM   #28
Simon_Penney_2003
LQ Newbie
 
Registered: Jan 2007
Posts: 11

Original Poster
Rep: Reputation: 0
I've worked out how to do it now. Thanks for everyones help. You may now close this thread.
 
Old 01-26-2007, 09:20 AM   #29
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 01-26-2007, 10:45 AM   #30
Netgh0st
LQ Newbie
 
Registered: Apr 2006
Distribution: Slackware 11
Posts: 18

Rep: Reputation: 0
Amen to that bro
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic bash script question kevpatts Linux - Newbie 3 10-12-2005 02:02 PM
Basic Bash script help valadamir Programming 2 12-01-2004 11:37 AM
Trouble with a basic bash script Tengil Linux - Newbie 5 03-04-2004 01:59 PM
Basic BASH script, what's wrong??? Satriani Linux - General 2 06-02-2003 06:34 PM
help with basic syntax in bash script Supp0rtLinux Linux - Software 4 03-27-2003 07:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration