Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace 'bc' tool in my bash script?

I have the following command in my bash script:

printf '\n"runtime": %s' "$(bc -l <<<"($a - $b)")"

I need to run this script on around 100 servers and I have found that on few of them bc is not installed. I am not admin and cannot install bc on missing servers.

In that case, what alternative can i use to perform the same calculation? Please let me know how the new command should look like

like image 577
all_techie Avatar asked Nov 16 '25 23:11

all_techie


1 Answers

In case you need a solution which works for floating-point arithmetic you can always fall back to Awk.

awk -v a="$a" -v b="$b" 'BEGIN { printf "\n\"runtime\": %s", a-b }' </dev/null

Putting the code in a BEGIN block and redirecting input from /dev/null is a common workaround for when you want to use Awk but don't have a file of lines to loop over, which is what it's really designed to do.

like image 135
tripleee Avatar answered Nov 18 '25 12:11

tripleee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!