-3

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.

  • 3
    Why do you think you need workarounds? What are you trying, and how is it failing? – Chris Davies Jan 04 '22 at 17:01
  • 2
    cd / gives rbash: cd: restricted as I'd expect, but echo $((44+66)) still correctly returns 110. Also, if it's a script written to use (e.g.) bash then you should still be able to run it from rbash and have it use the shell's full unrestricted power – Chris Davies Jan 04 '22 at 17:02
  • Actually I'm trying to add 2 variables and pipe the output to binary calculator. – satsensort Jan 04 '22 at 17:09
  • I have been trying like # echo $a / 1024 | bc – satsensort Jan 04 '22 at 17:12
  • 4
    Please edit your question to show a relevant (working) snippet or example of code. Don't put it in the comments - it can all too easily get missed – Chris Davies Jan 04 '22 at 17:12
  • 1
    as said, please edit your question to explain more detailledly what the problem is; there's nothing special about restricted shells when it comes to arithmetics – Marcus Müller Jan 04 '22 at 17:13
  • @roaima your solution seems to be working in my case. Thank you very much for your help and quick support. – satsensort Jan 04 '22 at 17:14
  • 1
    or don't delete it, now that you've got answers, but instead accept roaima's answer – Marcus Müller Jan 04 '22 at 17:20
  • ( 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, to echo), 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:48
  • Not that I'm exactly sure why you'd try to do some arithmetic, and then grep 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
  • @ilkkachu Thank you for your solution. I will try and proceed. Thank you very much – satsensort Jan 04 '22 at 17:55

2 Answers2

3

For an integer value of $a (you don't give an example value), the command echo $a / 1024 | bc should work perfectly under rbash. Even outside a script.

If you're using integer arithmetic (which seems to be the case as you haven't told bc otherwise) you may not even need to call out:

echo $(( a/1024 ))

"Works for me".

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
2

It's a normal shell, so the arithmetic operators do work:

value=$(( a / 1024 ))
echo "$value"
Kusalananda
  • 333,661