This question already has answers here:
"Invalid Arithmetic Operator" when doing floating-point math in bash
(5 answers)
How do I use floating-point arithmetic in bash?
(23 answers)
How can I compare two floating point numbers in Bash?
(22 answers)
Closed 3 years ago.
I am trying to run a script in which a variable is updated every time by a float number "given as user input" in a while loop. I know that bash doesn't support floats, I tried using | bc but it doesn't seem to work to me...
step_l0=0.25
step_l=0
while [ $step_l -le 2 ] do
step_r=$radius_input
while [ $step_r -le $radius_define ] do
stuff done in the while loops
step_r=$(( $step_r + $step_r0 ))
done
step_l=$(( $step_l + $step_l0 ))
done
Per chepner's suggestion -
$: a=0.25
$: b=1.753
$: awk "BEGIN{ print $a + $b }"
2.003
$: python -c "print $a + $b"
2.003
$: perl -e "print $a + $b"$'\n'
2.003
Where possible (and it's virtually always possible), if you need to invoke a more precise or powerful language to accomplish a task in your script, consider converting the entire script. You'll be glad you did.
Related
This question already has answers here:
Floating-point arithmetic in UNIX shell script
(4 answers)
How do I use floating-point arithmetic in bash?
(23 answers)
Division in script and floating-point
(6 answers)
Closed 2 years ago.
I have the following Bash code, which runs a C++ binary (./code) and stores the result:
#!/bin/bash
output=$(./code /);
read -ra arr -d '' <<<"$output"
value=${arr[-1]}
sum=$value+1.034
echo $sum
I want it to be able to take the value of the variable sum, which is a number less than zero, ie 0.01357 and be able to add another floating point number to it, before outputting the result to the screen.
So the result should be 1.04757, but the output I am currently getting is:
0.01357+1.034
Bash doesn't support floating point arithmetics. You need another program doing the math for you.
Here are three examples using bc, awk or GNU datamash:
#!/bin/bash
read -ra arr -d '' <<<"$(./code /)"
# bc
printf '%s + %s\n' "${arr[-1]}" "1.034" | bc
# or awk
#awk -v val="${arr[-1]}" 'BEGIN{print val + 1.034}'
# or datamash
#datamash sum 1 <<<$(printf '%s\n' "${arr[-1]}" "1.034")
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Compare output rather than command
(3 answers)
Closed 3 years ago.
piped eval in bash for string length comparison
i am trying to check if a certain device with a given id is plugged in and trigger a action based on that
i tried eval / exec
here is what i have so far
#!/bin/bash
KBP='[["lsusb -d 1c11:b04d | wc -c" == "0"]]'
if eval $KBP; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
expected result:
if device is plugged in and string is not 0 it would hop in the false condition
actual result - cant evaluate the piped condition
Guessing fixed expression would look like this:
if [ "$(lsusb -d 1c11:b04d | wc -c)" -eq 0 ]; then
To remember:
Bash is spaces aware. [[ and ]] needs after and behind (well, ; is special here, it separates commands).
To get output of a command use command substitution $( ... )
There is no need for eval here.
This question already has answers here:
What is "-le" in shell script?
(2 answers)
Closed 4 years ago.
I am using a script made by one of my former colleagues, he told me I'll need some working with it. I am wondering what this while loop does:
# This is the loop that does the simulation
lastsim=0
nextsim=`/usr/bin/expr $lastsim + 1`
while [ $nextsim -le $upperlimit ]
do
cp -i Dynamics_${lastsim}_500ps/*.prmtop ./$paramInput.prmtop
specifically I'm confused by thie -le syntax
This is only part of the script I can upload the rest if necessary.
-le means less or equal to. See the following example which would print 0-9:
i=0
while [ $i -le 9 ]; do
echo $i
let i++
done
This question already has answers here:
Floating-point arithmetic in UNIX shell script
(4 answers)
floating-point operations with bash
(3 answers)
"Invalid Arithmetic Operator" when doing floating-point math in bash
(5 answers)
Closed 5 years ago.
I am trying to multiply decimals and echo them
Here is what I have so far ...
#!/bin/bash
gbspace=1
limitUsers=2
limitInstances=2
echo $(($gbspace*0.5)) GB Webspace
echo limitUsers:$(($limitUsers*5))
echo limitUsers:$(($limitUsers*5)), limitInstances:$(($limitInstances)) \| Hi
and this is what I get ...
root#home /home/work # bash run
run: line 7: 1*0.5: syntax error: invalid arithmetic operator (error token is ".5")
limitUsers:10
limitUsers:10, limitInstances:2 | Hi
Use bc which is pre-installed on most systems, with the -l flag to enable floating-point arithmetic:
echo $(echo "$gbspace*0.5" | bc -l) "GB Webspace"
Note that you have to be careful with the quoting, and you have to pipe the expression you want to compute to bc with the echo command.
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 5 years ago.
im new to bash scripting, appreciate if you can help.
Im trying to write a script to compare the lines in file with integer argument.
Here is what i've got so far but i make some mistakes and get error.
#!/bin/bash
a="$1"
b="wc -l < /filepath/filename.txt"
if (( $a < $b )); then
echo "file has more lines than integer"
else
echo "file has less lines than integer"
fi
Appreciate if you can point to where i make mistake.
b="wc -l < /filepath/filename.txt"
should instead be:
b=$(wc -l < /filepath/filename.txt)
...if you want to run that command and store its output in the variable.