Register
It is currently Tue Oct 21, 2014 1:57 pm

Read an external file and set variabless


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Thu Feb 07, 2008 7:35 pm   

Joined: Thu Feb 07, 2008 6:20 pm
Posts: 1
My problem is this.
I open 3 0r 4 shell windows.
I run various scripts in each of the windows.
All the windows need the same basic data.

what i would like to do is this:

the data i use is stored in a file that has 1 line with 5 variables seperated by a pipeing character "|"
variable1|variable2|variable3|variable4|variable5

I need a snipplet of code to
read the file, set each variable to a $variable the script can use


all variables in the data file can have any character in them except the "|"

Thanks for any help


Top
 Profile  
 PostPosted: Fri Feb 08, 2008 5:00 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Hi,

Is the format of the data file fixed?
if you can set the content of the data file i suggest you setup a script like so...

Code:
# myvarsscript
varone=one
vartwo=two
varthree=three

export varone vartwo varthree


When you want to load the variables do...
Code:
. myvarscript


if the format of the data file cannot be changed then the following is much less flexible but may suffice

Code:
varone=$(awk -F\| '{ print $1 }' datafile)
vartwo=$(awk -F\| '{ print $2 }' datafile)
varthree=$(awk -F\| '{ print $3 }' datafile)
varfour=$(awk -F\| '{ print $4 }' datafile)
varfive=$(awk -F\| '{ print $5 }' datafile)


If they're fixed variables that you commonly need I personally would just added them to my .bashrc :)

DW


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP