Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I need an urgent help with a complex situation. I really appreciate your help!
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=60I 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=manageradmin_role_1=calls
admin_role_2=meetings
admin_role_3=gustsmanager_role_1=meetings
manager_role_2=workersage=60
Please let me know how to achieve this. I hope I am clear on my questions. I appreciate your help.
Thank you so much!
Have a great day,
Sam

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

Thank you so much for the help.
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.Thank you so much for your help!
Have a great day!
Sam

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 stringsub(/^\(/,"",value)
Remove ( at start of string in variable valuesub(/\)[:space:]*$/,"",value")
Remove last ) and optionals spaces at end of string in variable value.
Now, value contains elements of array delimited by commasnbv = 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_nelse { 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;
Jean-Pierre.

Hi Jean,
Thank you so much! This really helped me a lot. I appreciate your help.
Thanks again!
Have a great day!
Sam

Hi Jean,
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>Thanks a lot for your help!
Have a great day!
Sam

Hi Jean,
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.parThank you so much!
Have a great day!Regards,
Sam

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |