LinuxQuestions.org
Help answer threads with 0 replies.
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-30-2010, 05:41 AM   #1
muzzol
LQ Newbie
 
Registered: Nov 2010
Posts: 6

Rep: Reputation: 0
Question [bash] re-evaluate variable inside variable


hi,

i need to re-evaluate a variable that is included into another variable but declared after.

little example:

Code:
#!/bin/bash

VAR1="simple text - $VAR2 - here"

VAR2="ZZZ"

echo "$VAR1"
i've been playing with indirect variables without success.

any hints?
 
Old 11-30-2010, 06:06 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
It is generally better to tell us what you want to achieve rather than asking us to fix a solution that may not be optimal.

BTW, the scrippet posted does not include any declarations unless you include implicit declaration by assignment.
 
Old 11-30-2010, 06:28 AM   #3
muzzol
LQ Newbie
 
Registered: Nov 2010
Posts: 6

Original Poster
Rep: Reputation: 0
sorry for not using correct terms, but the example i've provided is very similar to final script.

what i want is a script with few variables at the beggining (what i call configuration block) that will be edited by operators, and then the main code of the script. some of the variables of the configuration block can include variables that are used later.

i dont want operators mess with the code, just configure somethings. this is a little elaborated example:

Code:
#!/bin/bash

# configuration block
FLOG="$DIRLOG/register-in.log"

#############################################
## please dont touch nothing from this point
#############################################

DIRLOG="/usr/local/appregister/logs"

tail -f "$FLOG"
 
Old 11-30-2010, 06:38 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by muzzol View Post
i dont want operators mess with the code, just configure somethings.
That makes sense.

The tidiest solution would be to use an external file for the configuration data. Most simply this can be sourced into the master script. It's easier to illustrate than describe. Here's the config file, let's call it /etc/local/my.conf:
Code:
FLOG="register-in.log"
and here the script:
Code:
#!/bin/bash

source /etc/local/my.conf

DIRLOG="/usr/local/appregister/logs"
FLOG=$DIRLOG/$FLOG

tail -f "$FLOG"
This has the advantage that the script is not changed to configure anything which makes support a lot easier, including knowing which version is in use.
 
Old 12-01-2010, 03:10 AM   #5
muzzol
LQ Newbie
 
Registered: Nov 2010
Posts: 6

Original Poster
Rep: Reputation: 0
that's not really what i want.

anyway i've ended doing it like this:


Code:
#!/bin/bash

# configuration block
FLOG="<dirlog>/register-in.log"

#############################################
## please dont touch nothing from this point
#############################################

DIRLOG="/usr/local/appregister/logs"

FLOG=`echo "$FLOG" | sed "s|<dirlog>|$DIRLOG|"`

tail -f "$FLOG"
thx anyway
 
Old 12-01-2010, 03:14 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Glad you found a solution

Threads can be marked SOLVED using the Thread Tools menu.

Last edited by catkin; 12-01-2010 at 03:15 AM. Reason: CLOSED -> SOLVED
 
Old 12-01-2010, 03:25 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Well personally I cannot see how that is more maintainable, but good to see you found a solution.
 
Old 12-01-2010, 03:31 AM   #8
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
BTW, your original idea is quite possible, you're just missing
  1. a backslash which will keep bash from interpreting $VAR2 right away while it's still empty
  2. an 'eval' which tells bash to reevaluate its argument(s), this time replacing '$VAR2' with its content
E.g.:
Code:
#!/bin/bash

VAR1="simple text - \$VAR2 - here"

VAR2="ZZZ"

echo $VAR1
eval echo $VAR1
 
Old 12-01-2010, 06:22 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by grail View Post
Well personally I cannot see how that is more maintainable, but good to see you found a solution.
There are many possible solutions to the requirement with various pros and cons, each of which has more or less weight depending on context; it is hard to imagine a context in which the chosen solution is optimal.
 
Old 12-01-2010, 11:31 AM   #10
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
For configuration, IMO catkin's solution of sourcing a config file is the most elegant way to go about it, and the method I use whenever I need something like that. eval is bad here, you need to be doing something *really* special to justify all the headaches it brings.
 
  


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
Pipe inside variable isn't working in bash Reginald0 Linux - General 6 12-09-2015 09:56 AM
Basic Bash: How to use eval to evaluate variable names made of arbitrary strings. GrapefruiTgirl Programming 9 12-16-2009 10:25 AM
[Bash] Command inside a variable yvovandoorn Programming 5 01-20-2007 06:48 PM
Variable expansion inside of a bash script! A.S.Q. Linux - Newbie 4 09-29-2006 09:09 AM
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 08:50 PM

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

All times are GMT -5. The time now is 06:26 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