The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to assign the specific value ryanW Shell Programming and Scripting 4 04-20-2009 10:20 PM
Assign o/p of awk to a variable c2b2 Shell Programming and Scripting 8 04-01-2009 07:50 AM
how to assign the first day of the next month rosalinda Shell Programming and Scripting 6 07-05-2008 04:13 PM
Assign variables with cut douknownam Shell Programming and Scripting 2 09-27-2004 09:18 AM
assign to variable AkumaTay UNIX for Dummies Questions & Answers 1 05-18-2002 02:11 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-22-2009
navjinder navjinder is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 2
Exclamation Using bc to assign value

Friends here is code which is used to add floating point using bc, but I m not getting any output instead some errors.

Code:
1 #!/bin/bash

 
  4 if [ -c /proc/$$/fd/0 ]
  5 then
  6    echo "Our input is from a Device"
  7         while read myline
  8         do
  9
 10         total= `echo $total + $myline |bc`
 11         #((total=$total+ $myline))
 12         done
 13
 14 elif [ -p /proc/$$/fd/0 ]
 15 then
 16
 17    echo "Our input is from a pipe"
 18         while read myline2
 19         do
 20         total= `echo $total + $myline |bc`
 21         #((total= $total+ $myline2))
 22         done
 23 elif [ -f /proc/$$/fd/0 ]
 24 then
 25
 26    echo "Our input is from a file"
 27         while read myline3
 28         do
 29         add=+
 30         $(($total $add $myline))| bc
 31         #echo $total $myline
 32         #total= echo `$total $add $myline |bc`
 33         #((total= $total+ $myline))
 34         done
 35
 36
37
38   echo "Error"
 39         break;
 40 fi
 41
 42 echo total is $total

Last edited by Franklin52; 04-25-2009 at 09:00 AM.. Reason: adding code tags
  #2 (permalink)  
Old 04-25-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 489
Maybe something like this:

Code:
$ 
$ # display the file "num" that has floating-point numbers, one per line
$                                                                    
$ cat num                                                            
2.1                                                                  
3.4                                                                  
4.6                                                                  
5.2                                                                  
6.3                                                                  
$
$ # display the script
$
$ cat test_scr.sh
#!/bin/bash
NUMFILE=$1
total=0
if [ -c $NUMFILE ]
then
  echo "Our input is from a Device"
  while read LINE; do
    total= `echo $total + $LINE | bc`
  done < $NUMFILE
elif [ -p $NUMFILE ]
then
  echo "Our input is from a pipe"
  while read LINE; do
    total= `echo $total + $LINE | bc`
  done < $NUMFILE
elif [ -f $NUMFILE ]
then
  echo "Our input is from a file"
  while read LINE; do
    total=`echo $total + $LINE | bc`
  done < $NUMFILE
fi
echo total is $total
$
$ # test the script, passing "num" as input
$
$ . test_scr.sh num
Our input is from a file
total is 21.6
$
$
HTH,
tyler_durden
  #3 (permalink)  
Old 04-28-2009
navjinder navjinder is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 2
Thumbs up

Thanks tyler_durden. A good job done, yea it makes more sense thanks again
Sponsored Links
Closed Thread

Bookmarks

Tags
adding, assigning values, bc

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0