Sponsored Content
Top Forums Shell Programming and Scripting Arithmetic operation in variable Post 302980832 by greet_sed on Friday 2nd of September 2016 04:44:56 PM
Old 09-02-2016
Arithmetic operation in variable

Hi,

Here is the script i try to perform arithmetic operation in two variables .

Code:
git branch -r | while read brname ; do
REV_COMMITS=`git rev-list --count $brname`
echo "$brname has $REV_COMMITS"
(( TOTAL = TOTAL + REV_COMMITS ))
echo "in loop" $TOTAL
done
echo "total is " $TOTAL

Output:
Quote:
x has 13326
in loop 13326
y has 17459
in loop 30785
z has 1170
in loop 31955
total is 0
In side the loop, i get correct value of variable TOTAL.
How to get correct TOTAL outside while loop ?
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple arithmetic operation

I have no idea why I can't get this to work, if anybody can help i would appreciate it. #!/bin/bash x=`cat counter.txt | wc -l` y= '$x / 7' printf "%d People have visited this page" $y :confused: (2 Replies)
Discussion started by: paladyn_2002
2 Replies

2. Shell Programming and Scripting

Help with arithmetic operation

I am using egrep to extract numbers from a file and storing them as variables in a script. But I am not able to do any arithmetic operations on the variables using "expr" because it stores them as char and not integers. Here is my code and the error I get. Any help will be appreciated. #!/bin/sh... (3 Replies)
Discussion started by: emjayshaikh
3 Replies

3. UNIX for Dummies Questions & Answers

arithmetic operation on two columns

Hi, All, I have a file, its content is as follows: 100 150 120 135 140 170 I want to insert a column, its content is determined by the difference between the two values in the same line, if the difference is less than 20, the new value is 1, otherwise is 0. after the operation, the... (1 Reply)
Discussion started by: Jenny.palmy
1 Replies

4. Shell Programming and Scripting

How to perform arithmetic operation on date

Hi all, I would appreciate if anyone knows how to perform adding to date. As for normal date, i can easily plus with any number. But when it comes to month end say for example 28 Jun, i need to perform a plus with number 3, it will not return 1 Jul. Thanks in advance for your help. (4 Replies)
Discussion started by: agathaeleanor
4 Replies

5. Shell Programming and Scripting

Arithmetic operation with awk

I have output like following in a file usmtnz-dinfsi19 72 71 38 1199 1199 0.8 19:23:58 usmtnz-dinfsi19 72 71 38 1199 1199 0.8 19:24:04 (9 Replies)
Discussion started by: fugitive
9 Replies

6. Shell Programming and Scripting

Arithmetic operation between columns using loop variable

Hi I have a file with 3 columns. say, infile: 1 50 68 34 3 23 23 4 56 ------- ------- I want to generate n files from this file using a loop so that 1st column in output file is (column1 of infile/(2*n+2.561)) I am doing like this: for ((i=1; i<=3; i++)) do a=`echo... (3 Replies)
Discussion started by: Surabhi_so_mh
3 Replies

7. Shell Programming and Scripting

floating point arithmetic operation error

I am writing a script in zsh shell, it fetchs a number from a file using the awk command, store it as a variable, which in my case is a small number 0.62000. I want to change this number by multiplying it by 1000 to become 620.0 using the command in the script var2=$((var1*1000)) trouble is... (2 Replies)
Discussion started by: piynik
2 Replies

8. UNIX for Dummies Questions & Answers

Basic arithmetic operation with awk?

input: Name|Operation rec_10|1+2+2- Output: rec_10|1 Basically I am trying to calculate the result of "the path" in $3 where the operators follow the number and not preceding them like we normally do: rec_10: +1+2-2=1 But I realise (I am sure there is a good reason for that) that awk... (7 Replies)
Discussion started by: beca123456
7 Replies

9. Shell Programming and Scripting

Using awk to do arithmetic operation

Hi, I've this following text file FileVersion = 1.03 Filetype = meteo_on_curvilinear_grid TIME = 0 hours since 2016-10-03 12:00:00 +00:00 -6.855 -6.828 -6.801 -6.774 -6.747 -6.719 -6.691 -6.663 -6.634 -6.606 -6.577 -6.548 -6.519 -6.489 TIME = 0 hours since... (2 Replies)
Discussion started by: xisan
2 Replies

10. How to Post in the The UNIX and Linux Forums

How to get defined precision after arithmetic operation using syncsort?

I have to do some arithmetic operation on Field 8 which is calculated by Field 9/Field 7 Suppose i have data like : 0800123456|JAN|2017|JAN|2018|0800123456|0|0.0000|0.00| 0800234567|JAN|2017|JAN|2018|0800234567|4|2.5812|10.32| 0800666666|JAN|2017|JAN|2018|0800666666|2|1.7255|3.45|... (0 Replies)
Discussion started by: pumrao
0 Replies
CHECK_INT32_ADD(3)					   BSD Library Functions Manual 					CHECK_INT32_ADD(3)

NAME
check_int32_add, check_uint32_add, check_int64_add, check_uint64_add, check_int32_sub, check_uint32_sub, check_int64_sub, check_uint64_sub, check_int32_mul, check_uint32_mul, check_int64_mul, check_uint64_mul, check_int32_div, check_uint32_div, check_int64_div, check_uint64_div, -- detect overflow in arithmetic SYNOPSIS
#include <checkint.h> int32_t check_int32_add(int x, int y, int *err); uint32_t check_uint32_add(int x, int y, int *err); int64_t check_int64_add(int x, int y, int *err); uint64_t check_uint64_add(int x, int y, int *err); int32_t check_int32_sub(int x, int y, int *err); uint32_t check_uint32_sub(int x, int y, int *err); int64_t check_int64_sub(int x, int y, int *err); uint64_t check_uint64_sub(int x, int y, int *err); int32_t check_int32_mul(int x, int y, int *err); uint32_t check_uint32_mul(int x, int y, int *err); int64_t check_int64_mul(int x, int y, int *err); uint64_t check_uint64_mul(int x, int y, int *err); int32_t check_int32_div(int x, int y, int *err); uint32_t check_uint32_div(int x, int y, int *err); int64_t check_int64_div(int x, int y, int *err); uint64_t check_uint64_div(int x, int y, int *err); DESCRIPTION
The check_<type>_<operation>(x, y, err) family of functions perform the specified arithmetic operation (addition, subtraction, multiplica- tion, or division) with the left operand of x and right operand of y and return the arithmetic result with the specified type. Either operand x or y (or both) can be of any type that is compatible to signed or unsigned 8-bit, 16-bit, 32-bit, or 64-bit integers. The err argument is or'ed by flags in the function to indicate if an overflow has occurred. The possible flag values are: CHECKINT_NO_ERROR no overflow has occurred CHECKINT_OVERFLOW_ERROR overflow has occurred CHECKINT_TYPE_ERROR operand is of an incompatible type The err argument is not cleared in calls to the check_<type>_<operation>(x, y, err) functions. Detected overflow persists in the err argu- ment until err is reset to CHECKINT_NO_ERROR. RETURN VALUES
If successful, the check_<type>_<operation>() functions will return the arithmetic result of performing the operation with left operand x and right operand y (even when overflow error occurs). If any other error occurs, the return value is -1 and the argument err will be set to indicate the error. EXAMPLES
/* Create a variable to store overflow flag */ int32_t err = CHECKINT_NO_ERROR; /* Use checkint API to perform an arithmetic operation and * store result in variable. */ int32_t arithmetic_result = check_int32_add(UINT_MAX, 1, &err); /* Check status of overflow flag */ if (err & CHECKINT_OVERFLOW_ERROR) { /* Perform overflow resolution code */ fprintf(stderr, "Overflow detected! "); } /* Check for type error */ else if (err & CHECKINT_TYPE_ERROR) { /* Deal with incompatible types error */ fprintf(stderr, "Incompatible types! "); } /* Reset overflow flag for next operation */ err = CHECKINT_NO_ERROR; ERRORS
The check_<type>_<operation>() functions may fail if: [CHECKINT_TYPE_ERROR] operand is of an incompatible type HISTORY
The checkint() API was introduced in Mac OS X 10.5. BSD
April 20, 2007 BSD
All times are GMT -4. The time now is 09:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy