- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Question about shell script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-12-2009 05:37 AM
тАО03-12-2009 05:37 AM
I make a shell script and i want to do this:
VAR1="casa"
VAR2=1
echo $(echo VAR${VAR2})
The result is "VAR1" instead of "casa".
What was I doing wrong???
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-12-2009 05:46 AM
тАО03-12-2009 05:46 AM
Re: Question about shell script
To me, it looks correct. It should echo "VAR1". If you want "casa1" to be echo'd, then the command should be
#echo $VAR1$VAR2
Good luck
Shahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-12-2009 05:54 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-12-2009 05:55 AM
тАО03-12-2009 05:55 AM
Re: Question about shell script
VAR[1]=casa
VAR2=1
echo ${VAR[$VAR2]}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-12-2009 05:56 AM
тАО03-12-2009 05:56 AM
Re: Question about shell script
I want print the content of the variable VAR1.
My will is, in the first echo - (echo VAR${VAR2}) print VAR1 and with the second echo command - (echo $VAR1) print "casa".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-12-2009 05:58 AM
тАО03-12-2009 05:58 AM
Re: Question about shell script
Variable expansion doesnt work that easy.
What you can do if you run bash or maybe ksh (not really sure actually) is the following.
#!/bin/bash
array[1]="Casa"
array[2]="Next thing"
array[3]="And so on"
ptr=1
echo ${array[$ptr]}
ptr=2
echo ${array[$ptr]}
ptr=3
echo ${array[$ptr]}
This also works when adding things.
x=0
for i in List of words; do
array[$x]=$i
let x=x+1
done
Best regards
Fredrik Eriksson
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-12-2009 06:05 AM
тАО03-12-2009 06:05 AM
Re: Question about shell script
typeset -n someVariable="VAR$VAR2"
not sure where to go with this now....
OR
Try what Patrick suggested.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-12-2009 06:06 AM
тАО03-12-2009 06:06 AM
Re: Question about shell script
expands step by step like this:
echo $(echo VAR${VAR2}) # original
=> echo $(echo VAR1) # ${VAR2} == 1
=> echo "VAR1" # $(echo VAR1) == "VAR1"
=> VAR1 # the output
If you want to use the value of the variable VAR2 as a part of the name of the variable you ultimately want, you would need a construct like this:
eval echo \${VAR${VAR2}}
=> eval echo ${VAR1} # backslash is removed, ${VAR2} == 1
=> echo casa # eval expands variables again, ${VAR1} == casa
=> casa
You will need "eval" to trigger a second pass of variable expansion, and the backslash to protect the first $ to remain unexpanded through the first expansion pass.
MK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-12-2009 06:18 AM
тАО03-12-2009 06:18 AM
Re: Question about shell script
Patrick gave me a good solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-14-2009 03:16 AM
тАО03-14-2009 03:16 AM