Doing math using user defined input and system environment variables

Hi,

I need some help to setup some environmental variables.

for example...Get A -> userdefined/user input
B -> a number.
c -> system variable...for example $GETCONF PAGE_SIZE
E = do some math using bc

display a message "The value is E"

setup the system/kernel paramter sysctl -p <parameter name> = E

setu of system/kernel paramter completed.

What OS are you using?
What shell are you using?
What have you tried so far?

i am trying to calculate oracle 11 rac parameters (shared memory, no of semaphores etc) in rhel6.5. since it depends on the Physical memory and page size
i want to calculate and update the values in /etc/sysctl.conf or using sysctl.conf -w command using a script (since its going to be multiple servers) and

Here are some untested code snippets setting variables as you requested:

printf "Enter userdefined/user input: "
read A
B=42
c=$(getconf PAGE_SIZE)
E=$(echo "$B * $c" | bc)
printf 'A is "%s", B is "%s", c is "%s", E (B * c) is "%s"\n' "$A" "$B" "$c" "$E"

If you using a POSIX conforming shell (such as bash and ksh) and you're doing integer arithmetic, you don't need to call bc ; you can use:

E=$((B * c))

instead.

If you're doing floating point arithmetic and you're using a 1993 or later version of the Korn shell, you can still use the above arithmetic evaluation instead of calling bc , but you'll want to change the format for printing $E to a floating format such as:

printf 'A is "%s", B is "%s", c is "%s", E (B * c) is "%.2f"\n' "$A" "$B" "$c" "$E"

where the number shown in red above specifies the number of digits you want to appear after the radix character in your output.

1 Like