Sponsored Content
Top Forums Shell Programming and Scripting Doing math using user defined input and system environment variables Post 302886490 by Don Cragun on Friday 31st of January 2014 11:46:16 PM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. Linux

environment variable is not defined

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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
symdiff(3tcl)						 Symbolic differentiation for Tcl					     symdiff(3tcl)

__________________________________________________________________________________________________________________________________________________

NAME
symdiff - Symbolic differentiation for Tcl SYNOPSIS
package require Tcl 8.5 package require grammar::aycock 1.0 package require math::calculus::symdiff 1.0 math::calculus::symdiff::symdiff expression variable math::calculus::jacobian variableDict _________________________________________________________________ DESCRIPTION
The math::calculus::symdiff package provides a symbolic differentiation facility for Tcl math expressions. It is useful for providing de- rivatives to packages that either require the Jacobian of a set of functions or else are more efficient or stable when the Jacobian is pro- vided. PROCEDURES
The math::calculus::symdiff package exports the two procedures: math::calculus::symdiff::symdiff expression variable Differentiates the given expression with respect to the specified variable. (See Expressions below for a discussion of the subset of Tcl math expressions that are acceptable to math::calculus::symdiff.) The result is a Tcl expression that evaluates the derivative. Returns an error if expression is not a well-formed expression or is not differentiable. math::calculus::jacobian variableDict Computes the Jacobian of a system of equations. The system is given by the dictionary variableDict, whose keys are the names of variables in the system, and whose values are Tcl expressions giving the values of those variables. (See Expressions below for a discussion of the subset of Tcl math expressions that are acceptable to math::calculus::symdiff. The result is a list of lists: the i'th element of the j'th sublist is the partial derivative of the i'th variable with respect to the j'th variable. Returns an error if any of the expressions cannot be differentiated, or if variableDict is not a well-formed dictionary. EXPRESSIONS
The math::calculus::symdiff package accepts only a small subset of the expressions that are acceptable to Tcl commands such as expr or if. Specifically, the only constructs accepted are: o Floating-point constants such as 5 or 3.14159e+00. o References to Tcl variable using $-substitution. The variable names must consist of alphanumerics and underscores: the ${...} nota- tion is not accepted. o Parentheses. o The +, -, *, /. and ** operators. o Calls to the functions acos, asin, atan, atan2, cos, cosh, exp, hypot, log, log10, pow, sin, sinh. sqrt, tan, and tanh. Command substitution, backslash substitution, and argument expansion are not accepted. EXAMPLES
math::calculus::symdiff::symdiff {($a*$x+$b)*($c*$x+$d)} x ==> (($c * (($a * $x) + $b)) + ($a * (($c * $x) + $d))) math::calculus::symdiff::jacobian {x {$a * $x + $b * $y} y {$c * $x + $d * $y}} ==> {{$a} {$b}} {{$c} {$d}} BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category math :: calculus of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. SEE ALSO
math::calculus, math::interpolate COPYRIGHT
Copyright (c) 2010 by Kevin B. Kenny <kennykb@acm.org> Redistribution permitted under the terms of the Open Publication License <http://www.opencontent.org/openpub/> math 1.0 symdiff(3tcl)
All times are GMT -4. The time now is 08:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy