LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-26-2013, 10:56 AM   #1
mrajdeep
LQ Newbie
 
Registered: Aug 2011
Distribution: UBUNTU 10.04 Lucid Lynx
Posts: 17
Blog Entries: 1

Rep: Reputation: Disabled
not able to evaluate variable


serv1=`hostname`
a=1

when I use following

echo $'serv'"$a"

I get

serv1

and not the value of $serv1

please help.
 
Old 09-26-2013, 10:56 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
You'll need to use eval if you want to dynamically build the variable name

edit: or this
Code:
serv1=`hostname`
a=1
var=serv$a
echo ${!var}

Last edited by suicidaleggroll; 09-26-2013 at 11:04 AM.
 
Old 09-26-2013, 11:11 AM   #3
mrajdeep
LQ Newbie
 
Registered: Aug 2011
Distribution: UBUNTU 10.04 Lucid Lynx
Posts: 17

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
i tried this...but I get output as 1
 
Old 09-26-2013, 11:13 AM   #4
Pap
Member
 
Registered: May 2011
Distribution: Salix 14.1 GNU/Linux, 64-bit
Posts: 70

Rep: Reputation: 29
I don't see why using quotes here. This should work:
Code:
$ serv1='hostname'
$ a=1
$ echo $serv1$a
hostname1
What shell are you using? (find out by typing
Code:
ps -p $$
You should see something like
Code:
  PID TTY          TIME CMD
23811 pts/0    00:00:00 bash
The important part is "bash", or whatever it says there in your system.
 
Old 09-26-2013, 11:19 AM   #5
Pap
Member
 
Registered: May 2011
Distribution: Salix 14.1 GNU/Linux, 64-bit
Posts: 70

Rep: Reputation: 29
Oh wait, I get it now... You want to evaluate the hostname command. Then use this:
Code:
$ serv=`hostname`
$ a=1
$ echo $(hostname)$a
debian1
 
Old 09-26-2013, 11:22 AM   #6
mrajdeep
LQ Newbie
 
Registered: Aug 2011
Distribution: UBUNTU 10.04 Lucid Lynx
Posts: 17

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
its KSH

PID TTY TIME CMD
26986 pts/4 00:00:00 ksh

echo $serv1$a gives me output as hostname suffixed by 1

but i want to evaluate $serv1 dynamically..
 
Old 09-26-2013, 11:26 AM   #7
mrajdeep
LQ Newbie
 
Registered: Aug 2011
Distribution: UBUNTU 10.04 Lucid Lynx
Posts: 17

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by Pap View Post
Oh wait, I get it now... You want to evaluate the hostname command. Then use this:
Code:
$ serv=`hostname`
$ a=1
$ echo $(hostname)$a
debian1
there are different hostname i have stored as serv1, serv2, serv3 and so on

now i want to evaluate hostname based on choice made through case statement

something like if some one chose 1 then serv1 will be considered..
 
Old 09-26-2013, 11:28 AM   #8
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
Code:
serv1=`hostname`
a=1
var=serv$a
eval echo \$$var
 
Old 09-26-2013, 11:31 AM   #9
mrajdeep
LQ Newbie
 
Registered: Aug 2011
Distribution: UBUNTU 10.04 Lucid Lynx
Posts: 17

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Code:
serv1=`hostname`
a=1
var=serv$a
eval echo \$$var
Thank you very much. This works!

I'd really appreciate if you explai what happens here.

Thanks again.
 
Old 09-26-2013, 11:37 AM   #10
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
Quote:
Originally Posted by mrajdeep View Post
I'd really appreciate if you explai what happens here.
eval evaluates a command line after all the substitutions are made. In this case $var is substituted by its current value, while the escaped $ is protected by the substitution. At the end the command results in
Code:
echo $serv1
and this one is evaluated. There are some caveats about the usage of eval, anyway. For the sake of curiosity, You can read about them here: http://mywiki.wooledge.org/BashFAQ/048
 
Old 09-26-2013, 11:45 AM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by mrajdeep View Post
i tried this...but I get output as 1
Probably because you're using ksh, I believe ${!var} is bash-specific. When writing scripts, you should always add the "#!/bin/xxxx" at the beginning to control which shell is used to interpret the script, with that set to "#!/bin/bash" what I posted should work.

Code:
$ cat script
#!/bin/bash

serv1=`hostname`
a=1
var=serv$a
echo ${!var}

$ ./script
fry

Last edited by suicidaleggroll; 09-26-2013 at 11:46 AM.
 
Old 09-26-2013, 01:01 PM   #12
mrajdeep
LQ Newbie
 
Registered: Aug 2011
Distribution: UBUNTU 10.04 Lucid Lynx
Posts: 17

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Thank you very much colucix.

Thank you very much suicidaleggroll for providing such useful information.
 
Old 09-26-2013, 05:57 PM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Actually, I think he is trying to use an array. If so, look at the bash manpage for "arrays".
 
  


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] shell evaluate problem 915086731 Linux - General 3 09-02-2011 07:34 AM
[SOLVED] How to evaluate the value of a variable ? 915086731 Linux - General 4 08-11-2011 05:03 AM
/ disk full evaluate? EDDY1 Linux - Newbie 2 08-08-2011 07:04 PM
[bash] re-evaluate variable inside variable muzzol Programming 9 12-01-2010 11:31 AM
Basic Bash: How to use eval to evaluate variable names made of arbitrary strings. GrapefruiTgirl Programming 9 12-16-2009 10:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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