Questions tagged [arithmetic]

This tag is meant for everything that has to do with mathematical operations. So if you want to ask a question about doing calculations in `awk`, or if you have a question about `bc`, this is the right tag to use.

Filter by
Sorted by
Tagged with
0 votes
4 answers
106 views

how to subtract a date in format: 'xyz/[int][int]/[int][int][int][int]' from the current date?

Scenario I have containers which have last deploy dates in the format: month/date/year or feb/11/2024. I cant change the way they output the last deploy date. I'd like to subtract the last deploy date,...
Nickotine's user avatar
  • 467
-1 votes
2 answers
68 views

Numeric if conditions that read a command in a bash script

I'm trying to write a command that looks at my battery state and suspends the laptop if the battery is low. I tried using something like this: #!/bin/bash if [ $(acpi -b | cut -c 25) < 11 ] then ...
Alexander Praehauser's user avatar
0 votes
1 answer
31 views

Logging sum of Mem and Swap from free command output

In relation to this: https://unix.stackexchange.com/a/754252/582781 Solution 1: free -g -s2 | sed -u -n 's/^Mem:\s\+[0-9]\+\s\+\([0-9]\+\)\s.\+/\1/p' >> memory.log Is there a way to add Swap to ...
Aleksander's user avatar
-4 votes
1 answer
52 views

How to protect variable name inside shell arithmetic?

Variables can be protected with curly braces. Mynewvar=1 echo $Mynewvar 1 echo ${Mynewvar} 1 However I can no longer protect them when inside shell arithmetic. Mynewvar=1 echo $((Mynewvar+9)) 10 echo ...
jwav629's user avatar
3 votes
4 answers
376 views

Apply arithmetic into piped command

So far I have this: sudo find /path/to/dir -type f | xargs -d "\n" sudo stat -c "%Y %n" | {arithmetic to check if %Y is between 1685518962 and 1685624474??} | {show ...
MonkeyZeus's user avatar
1 vote
1 answer
97 views

Arithmetic operation issue in bash script | invalid arithmetic operator

From a bash script I am downloading a file from a server using curl call. Now I want to check if the file is fully downloaded. For this I am comparing the size of downloaded file and Content-Length ...
Preeti 's user avatar
  • 223
-5 votes
1 answer
168 views

printing prime number in bash [closed]

hi i am new to programming and currnetly learing bash script. please help with this error " line 28: expected `)' " Given below is my program. read -p "enter the number:" a while [...
KNIGHTSS's user avatar
-1 votes
1 answer
658 views

What is an operand?

I just want someone to explain what an "operand" is because I just don't understand it. Here is the context I'm reading this in: In the section about Shell Arithmetic in the bash shell ...
john smith's user avatar
5 votes
1 answer
1k views

How does arithmetic expansion work with variables without the $ (dollar) sign?

I think the line below should be written differently, and the command should bug out. $ echo $((x=2, y=3, x+y)) 5 I thought the way to write this was: $ echo $((x=2, y=3, $x+$y)) 5 Both work, which ...
john smith's user avatar
2 votes
1 answer
117 views

Arithmetic operation in terminal from an output

I want to print the lines of updates via this command dnf check-update --refresh --q --downloadonly | wc -l However during the output there occurs a blank line which means the true update number is ...
Neptune's user avatar
  • 153
0 votes
2 answers
61 views

Unable to put output of basic calculator in a variable, formatting bytes to MB in bash

I want to get my data usage from my wifi device to display on my top bar, I can get the data usage in bytes using curl like this $bytes=(curl -d "Page=GetWANInfo" -X POST http://jiofi.local....
adumbProgrammer's user avatar
0 votes
1 answer
178 views

Evaluate a integer expression in bash

I have a set of piped commands that generates an integer expression. A sample looks like (1 +(0x1f+0x02)) I can evaluate this expression (get the resulting value), by enclosing it in $(), so: $ echo $(...
rookie's user avatar
  • 135
4 votes
4 answers
1k views

Precision of the date command and arithmetic

I take a date at nanosecond precision: $ start=$(date '+%s.%N') ...then print it: $ echo ${start} 1662664850.030126174 So far so good. But look what I get when I printf with some arbitrarily huge ...
The Poopsmith's user avatar
2 votes
3 answers
992 views

Replicating floating point arithmetic WITHOUT bc utility

I'm needing to write a script on some embedded Linux systems that don't have bc installed on them and won't be able to have it installed. The script I'm writing is essentially a monitoring script that ...
William's user avatar
  • 43
2 votes
3 answers
761 views

Performing calculation on the contents of two files

file1 contains 100 as its content, and file2 contains 200. How would I perform calculation on those two numbers in the terminal? Say I would like to perform division on those two numbers. My approach ...
Terrarium's user avatar
  • 121
0 votes
1 answer
129 views

How to replace text in file - conditions and arithmetic required

I need a script to change content in some text files. This a bit beyond my limited bash skills. Maybe perl or python would be a better approach, I have no experience with either of these. I have a ...
jamitin's user avatar
11 votes
2 answers
4k views

bash calc returns 800 (should be 600): echo "$(( 800 / (4 / 3) ))"

I'm calculating aspect ratio height from x number, in this example I'm using 4:3 ratio, and a width of 800, the result (height) should be 600, but bash is returning 800, and I'm not sure why. I've ...
Mint's user avatar
  • 265
1 vote
1 answer
249 views

Sum of timestamps in unix [closed]

I need a code to add 2 timestamps of format HH:MM:SS in unix, can anybody help? Unfortunately, I have stored timestamp as varchar in the database, in the format (e.g.) 07:53:39 Now I want sum of that ...
New user's user avatar
0 votes
1 answer
252 views

sed giving invalid arithmetic operator error

sed keeps giving invalid arithmetic operator error. I am trying to assign the output of sed to a variable. This one works, var=$(sed -e 's/"currentGeneration":5010/"currentGeneration&...
Ram's user avatar
  • 25
-1 votes
1 answer
390 views

How to round to the correct nearest number in Linux bc?

How do we round to the correct nearest number in a Linux basic calculator BC?
user avatar
2 votes
4 answers
420 views

Multiply numbers between two different patterns and print the whole file

I would like to replace number between patterns with multiplicated numbers and print the all the lines. The file is a tree file in newick format and consisted only a single line. My targets are all ...
web's user avatar
  • 193
7 votes
3 answers
3k views

Handling 64-bit integers in a shell script

I am trying to calculate the used bandwidth on the Ethernet interface (which is 1000 Mbit/s). To test my script, I am using the iperf tool to generate huge bandwidths. The problem I am facing is when ...
Cheppy's user avatar
  • 63
0 votes
3 answers
474 views

Is parameter expansion in test expression possible?

When I try the following expression in bash I get a strange error message: [: -lt: unary operator expected First the function definition some_func () { (( 3 + 5 )) } And the expression [ $(...
von spotz's user avatar
  • 435
3 votes
1 answer
788 views

Check if a number is in a range with zsh

Why is passing 0 as an argument results in a false positive (prints "True")? #!/bin/zsh k="$1" if ((0 < k < 1)) then echo "True" fi Note this script is ...
visual360's user avatar
1 vote
2 answers
272 views

How bash interprets triple parentheses?

I see that in bash the command echo $(((i=18))) prints 18. This makes me understand that $(((i=18))) is interpreted as an arithmetic expansion (with the variable i being initialized inside the ...
diciotto's user avatar
9 votes
4 answers
2k views

Bitwise operations in Bash not working as expected

I ran into a strange problem. To demonstrate, let's take the largest unsigned number on my machine (printf "%X \n" -1 gives me FFFFFFFFFFFFFFFF), and try to shift some bits.  First, shift ...
Pourko's user avatar
  • 1,834
11 votes
3 answers
2k views

Find the largest number that Bash arithmetic can handle?

How can I let my script determine the largest number for itself? I looked through my environment variables, and I found these two that looked promising: ~# declare -p BASH_VERSINFO HOSTTYPE declare -...
user avatar
1 vote
2 answers
139 views

Arithmetic between 2 files generating a series of new files using awk

I have a tab separated model input file i would like to vary for ensemble analysis formatted such as this cat input.txt ############################################# ### Parameter file for the ...
sethparker's user avatar
0 votes
1 answer
51 views

Arithmetic between 2 files generating a series of new files (Pt 3)

I have a tab separated model input file I would like to vary for an ensemble analysis formatted similar to this cat input.txt /* Precipitation in mm */ 10 30 15 20 ...
sethparker's user avatar
0 votes
1 answer
48 views

Arithmetic between 2 files generating a series of new files (Pt 2)

This is a more specific follow-up to a previous question (Arithmetic between 2 files generating a series of new files). I have a tab separated model input file I would like to vary for an ensemble ...
sethparker's user avatar
2 votes
6 answers
128 views

Arithmetic between 2 files generating a series of new files

I have a tab separated model input file I would like to vary for an ensemble analysis formatted similar to this input.txt /* Preciptation in mm */ 10 30 40 50 23 ### Species description *** sp_name ...
sethparker's user avatar
0 votes
2 answers
49 views

addition by specific value of arithmetic values presented in file1 to file2

I have in file 1 the following 'a 62-182' 'a 183-303' 'a 304-424' ... is there any way I can have based on file1's value the following in file 2? 'a 62-182' 'a 7195-7315' 'a 183-303' 'a 7316-7436' ...
Dimitris Mintis's user avatar
4 votes
1 answer
304 views

In ZSH, what arithmetic expressions can appear as array subscript?

The ZSH manual (zshparam(1)) reads: Array Subscripts Individual elements of an array may be selected using a subscript. A subscript of the form `[exp]' selects the single element exp, ...
Michaël's user avatar
  • 774
0 votes
3 answers
2k views

Divide by zero exception using awk

i have values stored in variables var1=6 and var2=4, i am trying to divide them with a precision of 2 and store the result in a variable var, var=`awk 'BEGIN {printf "%.2f\n", $var1/ $var2}'`...
SomeoneNew2Unix's user avatar
0 votes
0 answers
16 views

BASH: arithmetic expressions on point values [duplicate]

I am looking for bash function suitable to do some simple arithmetic expressions on point values For intance i=1 let "i_mod=${i}+1"; echo $i_mod gives me 2 but if i=1.0 let "i_mod=${i}+...
user3470313's user avatar
10 votes
6 answers
1k views

Arithmetic on values with memory size units

Let’s say I have a bunch of numbers representing quantities of memory, written in the form 86k or 320m or 1.7g for instance. How can I compute their sum in command line, and get back a human-readable ...
Maëlan's user avatar
  • 426
1 vote
3 answers
582 views

How do I sum up the results of multiple bash commands in bash?

So I've got the following cat a1 | wc -l // term1 cat a2 | wc -l // term2 cat a3 | wc -l // term3 ... cat a10 | wc -l // term10 And I'd like to calculate term1 + term2 + ... + term10 in a one liner (...
Serge Vu's user avatar
  • 147
0 votes
2 answers
534 views

Rounding numbers of a CSV file, skipping the header

This question is a derivative of the https://askubuntu.com/questions/601149/is-there-a-command-to-round-decimal-numbers-in-txt-files, which was successfully solved by using: perl -i -pe 's/(\d*\.\d*)/...
Charlie's user avatar
  • 93
-3 votes
3 answers
231 views

Bash script - How is 6 > 50? [duplicate]

if [[ 6 > 50 ]]; then echo "true" fi $ bash script.sh I'm missing something very obvious here. Why is 6 greater than 50 ?? ** EDIT ** I'm also try to solve for if [[ 6.5 > 50 ]]; ...
Jacksonkr's user avatar
  • 201
7 votes
4 answers
2k views

Why does the command a-=2 fail?

After executing the command declare -i a=5, the command a+=2 succeeds, but the command a-=2 fails. Can someone explain this strange behavior of bash?
tmpbin's user avatar
  • 753
0 votes
1 answer
398 views

Sum all media durations in current folder

I had a bunch of media (videos to be precise) in a folder and I wanted to sum all the lenghts. I'll be ignoring seconds and hours for now, just to get the gist of it. I wrote this script: sum=0 for ...
Fedja's user avatar
  • 115
1 vote
2 answers
1k views

Sum of large numbers and print the result with all decimal points for the stated question when using awk arrays

I have a below input file which I need to split into multiple files based on the date in 3rd column. Basically all the same dated transactions should be splitted into particular dated file. Post ...
hunter's user avatar
  • 73
2 votes
4 answers
4k views

Sum all positive integers in a bash array

How can I sum all positive integers in a bash array. Here is my code: #!/bin/bash arr=(2 43 -1 -33 24 12 -6) for (( i = 0; i < ${#arr[@]}; ++i )); do if (( arr[i] > 0 )); then sum=`...
Андрей Оганов's user avatar
0 votes
2 answers
1k views

awk column-wise division of all lines by another line

I'm trying to divide all lines in file1.txt by their respective (column-wise) value in the single line in file2.txt. cat file1.txt 1 2.5 3 7 7 7 1 3 5 cat file2....
Arynio's user avatar
  • 3
1 vote
2 answers
433 views

Bash: Arithmetic expansion, parameter expansion, and the comma operator

I have a question about bash's parameter expansion, inside a comma expression, inside an arithmetic expression. I have two statements that I thought should be equivalent, but they're not. Why does ...
Mitchell Spector's user avatar
0 votes
2 answers
4k views

How to resolve Illegal number: -1.00 | ubuntu

I have a scenario where I am taking difference of two numbers. Then the calculated difference needs to be checked within a range. If it lie within that range then display status as success, else ...
techvikky's user avatar
5 votes
1 answer
2k views

Why does arithmetic expansion work in Bash but not in Zsh?

I was demonstrating a piece of funny code (or so I think) to my students in class, and to my surprise, it did not work in zsh. I then switched to bash and it worked as expected. The code is the ...
Jeff's user avatar
  • 169
5 votes
4 answers
2k views

How to get bc to print trailing zeros?

I read topics about how to get bc to print the first zero, but that is not exactly what I want. I want more... I want a function that returns floating point numbers with eight decimal digits. I am ...
Clement S's user avatar
3 votes
2 answers
2k views

Why we can not compare . (dot) as a string value inside the round brackets of if statement?

Problem I have to compare a string "problem1.sh" with itself. It works fine in case of solution1.sh (given below) where I have used square brackets for comparison. However, it does not work in case ...
Md Sabbir Ahmed's user avatar
13 votes
4 answers
5k views

How to increment the value of a (decimal) variable (with leading zero) by +1?

I have a file in the name of Build.number with the content value 012 which I need to increment by +1. So, I tried this BN=$($cat Build.number) BN=$(($BN+1)) echo $BN >Build.number but here I am ...
Naik's user avatar
  • 303

1
2 3 4 5