LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 05-30-2014, 03:14 AM   #1
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Rep: Reputation: Disabled
add floating numbers


Hi,

i have assigned some floating numbers to the below variables, and want to add, however it is giving below error:

TOT_TRNXS=$(($succ_trnx + $fas_fail_trnx + $flcon_fail_trnx))
-bash: + + : syntax error: operand expected (error token is " ")

can we do addition with above syntax?
 
Old 05-30-2014, 03:21 AM   #2
eklavya
Member
 
Registered: Mar 2013
Posts: 622

Rep: Reputation: 136Reputation: 136
Try this.
Quote:
TOT_TRNXS=$(echo "$succ_trnx + $fas_fail_trnx + $flcon_fail_trnx" | bc)
echo $TOT_TRNXS
 
Old 05-30-2014, 04:52 AM   #3
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by eklavya View Post
Try this.
Its working, however i am getting below error:
$ ./vplus_hr_Test_Ver.sh
(standard_in) 1: parse error
(standard_in) 1: parse error
(standard_in) 1: parse error
(standard_in) 1: parse error
(standard_in) 1: parse error
(standard_in) 1: parse error
(standard_in) 2: parse error
(standard_in) 1: parse error
(standard_in) 1: parse error
(standard_in) 2: parse error
(standard_in) 2: parse error
(standard_in) 2: parse error
(standard_in) 2: parse error
(standard_in) 2: parse error


here is the script code:
Code:
#!/bin/ksh

SCRPT_DIR="/home/www/BCARD/scripts/vplus"
INPUT_DATA="/home/www/BCARD/data/vplus/vision_pls.txt"
VPLUS_DATA="/home/www/BCARD/data/vplus/vplusdata.txt"
VPLUS_TOTAL="/home/www/BCARD/data/vplus/vplustotal.txt"
VPLUS_XML="/home/www/BCARD/custom/CEMV/VPLUS"
#--------------------------------------------------------------------------------------------------------#

if [[ ! -f $INPUT_DATA ]]
then
echo "File : $INPUT_DATA not found. Exiting......"
exit 1
fi

if [[ ! -f $VPLUS_DATA ]]
then
#echo "Date|Hour|Successful Transactions|Successful Transactions Amount|FAS Failure Transactions|FAS Failure Transactions Amount|Falcon Failure Transactions|Falcon Failure Transactions Amount" >$VPLUS_DATA

echo "Date|Hour|Succ Trnxs|Succ Trnxs Amount|FAS Failure Trnxs|FAS Failure Trnxs Amt|Falcon Failure Trnxs|Falcon Failure Trnxs Amt" >$VPLUS_DATA
echo "Date|Hour|Total No. Trnxs|Total Trnxs Amt|Total Declined Trnxs Amt|FAS Total Declined Trnxs|Falcon Total Declined Trnxs" >$VPLUS_TOTAL

fi
#--------------------------------------------------------------------------------------------------------#
# Concatenating INPUT_DATA to form $VPLUS_DATA
#--------------------------------------------------------------------------------------------------------#
file=$INPUT_DATA

while IFS=\| read DATE TIME LABEL VALUE  # IFS = Internal File Seperator
do
        if [[ $LABEL == "Successful Transactions" ]]
        then
        succ_trnx=$(echo "${VALUE}" | sed -e 's/^ *//g;s/ *$//g')
        fi

        if [[ $LABEL == "Successful Transactions Amount" ]]
        then
        succ_trnx_amt=$(echo "${VALUE}" | sed -e 's/^ *//g;s/ *$//g')
        fi

        if [[ $LABEL == "FAS Failure Transactions" ]]
        then
        fas_fail_trnx=$(echo "${VALUE}" |sed -e 's/^ *//g;s/ *$//g')
        fi

        if [[ $LABEL == "FAS Failure Transactions Amount" ]]
        then
        fas_fail_trnx_amt=$(echo "${VALUE}" | sed -e 's/^ *//g;s/ *$//g')
        fi

        if [[ $LABEL == "Falcon Failure Transactions" ]]
        then
        flcon_fail_trnx=$(echo "${VALUE}" | sed -e 's/^ *//g;s/ *$//g')
        fi

        if [[ $LABEL == "Falcon Failure Transactions Amount" ]]
        then
        flcon_fail_trnx_amt=$(echo "${VALUE}" | sed -e 's/^ *//g;s/ *$//g')
        fi

        hour=$(echo ${TIME} | cut -c1-2)
        date1=$DATE

        TOT_TRNXS=$(scale=2;echo "$succ_trnx + $fas_fail_trnx + $flcon_fail_trnx"|bc)
        TOT_FAS_DCLD_TRNXS=${fas_fail_trnx}
        TOT_FLCN_DCLD_TRNXS=${flcon_fail_trnx}
        TOT_TRNX_AMT=$(echo "$succ_trnx_amt + $fas_fail_trnx_amt + $flcon_fail_trnx_amt"|bc)
        TOT_DCLD_TRNXS_AMT=$(echo "$fas_fail_trnx_amt + $flcon_fail_trnx_amt"|bc)

done <"$file"
echo "$date1|$hour|$succ_trnx|$succ_trnx_amt|$fas_fail_trnx|$fas_fail_trnx_amt|$flcon_fail_trnx|$flcon_fail_trnx_amt" >>$VPLUS_DATA
echo "$date1|$hour|$TOT_TRNXS|$TOT_TRNX_AMT|$TOT_DCLD_TRNXS_AMT|$TOT_FAS_DCLD_TRNXS|$TOT_FLCN_DCLD_TRNXS" >>$VPLUS_TOTAL

chmod 777 $VPLUS_DATA $VPLUS_TOTAL
how to resolve it?

Last edited by santosh0782; 05-30-2014 at 04:53 AM.
 
Old 05-30-2014, 05:00 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,904

Rep: Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921
you need to use #!/bin/bash (instead of ksh)
also insert set -vx at the beginning of the script to see what's happened during execution
 
1 members found this post helpful.
Old 05-30-2014, 07:54 AM   #5
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
i have x=32.765 and y=98.654, i am adding these 2 floating numbers using awk:

$ awk "BEGIN {print $x+$y; exit}"
131.419

how could i get the output upto 2 decimal places?
 
Old 05-30-2014, 08:02 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,904

Rep: Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921
see printf function of awk
 
1 members found this post helpful.
  


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
comparisions of floating point numbers bhatia.ankur8 Programming 3 09-13-2011 12:53 PM
LXer: The trouble with rounding floating point numbers LXer Syndicated Linux News 0 08-12-2006 02:54 PM
IEEE 754 floating point numbers dmail Programming 3 02-05-2006 06:10 AM
modulo of 2 floating point numbers? jenna_h Programming 6 09-24-2004 10:39 AM
floating point numbers in java spyghost Programming 8 09-06-2003 07:21 PM

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

All times are GMT -5. The time now is 11:06 PM.

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