Register
It is currently Sat Dec 20, 2014 11:44 pm

Need help with bc floating point script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue Sep 18, 2012 2:31 pm   

Joined: Tue Sep 18, 2012 2:16 pm
Posts: 2
Hi all. Just started learning shell scripting and picked up the Linux Command Line and Shell Scripting Bible (2nd ed). I can't get a script to work that is presented in the book on page 273:

Code:
#!/bin/bash
var1='echo " scale=4; 3.44 / 5" | bc'
echo The answer is $var1


When the script is ran it outputs this:

The answer is echo " scale=4; 3.44 / 5" | bc

What is wrong with the example the book is giving?

Thanks in advance!


Top
 Profile  
 PostPosted: Tue Sep 18, 2012 4:18 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
hi,

you're using wrong quotes: ' this is single quote
you need to use back ticks: `
or latest command substitution format $(command)

so
Code:
#!/bin/bash
var1=$(echo " scale=4; 3.44 / 5" | bc)
echo "The answer is $var1"


Top
 Profile  
 PostPosted: Thu Sep 27, 2012 8:18 am   

Joined: Tue Sep 18, 2012 2:16 pm
Posts: 2
Yup that was the issue and it works now. Thanks for the reply!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP