"Simple" arithmetic - bash

Why can't I do this simple arithmetic operation and store it in a variable in bash shell? I've been struggling with this and playing around with () and $ symbols but no luck.
read t
let r=$(5/9)*$($t-32)
I get a: let: r=*: syntax error: operand expected (error token is "*")

When you are using the let statement, you don't need the dollar-sign, but single-quote the expression instead to keep the shell preprocessor from messing with your operators. Note that bash does not seem to be able to handle numbers which aren't integers, so the (5/9) expression will always be zero. Try the second let statement.
read -p 'Temp in Fahrenheit (no decimals): ' t
# let r='(5/9)*(t-32)' -- this doesn't work
let r='5*(t-32)/9'
echo "Centigrade: $r"

Try that :
read -p 'Type integer temp (Fahrenheit) >>> ' int
echo "$(( 5 * ( int - 32 ) / 9 )) Celcius"

Related

Updating (sequentially adding a value into) a variable within a for loop, in bash script

I am trying to update a variable within a for loop, sequentially adding to the variable, and then printing it to a series of file names.
Actually, I want to sequentially update a series of file, from a tmp file distTmp.RST to sequentially dist1.RST, dist2.RST, etc..
The original distTmp.RST contains a line called "WWW". I want to replace the string with values called 21.5 in dist1.RST, 22.5 in dist2.RST, 23.5 in dist3.RST, etc...
My code is as follows:
#!/bin/bash
wm=1
wM=70
wS=1
F=20.5
for W in {${wm}..${wM}..${wS}}; do
F=$(${F} + 1 | bc)
echo ${F}
sed "s/WWW/"${F}"/g" distTmp.RST > dist${W}.RST
done
echo ${F}
========
But I am getting error message as follows:
change.sh: line 13: 20.5 + 1 | bc: syntax error: invalid arithmetic operator (error token is ".5 + 1 | bc")
Kindly suggest me a solution to the same.
Kindly suggest me a solution to the same.
This might do what you wanted. Using a c-style for loop.
#!/usr/bin/env bash
wm=1
wM=70
wS=1
F=20.5
for ((w=wm;w<=wM;w+=wS)); do
f=$(bc <<< "$F + $w")
echo "$f"
sed "s/WWW/$f/g" distTmp.RST > "dist${w}.RST"
done
The error from your script might be because the order of expansion. brace expansion expansion happens before Variable does.
See Shell Expansion
Use
F=$(echo ${F} + 1 | bc)
instead of F=$((${F} + 1 | bc)). The doubled-up parentheses are what caused your error. Double parentheses weren't in the original code, but I get a different error saying 20.5: command not found with the original code, so I tried doubling the parentheses and get the error in the question. Apparently, floating point numbers aren't supported by $(()) arithmetic evaluation expressions in Bash.

How to get the pass percentage using shell script

I have the below script which is trying to get the pass percentage through shell script.
Script:
n= "$pass"/"$total";
"$n" * = 100;
echo "$n"
Output:
/tmp/jenkins3601870177535319162.sh: line 45: 20/25*=: No such file or
directory 20/25
I'm not sure the above calculation is correct. But I just have variable $pass which is having pass test case count and $total variable which had total test case count. Just wanna get percentage of the passed test cases using shell.
You have two primary problems: (1) there cannot be any spaces on either side of the '=' sign; and (2) shell uses integer math so 20/25 will equal 0.
The POSIX arithmetic operator is ((...)) where your expression goes within ((...)). Also, within ((...)) there is no need to derefernce the variable name by preceding it with a '$'.
To assign the result of the expression to a variable you precede the operator by the '$', e.g. n=$((pass/total)).
To get around the fact that shell uses integer math, you can multiply pass by 100 before you divide and at least get a whole-number percentage. For example:
#!/bin/sh
pass=20
total=25
n=$(((pass * 100)/total))
printf "pass percentage: %d\n" "$n"
If you run the script you will then get:
$ sh percent.sh
pass percentage: 80
Where 80% is what would be the percent result for 20/25. Look things over and let me know if you have further questions.

Subtract 2 array values having floating point decimals

I'm reading a file with multiple columns, and dumping 2 columns of the file into into 2 different arrays. Now based on a condition, I need to get the difference between 2 values retrieved from the array. So my code looks like this -
if [ condition ]; then
VAL = (( ${local[$x]} - ${local[$y]} ))
fi
The thing is, while I'm able to echo and see both values ${local[$x]} and ${local[$y]}, the subtraction operation gives me a syntax error. I understand it's failing because the values currently held within the array involve floating point decimal values - like 3456712.126758, and the assignment throws errors with the decimal part. I understand arithmetic operations are not a strong point with the bash shell as floating point numbers are considered strings hence the issue.
Could you please help getting the right format please?
Should I do something like this
VAL= awk '{ print ${local[$x]} - ${local[$y]} }'
or
VAL=echo ${local[$x]} - ${local[$y]} | bc -l
I'm sure the syntax above is wrong, kindly help with the syntax, I need it assigned the subtracted result assigned to the field VAL.
Not only floating points, but also the spacing would lead to syntax errors. Bash variable assignments must have no spaces, as in val=x, and not val = x.
Uppercase variable names are reserved for environment variables, and it is recommended to use lowercase instead for your own variables. (Oh, and local is also a reserved word.)
Your assignment wouldn't work with proper spacing, either: the arithmetic expression
var=(( ${vals[$x]} - ${vals[$y]} )) # syntax error near unexpected token `('
is just evaluating its contents, but not returning anything. You could use the part after the = as a condition. To make it return something, you need arithmetic expansion (note the extra $):
var=$(( ${vals[$x]} - ${vals[$y]} )) # works for integers
^
In an arithmetic context, you don't even need to prepend $ to your variables:
var=$(( vals[x] - vals[y] ))
works just as well. Exception: in associative arrays, you still have to do it for indices:
$(( vals[$x] ))
And finally, as you noticed, this all doesn't work for floating point numbers. Instead of piping to bc, you can also use a here string and avoid spawning a subshell:
$ vals=(1.1 2.2)
$ x=0
$ y=1
$ echo $(( local[x] - local[y] )) # No '$' needed for variable expansion
bash: 1.1: syntax error: invalid arithmetic operator (error token is ".1") # But :(
$ bc -l <<< "local[x] - local[y]" # Requires '$' - these expand to nothing
0
$ bc -l <<< "${local[x]} - ${local[y]}" # Works!
-1.1
With awk:
awk -v a=${a} -v b=${b} 'BEGIN{print a - b}'
With bc:
echo "${a} - ${b}" | bc -l
See also other options here.

atoi() like function in bash?

Imagine that I use a state file to store a number, I read the number like this:
COUNT=$(< /tmp/state_file)
But since the file could be disrupted, $COUNT may not contain a "number", but any characters.
Other than using regex, i.e if [[ $COUNT ~ ^[0-9]+$ ]]; then blabla; fi, is there a "atoi" function that convert it to a number(0 if invalid)?
EDIT
Finally I decided to use something like this:
let a=$(($a+0))
Or
declare -i a; a="abcd123"; echo $a # got 0
Thanks to J20 for the hint.
You don't need an atoi equivalent, Bash variables are untyped. Trying to use variables set to random characters in arithmetic will just silently ignore them. eg
foo1=1
foo2=bar
let foo3=foo1+foo2
echo $foo3
Gives the result 1.
See this reference
echo $COUNT | bc should be able to cast a number, prone to error as per jurgemaister's comments...
echo ${COUNT/[a-Z]*} | bc which is similar to your regex method but not prone to error.
case "$c" in
[0-9])...
You should eat the input string charwise.

Can a function be used in $(( func arg ? str1 : str2 ))?

I'd like to use a statement like this:
var=$(( func arg ? str1 : str2 ))
but bash gives this syntax error message:
syntax error in expression (error token is "arg")
I've played with various forms but I can't figure out how to make it accept a function with argument. Any ideas?
echo $(( $(seq 1) + 1 ))
2
You need to use the same syntax as bash expects elsewhere. As far as the conditional ? iftrue : iffalse syntax, I don't think you can do that in bash. Instead, you can do something like:
echo $(( 1 + $(true && echo 1 || echo 0) ))
2
I think the correct answer is that there is no way to use the statement I asked about. The problem is that this conditional operator can only evaluate to an integer and not a string as I wanted to do.
jm666 answered the question in a comment to Steve's answer so I gave him an up vote.

Resources