LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-09-2010, 08:02 AM   #1
manickaraja
Member
 
Registered: Sep 2003
Location: India
Posts: 36

Rep: Reputation: 0
Question on Shell Script


Hi,
Below is the requirement. Please let me know if anyone can suggest a solution.

There is a host variable by name MOUNT1 which holds value /ora1

$ echo $MOUNT1
/ora1

Similarly there are 5 variables (MOUNT1 thorugh MOUNT5) with different mount point values. The number suffix after MOUNT (1 in above example) is coming dynamically from a varaible VAR

$ echo $VAR
1

The value of VAR will vary in a loop (first time it will be 1, second time it will be 2 etc.. after reaching 5 it will again start from 1)

Now how do I get the "/ora1" value inside the loop.

Something like echo $MOUNT$VAR. But its not simple. echo $MOUNT$VAR will not work (it will just return 1 instead of /ora1). Let me know if any of you can find the answer. Please let me know incase if you could not understand the puzzle.
 
Old 02-09-2010, 08:21 AM   #2
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
The following should do:

Code:
echo $( eval \$MOUNT$VAR )
 
Old 02-09-2010, 08:35 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I think it's better
Code:
eval echo \$MOUNT$VAR
otherwise the shell tries to execute the value of the variable as a command. Some details about indirect variable reference, here.

Last edited by colucix; 02-09-2010 at 08:38 AM.
 
Old 02-09-2010, 09:44 AM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Use Arrays:
http://mywiki.wooledge.org/BashFAQ/005
 
Old 02-09-2010, 10:07 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by manickaraja View Post
Hi,
Below is the requirement. Please let me know if anyone can suggest a solution.

There is a host variable by name MOUNT1 which holds value /ora1

$ echo $MOUNT1
/ora1

Similarly there are 5 variables (MOUNT1 thorugh MOUNT5) with different mount point values. The number suffix after MOUNT (1 in above example) is coming dynamically from a varaible VAR

$ echo $VAR
1

The value of VAR will vary in a loop (first time it will be 1, second time it will be 2 etc.. after reaching 5 it will again start from 1)

Now how do I get the "/ora1" value inside the loop.

Something like echo $MOUNT$VAR. But its not simple. echo $MOUNT$VAR will not work (it will just return 1 instead of /ora1). Let me know if any of you can find the answer. Please let me know incase if you could not understand the puzzle.
Hi,

I'm not sure if I understand the problem but I tried the following:
Code:
for VAR in $(seq 5); do
 echo $MOUNT$VAR
done
and this resulted in
Code:
/ora1
/ora2
/ora3
/ora4
/ora5
Is this the output you expected?
- If yes, are you using this loop in a script? Was MOUNT declared outside that script? Are you using pipes?
- If no, please post the code that you tried to execute

Last edited by crts; 02-09-2010 at 10:09 AM.
 
Old 02-12-2010, 06:56 AM   #6
manickaraja
Member
 
Registered: Sep 2003
Location: India
Posts: 36

Original Poster
Rep: Reputation: 0
Thanks a lot for all your help and guidance. I was able to resolve the issue by using below command.

eval echo \$MOUNT$VAR

Thanks,
Manick.
 
  


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
Shell Script Question keenboy Linux - General 10 10-22-2009 10:08 AM
Shell script question. pete1234 Programming 10 09-06-2005 02:46 AM
Shell Script Question. rvijay Linux - General 2 07-14-2005 06:41 PM
shell script question yoderp Programming 6 06-16-2005 04:47 PM
shell script question rrwhite Linux - General 3 09-15-2004 01:01 AM

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

All times are GMT -5. The time now is 06:49 AM.

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