I need to echo multiplication with decimals in bash [duplicate] - bash

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.

Related

bash is saying a range is an invalid variable name [duplicate]

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Variables in bash seq replacement ({1..10}) [duplicate]
(7 answers)
Closed 5 months ago.
I have asked way too many dumb questions here, but here we go.
Code:
#!/bin/bash
for i in {0..$#}; do
ascii ${!i} | grep "\`" | tail -c 3 | head -c 1;
done
echo
(This is to translate long binary input, like from those horror games that have binary at the end)
Error message:
./asciit: line 4: {0..2}: invalid variable name
Command run:
./asciit 01101000 01101001
Why?

Taking Floating Point Number as String and Performing Aritmetic in Bash [duplicate]

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")

How to sum floats variables in bash? [duplicate]

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.

'4.5: syntax error: invalid arithmetic operator (error token is ".5")' - but the code still seems to work. Why? [duplicate]

This question already has answers here:
How can I compare two floating point numbers in Bash?
(22 answers)
Closed 3 years ago.
My calculator generates the error mentioned in the title when I try to use the operator "/" as well as the numbers 4.5 and 2.
This is (just like the error states) most likely due to what's after the decimal point in 4.5, but I don't know how I could fix this and why the script actually manages to give me the correct result afterwards.
Code:
#!/bin/bash
read -p "Operator: " operator
read -p "First number: " ch1
read -p "Second number: " ch2
case $operator in
"+") echo "scale=2; $ch1+$ch2" | bc -l;;
"/") if [[ $ch1 -eq 0 || $ch2 -eq 0 ]]
then
echo "Nope..."
else
echo "scale=2; $ch1/$ch2" | bc -l
fi
;;
esac
Full output:
./script2.sh: line 9: [[: 4.5: syntax error: invalid arithmetic operator (error token is ".5")
2.25
Despite producing floating point results, Bash does not support other type of arguments than integers, so you need to rather invoke external tools like bc for your math or stick to integers only.
See the Bash documentation, section "6.5 Shell Arithmetic":
The shell allows arithmetic expressions to be evaluated, as one of the shell expansions or by using the (( compound command, the let builtin, or the -i option to the declare builtin.
Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error

Bash tr: extra operand `\n'\\n\'' [duplicate]

This question already has answers here:
Why should eval be avoided in Bash, and what should I use instead?
(3 answers)
Variables as commands in Bash scripts
(5 answers)
Closed 5 years ago.
Command="tr ' ' '\n' < /location/to/file.xml | sed -e '/\/Name/, $d' | grep Apple | wc -l"
New_command=`$Command`
The error I am getting:
tr: extra operand \'\\n\'' Trytr --help' for more information.
What is wrong with what I am doing? Trying to assign a string variable to 'New_Command' so I can execute it and get the output.

Resources