Operating System - HP-UX
1752808 Members
6312 Online
108789 Solutions
New Discussion юеВ

Awk help to process a resource file by a shell script

 
SOLVED
Go to solution
Rui Vilao
Regular Advisor

Awk help to process a resource file by a shell script

Greetings to all awk and sed gurus!

I have a general resource file of the following format that must be read and sourced by several shell scripts:


Loading.NRT_DAYS=1
Loading.POLLING_INTERVAL=5
#Loading.POLLING_INTERVAL=30
Loading.MASK=*

Running.NRT_DAYS=1
Running.POLLING_INTERVAL=5
#Running.POLLING_INTERVAL=30

тАж

Where:
Line beginning with #, are comments
Each entry has the format: Script_name.variable_name=value

I want to write within my shell script an awk command to read and parse this file. For each line matching the script name, it will:
export variable_name=value
тАж

I am quiet proficient in shell scripting and tha kind of parsing is complex because * can be a valueтАж
I believe the parsing can be achieved by a simple awk commandтАж


Any help/suggestion is highly appreciated.

Thanks in advance for your help,

Kind Regards,

Rui
"We should never stop learning"_________ rui.vilao@rocketmail.com
8 REPLIES 8
Rodney Hills
Honored Contributor
Solution

Re: Awk help to process a resource file by a shell script

Try this-

awk -F= '!/#/{print "export " $1 "=''" $2 "''" }' /tmp/export$$
. /tmp/export$$

HTH

-- Rod Hills
There be dragons...
curt larson_1
Honored Contributor

Re: Awk help to process a resource file by a shell script

why parse a file at all?

create a seperate file for loading, running, etc.; put the files all in the same directory

and instead of lines like:
Running.NRT_DAYS=1

make them like this:
export NRT_DAYS=1

then exporting the variables is as easy as:

. $dirWhereFilesAre/$yourScriptName

just a suggestion
john korterman
Honored Contributor

Re: Awk help to process a resource file by a shell script

Hi,
no gurus around here...
But the attached script may serve as a starting point. Try it with your input file as $1.

regards,
John K.
it would be nice if you always got a second chance
Greg Vaidman
Respected Contributor

Re: Awk help to process a resource file by a shell script

Rod, I think you missed the part about matching the script_name...

Some of the other guys suggest sourcing in your resource file(s) directly, which will work fine, assuming you have the capability to make this change (it's a manually generated file, and not generated by some closed-source app).

However, this mechanism assumes a level of trust that noone has put malware-type commands into the resource file(s), as they would be happily executed with the permissions of the user running the script. Of course, if the write permissions on the resource file(s) are as restrictive or stronger than execute permissions on the script, then this isn't a big deal.

Just something to think about...

--Greg
Stuart Browne
Honored Contributor

Re: Awk help to process a resource file by a shell script

To add confusion to this is shell environment space protection.

Which shell are you going to use?

Generally you could use something like Rodney's solution (expanding upon that a little) in `` shell-execution, but some shells might not allow it.
One long-haired git at your service...
Amit Agarwal_1
Trusted Contributor

Re: Awk help to process a resource file by a shell script

$ cat myscript
!/usr/bin/awk
FILENAME=$1
awk -F'.' `
BEGIN { script_name=$FILENAME }
{ if($1==script_name) 'print "export "$2' }
`

$ myscript <script name>

I am not sure if the awk uses forward quotes or single quotes for instruction. You might try it. Sorry, I haven't tested this program. But am sure it will help you in writing one.
Amit Agarwal_1
Trusted Contributor

Re: Awk help to process a resource file by a shell script

Okay, I got access to a system. Here is the solution

$ cat myscript
FILENAME=$1
awk -v script_name=$FILENAME -F "." '
{ if($1==script_name) print "export " $2 }
'< resource_file

Output:
========
$ ./myscript Loading
export NRT_DAYS=1
export POLLING_INTERVAL=5
export MASK=*
$ ./myscript Running
export NRT_DAYS=1
export POLLING_INTERVAL=5

I hope this helps.

Amit

Muthukumar_5
Honored Contributor

Re: Awk help to process a resource file by a shell script

For Example, Details are in resource file as,

resource.log
Loading.NRT_DAYS=1
Loading.POLLING_INTERVAL=5
#Loading.POLLING_INTERVAL=30
Loading.MASK=*

Running.NRT_DAYS=1
Running.POLLING_INTERVAL=5
#Running.POLLING_INTERVAL=30

Then, You can try in each script file as,

---- Loading file ---
File=$(basename $0 | cut -d"." -f1)
for var in `grep $FILE resource.log | cut -d"." -f2-`
do
variable=$(echo $var | cut -d"=" -f1)
value=$(echo $var | cut -d"=" -f2)
export $variable=$value
done

Keep the same in all required files.

hth.

Easy to suggest when don't know about the problem!