Using bc to assign value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using bc to assign value
# 1  
Old 04-22-2009
Error 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 10:00 AM.. Reason: adding code tags
# 2  
Old 04-25-2009
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  
Old 04-28-2009
MySQL

Thanks tyler_durden. A good job done, yea it makes more sense thanks again Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check assign

I have a person script which has a following statement. BUILD_FOLDER=$2 i=$((${#BUILD_FOLDER}-1)) if then BUILD_FOLDER=$BUILD_FOLDER/ #echo $BUILD_FOLDER else echo " " #echo $BUILD_FOLDER fi What and how this statement works ? i=$((${#BUILD_FOLDER}-1)) (5 Replies)
Discussion started by: ilugopal
5 Replies

2. Shell Programming and Scripting

Unable to assign a value

I have written a shell script to calculate dbsize :- db2 "call get_dbsize_info(?,?,?,-1)" | sed -n '8p' | awk -F : '{print $2}' dbsize=`db2 "call get_dbsize_info(?,?,?,-1)" | sed -n '8p' | awk -F : '{print $2}'` echo $dbsize when I execute it the syntax works but it's not... (11 Replies)
Discussion started by: lazydev
11 Replies

3. UNIX for Dummies Questions & Answers

Assign dir name

Hi guys i need to assign to a $var the name of a directory i've created before like this: mkdir cd /esb/cargomax/fuse/SMX_SV_$(date +%F) i guess this is very simple, but..... Thanks!! (3 Replies)
Discussion started by: Newer
3 Replies

4. Linux

HOW TO assign IP in REDHAT 6?

Hi Guys, It may sound silly questions but I am really confused and forgot the sequence to set IP in REDHAT 6. 1st type system-config-network then I give IP as 192.168.1.78 Subnet as 255.255.255.0 Then do /etc/init.d/network restart Then when I check with ifconfig eth0... (1 Reply)
Discussion started by: manalisharmabe
1 Replies

5. Shell Programming and Scripting

how to assign value

#! /bin/bash if ; then echo "Set number " else k=$1 sqlplus ${scheme}/${apsswd}@${server} @query.sql $k fi file query.sql looks like this select * from tab1 where number =${k}; =================================== it doesnt work my question is how to assign k value in last... (2 Replies)
Discussion started by: kvok
2 Replies

6. Shell Programming and Scripting

Assign the value

DATA --------------- 0 Please tell me, if the file contains 0 after --. then assign the value to variable $var=false, DATA --------------- 1 then $var=true, (2 Replies)
Discussion started by: sandy1028
2 Replies

7. Shell Programming and Scripting

Not able to assign a value to variable

Hi Experts, I am facing some problem while developing the script.My input config.csv file contains the three columns namely pathname,filename,filetype.Based on the file type i have to use ftp command that is if filetype=csv then do ftp. The input file is cat config.csv... (13 Replies)
Discussion started by: Amey Joshi
13 Replies

8. Shell Programming and Scripting

Assign a sting to a name?

i want to assign a anme for a string, i got the things below, but it doesn't work. MParc1=`` if then $MPrac1=`Prac1` # assign $MPrac1 to Prac1 else $MPrac1=`` #assigne $MPrac1 to nothing fi echo "${MPrac1} >>file Output: (if num is 0) Prac1 can you please... (6 Replies)
Discussion started by: mingming88
6 Replies

9. Shell Programming and Scripting

How to assign the specific value

Hi Everyone, How to assign the specific value which return from database? here is the return value from database --> (return status = 0) 0 <----- this I only need to get the "0" .. assign to another declare variable. hope someone will help me.. Please thank you.. (4 Replies)
Discussion started by: ryanW
4 Replies

10. Shell Programming and Scripting

assign a value to variable

I have to assign a result of a query to a vairable like this how can i do this Query = select count(*) from table x=`db2 ${Query}| sed -n '4p'` but this doesn't work, is there any other way to assign the result without redirecting the result to temp file. . Thanks Mark. (3 Replies)
Discussion started by: markjason
3 Replies
Login or Register to Ask a Question