LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 11-26-2013, 06:50 AM   #1
spadez
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Rep: Reputation: Disabled
Bash to find all placeholder variables in a file and replace will real variables


I am looking to make a generic config file using variables such as:
$PLACEHOLDER_USER

Then after moving the file into the correct position I want to replace every instance of $PLACEHOLDER_VARIABLE into the value of $VARIABLE from my settings.sh file.

In the example below "$PLACEHOLDER_USER" in log.conf would be replace by "tom".

log.conf

deploy = $PLACEHOLDER_USER

settings.sh

USER="tom"

deploy.sh

FILE="/home/logging/log.conf"
ln -s /home/log.conf $FILE
while IFS== read variable value
do
sed -i "s/\$PLACEHOLDER_$variable/$value/g" $FILE
done < settings.sh

Is there a better way of doing this?
 
Old 11-26-2013, 09:33 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
A better way to take the long way around to do things?? Why not simply use the variables as they are named in settings.sh and then simply source that at the start of your script.

This way there is no need for any messy seds or altering IFS.

You would need to explain further why you think the method you are trying to employ is better??
 
Old 11-26-2013, 09:48 AM   #3
spadez
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
- Are you saying I can source the settings file no matter what style my configuration scritps are and what the program is?
- Would it impact my performance since I am linking my settings to another file or would they just be loaded into memory.
- What about being accessible by all different user accounts, would I have to duplicate the file in each users directory?

nginx-server.conf
server {
listen 80;
server_name api.$DOMAIN;

supervisor.conf
[unix_http_server]
file=/var/run/$ID.sock

uwsgi.conf
[uwsgi]
#master = true
processes = 2
uid = $USERNAME
 
Old 11-26-2013, 09:59 AM   #4
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
You could also use an existing macro processor such as cpp or m4. YMMV.
 
Old 11-26-2013, 10:10 AM   #5
spadez
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Why stray from bash. Using SED really isn't a big deal is it?
 
Old 11-26-2013, 11:11 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Quote:
Why stray from bash. Using SED really isn't a big deal is it?
Not sure I follow? If you use sed that your script is not strictly all bash but using an external command. The pros and cons are around whether or not it is more expedient to use sed and in this case I do not believe so.

As to your questions:

- Are you saying I can source the settings file no matter what style my configuration scripts are and what the program is?
What do you mean by style?
Are the scripts you are calling bash scripts, if so then yes source away

- Would it impact my performance since I am linking my settings to another file or would they just be loaded into memory.
The overhead would only really be if the location of the file prevented you sourcing it when needed, ie on a network connection that has yet to be connected

- What about being accessible by all different user accounts, would I have to duplicate the file in each users directory?
Well this would be a question for you, ie your example uses a USER variable, is this related to the user logged in or will it be the same user for all?
 
Old 11-26-2013, 12:24 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by grail
Why not simply use the variables as they are named in settings.sh and then simply source that at the start of your script.
It looks like the conf files are not bash, so I think that wouldn't work. Maybe something like

Code:
#!/bin/bash
# log.conf.template.sh
cat <<EOF
deploy = $PLACEHOLDER_USER
EOF
Code:
#!/bin/bash
# deploy.sh

. settings.sh
for file in log nginx-server supervisor uwsgi; do
   . templates/$file.conf.template.sh > destination/$file.conf
done
 
  


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
[SOLVED] replace multiple variables in file? bash nuser Programming 15 10-21-2012 01:02 PM
[SOLVED] sed doesn't accept variables to replace in file jomann Programming 7 03-18-2011 05:16 AM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
[SOLVED] [BASH] Find + sed --> can you pipe variables? RaptorX Programming 9 08-29-2009 08:48 AM
search and replace predefined variables in a file adam_blackice Programming 7 04-14-2008 08:54 PM

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

All times are GMT -5. The time now is 02:42 PM.

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