Doing math using user defined input and system environment variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Doing math using user defined input and system environment variables
# 1  
Old 01-31-2014
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.
# 2  
Old 01-31-2014
What OS are you using?
What shell are you using?
What have you tried so far?
# 3  
Old 01-31-2014
O.S is rhel6.5, bash shell, oracle rac 11

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
# 4  
Old 02-01-2014
Quote:
Originally Posted by saravanapandi
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.
Here are some untested code snippets setting variables as you requested:
Code:
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:
Code:
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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX $USER and $LOGNAME environment variables

I have some comments about a previously closed topic whose name is the same as above Omitted from the discussion was the situation with a "sudo command or sudo within a script". There is an inconsistency between systems. On some systems $LOGNAME is invariant, on others, on RedHat sudo... (3 Replies)
Discussion started by: lsatenstein
3 Replies

2. UNIX for Beginners Questions & Answers

How are environment variables defined in a Gnome terminal session?

Hello... and thanks in advance for any help anyone can offer me with my question! I'm hoping someone will have a little patience with me and walk me through this! I'm trying to understand a user login process on Centos 7 and I've gotten a bit confused trying to figure out when/how a Gnome... (4 Replies)
Discussion started by: bodisha
4 Replies

3. Shell Programming and Scripting

Run script through cron with user environment variables

Hi everyone, I wrote a script that is supposed to be run by cron on a daily basis. It works just fine if I run it manually, but due to a lack of environment variables (which are available during my user session but not when cron runs the script) it keeps failing to run successfully. Here's the... (2 Replies)
Discussion started by: gacanepa
2 Replies

4. Shell Programming and Scripting

How to automatically create variables from user input in ksh?

I need some help to write a ksh script. My code so far (pretty bad, sorry): #! /bin/ksh echo "Calculate average" UserDecision=y while test $UserDecision = y do echo "Enter a number: " read Number1 echo "Enter a number: " read Number2 echo "Do you want to enter another number?... (2 Replies)
Discussion started by: johnagar
2 Replies

5. Shell Programming and Scripting

Unix $USER and $LOGNAME environment variables

Hi, I am wondering what is the difference between the USER and LOGNAME environment variables, and under what situations would they be different? I am using Ubuntu 8.04 32-bit and I do not have 'login' command to test it. (7 Replies)
Discussion started by: royalibrahim
7 Replies

6. Shell Programming and Scripting

awk printf for user defined variables

I am working on a SunFire 480 - uname -a gives: SunOS bsmdb02 5.9 Generic_112233-08 sun4u sparc SUNW,Sun-Fire-480R I am tyring to sum up the total size of all the directories for each running database using awk: #!/usr/bin/ksh for Database in `ps -efl | grep "ora_pmon" | grep -v grep |... (1 Reply)
Discussion started by: jabberwocky
1 Replies

7. Shell Programming and Scripting

Force Input in User Defined Variable

In a line such as: echo -n "How many days back would you like to check? "; read days How can I ensure that the user has a.) entered a number between 1-30 (not 0 or 31+) and b.) has not just hit enter (ie set it to "") and if it's entered wrong, how do I start the if statement over? I... (10 Replies)
Discussion started by: earnstaf
10 Replies

8. Programming

Math with user variables

Hi everybody: I have a problem about use variables. I 've created this variable: var=`wc -l file.txt | cut -c 1-2`; n_var="$var"-1 ; echo $n_var; In my case var is 8. When echo shows $n_var does not appear as I want. The question is how can I subtract, this is operate, to my variable.... (1 Reply)
Discussion started by: tonet
1 Replies

9. Linux

environment variable is not defined

moved to correct thread (0 Replies)
Discussion started by: alien12
0 Replies

10. UNIX for Advanced & Expert Users

Expanding Variables in User Input

If have var='$variable' how can I expand $variable. I have tried many thing like duble quotes/braces etc, but nothing worked. I need the solution ASAP. (2 Replies)
Discussion started by: Bsk
2 Replies
Login or Register to Ask a Question