LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-24-2005, 03:11 PM   #1
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Red face (automatic) exporting content of file


Hi all,

Thought I had an understanding of this, guess I'm wrong.......

Problem is this: I have an input file, db.conf, looking like this (shortened):

Code:
# start db config file

# tets01
dbname=tets01
dm="Some Bloke"
tak="o=left"

# dummy01
dbname=dummy01
dm="someuser"
tak="o=right"

# end db config file
I need to export dbname, dm and tak, based on the db given (tets01 or dummy01 in the above example).
The export part is giving me trouble, instead of exporting 3 variables it exports 1 (dbname), which holds all other entries.

The script (tailored for this question):
Code:
#!/bin/bash
# -------------------------------------------------------------------------- #
#set -x
DB="$1"
export dummy1=testing123 dummy2="123 testing"

for EXPORT_ME in "`awk -v DB="${DB}" \
              'BEGIN { FS="\n" ; RS="" }
               $1 ~ DB { print $2, $3, $4, $5 }
              ' db.conf`"
do
  export "$EXPORT_ME"
done

echo "----------------------------------------------------------"
echo "dummy1 : $dummy1"
echo "dummy2 : $dummy2"
echo "----------------------------------------------------------"
echo "dbname : $dbname"
echo "dm     : $dm"
echo "tak    : $tak"
echo "----------------------------------------------------------"

# -------------------------------------------------------------------------- #
# end
Example run:

Code:
$ ./db.parser tets01
----------------------------------------------------------
dummy1 : testing123
dummy2 : 123 testing
----------------------------------------------------------
dbname : tets01 dm="Some Bloke" tak="o=left" 
dm     : 
tak    : 
----------------------------------------------------------

$ ./db.parser dummy01
----------------------------------------------------------
dummy1 : testing123
dummy2 : 123 testing
----------------------------------------------------------
dbname : dummy01 dm="someuser" tak="o=right" 
dm     : 
tak    : 
----------------------------------------------------------
I think this has to do with the export "$EXPORT_ME" not expanding the content because of the double quotes. Loosing them is not an option 'cause the dm entry can have spaces (tak as well).

set -x shows this difference between the pasing of the dummy's and the parsing of EXPORT_ME:

dummy: + export dummy1=testing123 'dummy2=123 testing'
export_me: + export 'dbname=tets01 dm="Some Bloke" tak="o=left" '

Which says it all.

Anyway around this? db.conf file cannot change, all the rest is useless anyway so hack away

Thanks in advance.
 
Old 05-24-2005, 03:38 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Different approach?

If I understand correctly what you're trying to do,....
Code:
#!/bin/bash

DB="$1"
TMPFILE="/tmp/db_one_record_$$"

awk -v DB="${DB}" \
 'BEGIN { FS="\n" ; RS="" }
 $1 ~ DB { print }
 ' db.conf >$TMPFILE
source $TMPFILE
rm $TMPFILE

echo "----------------------------------------------------------"
echo "dbname : $dbname"
echo "dm     : $dm"
echo "tak    : $tak"
echo "----------------------------------------------------------"
Doet dit wat je wilt?
 
Old 05-24-2005, 03:43 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Original Poster
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Yep, that works for me. Was focusing too much on solving this to think about a temporary file.....

Thanks again.

Oftewel: Mijn dank grenst aan hondsdolheid!

Sorry about the dutch folks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk and change file content Ayman.mashal Programming 4 06-02-2005 06:03 AM
Modify content of .deb file njes Debian 2 05-21-2005 09:11 AM
find the same content in the file ust Linux - Newbie 5 03-23-2005 03:03 AM
How to re-build a corrupted content file ? ]un]ie Solaris / OpenSolaris 0 09-06-2004 05:16 AM
Monitoring a Specific Port and Exporting to a log file chrisfirestar Linux - General 0 10-27-2003 04:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration