I have a shell script to calculate cpu and memory resources in a vm. For one task, I would like to perform mathematical calculations in a rbash shell. Could you please guide me the possible workarounds.
I have been trying like
echo $a / 1024 | bc
I'm trying the below substitution and getting the below error. Can you please advise me
Both the variables have decimal values as well. Like 0.4, 10.0 and would need to subtract and get the values
for used_cores in `echo (( $config_cpu - $free_cpu )) | grep [0-9] || echo NA`
do
echo $used_cores
done
Error:
bash: command substitution: line 12: syntax error near unexpected token `('
The question seems to be different from the reference question. Can you please consider this question.
cd /
givesrbash: cd: restricted
as I'd expect, butecho $((44+66))
still correctly returns110
. Also, if it's a script written to use (e.g.)bash
then you should still be able to run it fromrbash
and have it use the shell's full unrestricted power – Chris Davies Jan 04 '22 at 17:02(
is a special character to the shell: if you want to print it (or rather, pass it as an argument to some command as-is, here, toecho
), you need to quote it. But if you want to do arithmetic, the syntax is$(( 1 + 2 + 3 ))
etc., see https://www.gnu.org/software/bash/manual/html_node/Arithmetic-Expansion.html and https://mywiki.wooledge.org/BashGuide/CompoundCommands#Arithmetic_Evaluation – ilkkachu Jan 04 '22 at 17:48grep
for a digit from the result. Any arithmetic result would contain at least one, and errors can be detected otherwise. – ilkkachu Jan 04 '22 at 17:49