Get variables from expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get variables from expression
# 1  
Old 05-14-2014
Get variables from expression

Hi,

I have a k-shell master script which reads variables that are globally set and this script is modified by users per their requirements and users define few other variables.

In the script few expressions are evaluated which include both global variables and those added by the user, I would like to parse the expressions to get the list of variables used in the expression and check if they are set or not set before evaluating the expression.

The expressions are similiar to SQL where clause and I would like to know if there is any handy utility that parses the expressions and returns only the variables eliminating all constants, relational operators, arithmetic operators and logical operators ?
# 2  
Old 05-14-2014
Give us something to parse. Some sample input and required output.
# 3  
Old 05-14-2014
I would keep your master script somewhere that they cannot alter it else you will have a multitude of differences as people tweak the code about and a nightmare to debug. Assuming that each one has a separate home directory, could you get your script to use an input file in there?

You haven't given us anything to look at, but I would suggest something like this:-
Code:
#!/bin/ksh
# Any comments you want
# Change history perhaps

# Define standard settings
a=1
b=2
c=3

# Define user settings from user home directory file my_variables
. ~/my_variables

# Define defaults for unset variables
z1="${z1:-Hello}"
z2="${z2:=Here}"

# Define functions

# Main script processing
echo "Running with\n\ta=$a\n\tb=$b\n\tc=$c\n\tmy_var=$my_var\n\tz1=$z1\n\tz2=$z2"

exit

Does that give you a framework to start with?




Robin
# 4  
Old 05-14-2014
Thank you for your replies

Below is a sample Expression

Code:
 
UsrAppNO=(UsrCustGBLNo*2) + UsrCustRegNo + 99

Actually there is a common global code over which regional customizations happen. In the above expression UsrCustGBLNo is set at global level and UsrCustRegNo at regional level and before I evaluate the expression I have to ensure all variables are set

To add to complexity some expressions are passed as arguments to few scripts so I am forced to look at a generic solution by parsing the input expression and get the variables and check if they hold a value.

The expressions contain relational,logical and arithmetic operators and constants

Thanks
Zulfi

Last edited by zulfi123786; 05-14-2014 at 11:06 AM..
# 5  
Old 05-14-2014
Here are some more expressions SmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilie

Are you saying that the people setting up their customised variables will be doing so free-form? I would be concerned that they will not process. What are you writing in and what would you expect to write in?

Would you expect to be able to recognise and process UsrAppNO=(UsrCustGBLNo*2) + UsrCustRegNo + 99 or could you set the rules? In ksh (like my example) this could be written as:-
Code:
((UsrAppNO=$UsrCustGBLNo*2)+$UsrCustRegNo+99))

Setting up a quick test, I got this:-
Code:
# UsrCustGBLNo=5
RBATTE1> UsrCustRegNo=10
RBATTE1> ((UsrAppNO=($UsrCustGBLNo*2)+$UsrCustRegNo+99))
RBATTE1> echo $UsrAppNO
119
RBATTE1>


Does that help?
Robin
# 6  
Old 05-14-2014
Yes, the input expression has to be recognised and processed using expr. We are told that the expressions would be simple combinations of basic operators.

Thanks
Zulfi
# 7  
Old 05-14-2014
Quote:
Originally Posted by zulfi123786
Yes, the input expression has to be recognised and processed using expr. We are told that the expressions would be simple combinations of basic operators.

Thanks
Zulfi
Homework?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

SHELL: UNIX : Ls regular expression not working when used with variables

If i do below command in unix prompt which static values (ie 27..97), it is working fine and gives desired output >ls -d $WORKDIR/batch/somefilename_{27..97}.* 2>/dev/null somefilename_27.sometxt somefilename_28.sometxt somefilename_29.sometxt .. somefilename_97.sometxt But if i want... (2 Replies)
Discussion started by: haiderali
2 Replies

2. Shell Programming and Scripting

Why Relational Expression is Writing to a Expression?

Hello All, Not sure why this is happening... When the following If Statement is evaluated for some reason it is creating a file in the CWD called '0'. I've seen this happen before, just not in an If Statement... CODE: if then DIR_NAME="$1" DIR_SIZE=0 STATUS="" else... (3 Replies)
Discussion started by: mrm5102
3 Replies

3. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

4. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

5. Shell Programming and Scripting

awk variables in regex expression ?

Hello, Could someone explain why this one returns nothing: $ x=/jon/ $ echo jon | awk -v xa=$x '$1~xa {print}' $ while the following works fine: $ x=jon $ echo jon | awk -v xa=$x '$1==xa {print}' $ jon and the following works fine: $ echo jon | awk '$1~/jon/ {print}' $ jon ... (3 Replies)
Discussion started by: vilius
3 Replies

6. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

7. Shell Programming and Scripting

Assigning expression value to tcsh variables

Hi All, I have a tcsh script as: #!/usr/bin/csh -x set packsName=$(awk -F'' '/^execute.*=true/{print $2}' ExecutePacks.config) for var in $packsName do echo "printed $var" done I want to assign the value which is returned by awk function to the variable called packsName. How do I... (2 Replies)
Discussion started by: AB10
2 Replies

8. Shell Programming and Scripting

Need help with arithmetic expression using variables

Hi, I am trying to do a percentage calculation in a Bourne shell script. The following works fine, but when I try to subsitute the values 153824 and 246000 for variables (e.g. used and limit), I get errors. calc() { awk 'BEGIN {OFMT="%f"; print '"$*"'; exit}' } calc '(153824 /... (3 Replies)
Discussion started by: shorehil
3 Replies

9. Programming

error: initializer expression list treated as compound expression

I had seen this error for the first time ..... error: initializer expression list treated as compound expression please help.... (12 Replies)
Discussion started by: arunchaudhary19
12 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question