Skip to main content

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
12 votes
2 answers
1k views

I was debugging one script, and discovered unexpected behavour of handling arithmetic syntax error. Normally, when error happens, script is just continuing execution. #!/bin/bash func2 () { echo &...
Ashark's user avatar
  • 1,209
4 votes
1 answer
297 views

I am using the bc calculator with obase and ibase to convert integer representation. I'm not used to it, because very basic things can be done with $(( expresion )) in bash, and most things that can't ...
Gyro Gearloose's user avatar
2 votes
1 answer
483 views

Bash accepts both these syntax: FOO=$(($BAR + 42)) and FOO=$((BAR + 42)) Which one is correct / most portable / less prone to errors? Or are both equally valid?
dr_'s user avatar
  • 32.4k
-3 votes
1 answer
195 views

Hypothetic example: There is a number that I will split into x parts (smaller numbers, in a range you see in the example). I need a way, ideally a one liner for the Linux Bash, to split a large number ...
user447274's user avatar
1 vote
5 answers
271 views

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
  • 554
-1 votes
2 answers
158 views

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
61 views

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
131 views

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
587 views

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
339 views

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
349 views

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
2k views

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
9 votes
1 answer
11k views

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
153 views

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 ...
camarman's user avatar
  • 153
0 votes
2 answers
103 views

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
533 views

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
  • 145
4 votes
4 answers
2k views

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
2k views

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
926 views

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
181 views

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

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
296 views

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
312 views

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
575 views

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

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
4k views

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
634 views

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
  • 515
3 votes
2 answers
1k views

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
361 views

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
3k views

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,954
11 votes
3 answers
3k views

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
218 views

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
71 views

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
66 views

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
166 views

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
57 views

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
374 views

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
  • 844
0 votes
3 answers
3k views

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
17 views

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
2k views

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
  • 446
1 vote
3 answers
814 views

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
632 views

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
309 views

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

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
  • 793
0 votes
1 answer
490 views

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
  • 125
1 vote
2 answers
2k views

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
  • 83
2 votes
4 answers
5k views

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

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.txt ...
Arynio's user avatar
  • 3
1 vote
2 answers
617 views

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
5k views

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

1
2 3 4 5