Computing.Net > Forums > Unix > Parameter file for a shell script

Parameter file for a shell script

Reply to Message Icon

Original Message
Name: oracledba
Date: March 1, 2004 at 22:45:28 Pacific
Subject: Parameter file for a shell script
OS: HP-UX
CPU/Ram: 8g ram
Comment:

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=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. I hope I am clear on my questions. I appreciate your help.

Thank you so much!

Have a great day,
Sam


Report Offensive Message For Removal

Response Number 1
Name: aigles
Date: March 1, 2004 at 23:07:20 Pacific
Subject: Parameter file for a shell script
Reply: (edit)

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.



Report Offensive Follow Up For Removal

Response Number 2
Name: oracledba
Date: March 2, 2004 at 10:29:17 Pacific
Subject: Parameter file for a shell script
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 3
Name: aigles
Date: March 2, 2004 at 12:23:24 Pacific
Subject: Parameter file for a shell script
Reply: (edit)

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;


Jean-Pierre.


Report Offensive Follow Up For Removal

Response Number 4
Name: oracledba
Date: March 2, 2004 at 13:00:13 Pacific
Subject: Parameter file for a shell script
Reply: (edit)

Hi Jean,

Thank you so much! This really helped me a lot. I appreciate your help.

Thanks again!

Have a great day!
Sam


Report Offensive Follow Up For Removal

Response Number 5
Name: oracledba
Date: March 2, 2004 at 13:20:11 Pacific
Subject: Parameter file for a shell script
Reply: (edit)

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


Report Offensive Follow Up For Removal


Response Number 6
Name: oracledba
Date: March 2, 2004 at 21:40:17 Pacific
Subject: Parameter file for a shell script
Reply: (edit)

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

Thank you so much!
Have a great day!

Regards,
Sam


Report Offensive Follow Up For Removal

Response Number 7
Name: aigles
Date: March 3, 2004 at 03:00:03 Pacific
Subject: Parameter file for a shell script
Reply: (edit)

Replace the statement :

NF >= 2 {

by :

NF >= 2 && $0 !~ /^[:space:]*#/ {


Jean-Pierre.


Report Offensive Follow Up For Removal

Response Number 8
Name: oracledba
Date: March 4, 2004 at 20:50:05 Pacific
Subject: Parameter file for a shell script
Reply: (edit)

Hi Jean,

Great! Thank you so much for your help!

Have a great day!
Sam


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Parameter file for a shell script

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software