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 07-01-2010, 07:41 AM   #1
TVT
LQ Newbie
 
Registered: Oct 2006
Posts: 24

Rep: Reputation: 0
Question SH: Recursive Variable Substitution


Hi!

I have the following test code fragment:

Code:
#! /bin/sh

A1='ZZZ'

B='A'
N='1'

if [ "${$B$N}" == 'ZZZ' ]; then echo 'OK'; fi
When I try to run it 'sh' reports the following message^

Code:
test.sh: ${$B...}: Bad substitution
You might understand what I mean with that test code so could you recommend how to achieve the required effect?
 
Old 07-01-2010, 08:49 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hmm. I think you are looking for eval.

Code:
A1="ZZZ"
V1="A"
V2="1"
VARNAME="$V1$V2"
eval VALUE=\$$VARNAME
echo $VALUE
 
Old 07-01-2010, 09:21 AM   #3
TVT
LQ Newbie
 
Registered: Oct 2006
Posts: 24

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Guttorm View Post
Hmm. I think you are looking for eval.

Code:
A1="ZZZ"
V1="A"
V2="1"
VARNAME="$V1$V2"
eval VALUE=\$$VARNAME
echo $VALUE
YES but how to elegantly use your way with 'if' statement?
 
Old 07-01-2010, 09:27 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You could possibly go one line less too:
Code:
A1="ZZZ"
V1="A"
V2="1"
eval VARNAME="$V1$V2"
echo ${!VARNAME}
Just an alternative at the end their for indirect

Edit: Use the "if" instead of "echo" line
 
Old 07-01-2010, 12:57 PM   #5
TVT
LQ Newbie
 
Registered: Oct 2006
Posts: 24

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
You could possibly go one line less too:
Code:
A1="ZZZ"
V1="A"
V2="1"
eval VARNAME="$V1$V2"
echo ${!VARNAME}
Just an alternative at the end their for indirect

Edit: Use the "if" instead of "echo" line
Doesn't seem to solve the difficulty:
Code:
$ sh -x test1.sh
+ A1=ZZZ
+ V1=A
+ V2=1
+ eval VARNAME=A1
+ VARNAME=A1
test1.sh: ${!V...}: Bad substitution
 
Old 07-01-2010, 01:35 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by TVT View Post
Doesn't seem to solve the difficulty
Is your shell by any chance a "true" Bourne Shell?
 
Old 07-01-2010, 03:09 PM   #7
TVT
LQ Newbie
 
Registered: Oct 2006
Posts: 24

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
Is your shell by any chance a "true" Bourne Shell?
No. My shell is just ordinary /bin/sh shell (see message title). This is because some systems do not have bash by default.
 
Old 07-01-2010, 03:30 PM   #8
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
No. My shell is just ordinary /bin/sh shell (see message title). This is because some systems do not have bash by default.
Just because it's /bin/sh doesn't give *any* information on whether it's a actual legacy Bourne shell or a POSIX shell. Please verify that it really is POSIX, not Bourne.
 
  


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] bash variable substitution Jerry Mcguire Programming 6 04-29-2010 09:33 AM
[SOLVED] variable substitution in awk statement emmalg Linux - Software 12 07-02-2009 08:39 AM
Trying to combine lookahead and variable substitution djeepp Programming 1 09-03-2008 04:32 PM
variable substitution in sed gaynut Programming 1 07-14-2008 07:38 AM
Bash variable substitution daYz Programming 3 04-14-2006 01:16 PM

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

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