I have a parameter file like this.
unix> cat script.par
name=david
city=portland
job=(admin,manager)
admin_role=(calls,meetings,gusts)
manager_role=(meetings,workers)
age=60
I have to use the above parameters in a shell script. Since the number of parameters are high I can't pass them as $1.. $10 etc in the command prompt. Number of oarameters in script.par can vary.
In the shell script I need to read the values in script.par and assign them to different variables as shown below!
name=david
city=portland
job_1=admin
job_2=manager
admin_role_1=calls
admin_role_2=meetings
admin_role_3=gusts
manager_role_1=meetings
manager_role_2=workers
age=60
Please let me know how to achieve this.
>>>>>
Try this :
ReadParams()?{
eval?$(?awk?-F?"="?'
NF?>=?2?{
???variable?=?$1;
???value????=?$2;
???if?(value?~?/^\(.*\)[:space:]*$/)?{
??????sub(/^\(/,?"",?value);
??????sub(/\)[:space:]*$/,?"",?value);
??????nbv?=?split(value,?values,?",")
??????for?(i=1;?i<=nbv;?i++)
??print?variable?"_"?i?"="?values[i];
???}?else?{
??????print?variable?"="?value;
???}
}
'?$1?)????
}
ReadParams?script.par
>>>>>
I would like to know what is the purpose of ~, :space: in the awk command.
I really appreciate if you could give me brief description of each line
with in the awk command. I am trying to understand what it is doing.
Since values can vary in script.par file, when I am using the variables
I want to know what's the max values supplied for that variable. in other
words there will be 3 admin roles (admin_role_1=calls, admin_role_2=meetings,
admin_role_3=gusts) and two manager roles (manager_role_1=meetings, manager_role_2=workers),
so number of
values are changing. so, Would you please let me know how to know what's
the max values supplied for a given variable
so that I will refer to only to those variables in shell script.
>>>>>
Some explanations about the script
-F "="
Set the internal awk variable FS to "=".,this specify the field separator
value.
NF >= 2 { . . . . }
Select lines with two or more fields (in the form variable=value) and
specify the code to execute for those lines.
variable = $1 ; value = $2
Get the variable name an it's value (fields delimited by FS="=")
if (value ~ /^\(.*\)[:space:]*$/) { ... }
Test if the value is an array definition
value ~ /.../ : value match the regular expression
^ : start of string
\(.*\) : characters between parenthesis
[:space:]* : spaces (space or tab)
$ : end of string
sub(/^\(/,"",value)
Remove ( at start of string in variable value
sub(/\)[:space:]*$/,"",value")
Remove last ) and optionals spaces at end of string in variable value.
Now, value contains elements of array delimited by commas
nbv = split(value, values, ",")
The elements of array are stored in the array values (elements delimited
by "," in value).
The number of elements is stored in the variable nbv.
for (i=1; i<=nbv; i++) print variable "_" i "=" values[i];
For each element of the array a variable assignment is created in the
form :
variable_n=value_n
else { print variable "=" value }
The else state for 'not an array definition'.
The variable assignment is printed in the form :
variable=value
(same as input without optional ending spaces).
The global result of the awk script is a list of variable assignments that are evaluated by the 'eval' command to define script variables.
You need to determine how many values are supplied for a variable (array
definition). I propose you to define an extra variable which contains this
count
(for example admin_role_count=3).
Add the following statement in the awk script :
for (i=1; i<=nbv; i++)
???print variable "_" i "=" values[i];
print variable "_count=" nbv;
>>>>>
I am sorry to post another question. some parameters in script.par
file can be commented out. Would you please let me know how to handle that.
An example situation: I commented out "city" in script.par and this parameter
should not be used in shell script. Would you please let me know how to
handle that.
DEV UNIX 27> cat script.par
name=david
#city=phoenix
job=(admin,manager)
admin_role=(calls,meetings,gusts)
manager_role=(meetings,workers)
age=60
DEV UNIX 28>
>>>>>
I modified the script like this to handle the commented parameters.
It is working as expected. When you get a chance please verify this. Please
look at my earlier post for the challenge I am facing.
Modified script. Please let me know if I am doing it right -
ReadParams() {
eval $( awk -F "=" '
NF >= 2 {
variable = $1;
value = $2;
if (variable !~ /^#/)
{
if (value ~ /^\(.*\)[:space:]*$/) {
sub(/^\(/, "", value);
sub(/\)[:space:]*$/, "", value);
nbv = split(value, values, ",")
for (i=1; i<=nbv; i++)
print variable "_" i "=" values[i];
print variable "_max" "=" nbv;
} else {
print variable "=" value;
}
}
}
' $1 )
}
ReadParams script2.par
>>>>>
Replace the statement :
NF >= 2 {
by :
NF >= 2 && $0 !~ /^[:space:]*#/ {
Quick Links:
Do you have
a UNIX Question?
Unix Home: Unix System Administration
Hints and Tips