Jump to content
Ric

Reading variables from a file [solved]

Recommended Posts

Can any one tell me how to read variables from a text file so they can be used with in a bash script

 

ie text file has

 

 
serverone_host=amber 
serverone_ip=10.0.0.1 
servertwo_host=green 
servertwo_ip=10.0.0.2 
and so on

 

bash file picks out the different variables so rather then having to edit a load of scrips i would only have to edit the text file each time i deploy a system

 

gee i am asking the question and that doesn't make sense to me hope it make sense to some one out there

 

Regards

 

Ric

Share this post


Link to post
Share on other sites

. data_file

(If it's in the same directory as your script)

 

. /full/path/to/data_file

(If it's n a different directory)

Note that there is a space between . and the filename.

Share this post


Link to post
Share on other sites

textfile:

serverone_host=amber
serverone_ip=10.0.0.1
servertwo_host=green
servertwo_ip=10.0.0.2

 

text.sh:

#!/bin/bash

. textfile

echo "$serverone_host"
echo "$serverone_ip"
echo "$servertwo_host"
echo "$servertwo_ip"

 

< omar ~ > ./text.sh
amber
10.0.0.1
green
10.0.0.2

Share this post


Link to post
Share on other sites

Thats excactly what i needed, ive been trying to grep and awk my way thro it, this is so much easier.

 

Thanks A Lot

 

Ric

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...