LinuxQuestions.org
Review your favorite Linux distribution.
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 12-05-2011, 03:59 PM   #1
jakev383
QmailToaster Developer
 
Registered: Dec 2005
Location: Burlington, NC
Distribution: CentOS, Voyage, Debian, Fedora
Posts: 220

Rep: Reputation: 31
Some help with variable expansion please


Hello all - I have a file, which contains some config variables:
Code:
#!/bin/sh
# filename: config_info
host1="192.168.0.1"
host2="192.168.0.2"
host3="192.168.0.3"
That file is then called by the one I wish to run some rsh commands - I've been testing and cannot seem to get the variable expansion to work like I'm expecting:
Code:
#!/bin/bash
# Let's pull in our host data
echo "Reading host config data...."
. config_info

echo
echo "to test....."
echo "host1 is ${host1}"
echo "host2 is ${host2}"
echo
for i in 1 2 3
  do
    echo "host$i IP is `${host[$i]}`"
    echo
  done
What I'm expecting is this:
Code:
host1 IP is 192.168.0.1
host2 IP is 192.168.0.2
host3 IP is 192.168.0.3
What I'm actually getting is:
Code:
host1 IP is
host2 IP is
host3 IP is
Anyone offer any suggestions on how to get the variables to expand like I'm expecting?
Ultimately, the variable will be used like this:
rsh -l root (variable for IP of host) version

Thanks!
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-05-2011, 04:15 PM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
is config_info executable?
id config_info in somewhere that it is going to be found?

Assuming that config_info is in the same location as the second script, is . in the PATH environment variable?

what happens if you type:
Code:
type config_info
 
Old 12-05-2011, 04:33 PM   #3
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,261

Rep: Reputation: 315Reputation: 315Reputation: 315Reputation: 315
You can use temporary variable to store variable name you want and then get value of this variable, like that:
Code:
for i in 1 2 3
  do
    HOST_ID="host$i"
    echo "host$i IP is ${!HOST_ID}"
    echo
  done
Probably this can be done in one line, but I don't known how.

Last edited by eSelix; 12-05-2011 at 04:36 PM.
 
2 members found this post helpful.
Old 12-05-2011, 05:00 PM   #4
jb_gpk
Member
 
Registered: Dec 2010
Distribution: Debian
Posts: 30

Rep: Reputation: 13
this work can be done by on line of awk:
Code:
awk 'BEGIN{FS="="}{ print $1 "IP^Cs " $2}' config_info
putting it on a script would be:
Code:
#!/usr/bin/awk -f
BEGIN{
        FS = "="
}
{
        print $1  " IP is " $2  
}

config_info don't need to be executable, so you should not put #!/bin/sh on this file.
Do you really need the double quotes around the IP number? if you remove it your output would be more clean.
 
Old 12-05-2011, 07:23 PM   #5
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
Quote:
Originally Posted by jakev383 View Post
Anyone offer any suggestions on how to get the variables to expand like I'm expecting?
Ultimately, the variable will be used like this:
rsh -l root (variable for IP of host) version

Thanks!
You could use eval like

Code:
...
for i in 1 2 3
  do
          eval "echo host$i IP is \$host$i"
    echo
  done
 
Old 12-06-2011, 07:01 AM   #6
jakev383
QmailToaster Developer
 
Registered: Dec 2005
Location: Burlington, NC
Distribution: CentOS, Voyage, Debian, Fedora
Posts: 220

Original Poster
Rep: Reputation: 31
Thanks eSelix - that got me what I was looking for I think.
Appreciate it!
 
Old 12-06-2011, 02:35 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
No, no, no. Please don't use the "${!HOST_ID}" style indirect variable expansion. It's both confusing and unnecessary. And using eval is even worse.

Your real problem was this:

Code:
host1="192.168.0.1"
host2="192.168.0.2"
host3="192.168.0.3"
The above lines set the ip addresses as a series of individual (scalar) variables. But what you tried to expand in your main script was this:

Code:
echo "host$i IP is `${host[$i]}`"
...which is array syntax. "${host1}" is not the same as "${host[1]}".


So what you need to put in your sourced file is this:
Code:
host[1]="192.168.0.1"
host[2]="192.168.0.2"
host[3]="192.168.0.3"
Or alternately:

Code:
host=(
	"192.168.0.1"
	"192.168.0.2"
	"192.168.0.3"
)
And use ${host[i]} syntax for all subsequent expansions.

http://mywiki.wooledge.org/BashGuide/Arrays

BTW: Notice also that a variable does not need to be prefixed by a "$" when inside a regular array's [] index field. Since the field operates in an arithmetic context, anything not a digit or a math operator is treated as a variable and expanded automatically.

(This is not true for associative arrays, however, which treat indexes as strings. Then you do need "$".)

BTW2, a sourced file doesn't need a shebang (#!) at the top, unless you also intend it to be capable of running as a stand-alone script.

Last edited by David the H.; 12-06-2011 at 02:37 PM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Variable and shell expansion timbCFCA Linux - Server 2 07-15-2011 02:21 PM
[SOLVED] Bash variable expansion problem JulianTosh Programming 2 09-06-2009 04:54 AM
variable expansion in bash coolhandluke1 Programming 4 01-09-2008 04:45 PM
Variable expansion in BASH champak Programming 5 11-26-2007 03:44 AM
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 11:38 AM

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

All times are GMT -5. The time now is 02:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration