LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-03-2012, 11:27 PM   #1
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
Help in bash scripting.


Filename a:

Content:

#!/bin/bash

msg='hi'
${msg}_status=hello

echo -n ${msg}_status

I guess you can understand my requirement.But I'd explain it line by line..
1. Assigning the string "hi" to "msg" variable.

2.I want a variable with name "hi_status" & the string "hello" should be assigned to it.
i.e substituting the above variable here/.

3. i want to print the varaible(hi_status)now.

Output expected: hello

Current output of the above script:
./a: line 3: hi_status=hello: command not found
 
Old 12-04-2012, 12:24 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,431

Rep: Reputation: 2768Reputation: 2768Reputation: 2768Reputation: 2768Reputation: 2768Reputation: 2768Reputation: 2768Reputation: 2768Reputation: 2768Reputation: 2768Reputation: 2768
yes, in such cases one can use eval:
Code:
eval ${msg}_status=hello
eval echo -n \$${msg}_status
should work, but you will see soon: in general eval is not a good idea (to use), you would need to find a better way
 
Old 12-04-2012, 08:53 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,511

Rep: Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891
Although many of us, myself included, go down this path in early scripting, I would advise against it as it serves no real purpose. If you need a status linked to a name instead of a value
simply use associative arrays or choose a language that allows dictionary / hash key combinations, like Ruby, Python or Perl.
 
Old 12-05-2012, 08:45 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
How can I use variable variables (indirect variables, pointers, references) or associative arrays?
http://mywiki.wooledge.org/BashFAQ/006

As said, this is almost certainly not the best way to accomplish your purpose. And in particular, you should never use eval unless absolutely necessary. In modern scripting this should be very rare.

Eval command and security issues
http://mywiki.wooledge.org/BashFAQ/048

If you could explain your larger purpose in more detail, perhaps we could help you develop a more appropriate solution.

PS: please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

Last edited by David the H.; 12-05-2012 at 08:47 PM.
 
  


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
bash scripting playonmonkey Programming 4 02-17-2012 10:15 AM
bash scripting ninjafairy Programming 11 05-18-2011 10:00 AM
Bash scripting navie Linux - Newbie 1 11-11-2010 12:21 PM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
BASH Scripting ? eroica Programming 3 06-07-2004 07:51 PM

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

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