Re: need bash variable syntax help

2014-06-08 Thread ToddAndMargo

On 06/06/2014 08:52 PM, ToddAndMargo wrote:

Hi All,

In Bash script language, how do I create a variable name
from a variable?

I am trying to create a variable called abcStatus

x=abc
$xStatus=xyz

obviously doesn't work.  What am I doing wrong?

Many thanks,
-T



Hi All,

For an eval free version:

x=z
a=x
echo ${!a}
z

-T


Re: need bash variable syntax help

2014-06-07 Thread Nico Kadel-Garcia
On Fri, Jun 6, 2014 at 11:52 PM, ToddAndMargo toddandma...@zoho.com wrote:
 Hi All,

 In Bash script language, how do I create a variable name
 from a variable?

 I am trying to create a variable called abcStatus

 x=abc
 $xStatus=xyz

 obviously doesn't work.  What am I doing wrong?

 Many thanks,
 -T

It's not really core to Scientific Linux itself, but:

For most bash cases where I've found myself wanting something like
this, I've usually wound up using hashes instead. The first hash,
abc, contains a list of variable names, abcstatus, abctime, abcdate,
abcowner, etc. that can then be referrenced to refer to other valures
or hashes as needed.


Re: need bash variable syntax help

2014-06-07 Thread Mark Stodola

On 6/7/2014 11:25 AM, Nico Kadel-Garcia wrote:

On Fri, Jun 6, 2014 at 11:52 PM, ToddAndMargotoddandma...@zoho.com  wrote:

Hi All,

In Bash script language, how do I create a variable name
from a variable?

I am trying to create a variable called abcStatus

x=abc
$xStatus=xyz

obviously doesn't work.  What am I doing wrong?

Many thanks,
-T

It's not really core to Scientific Linux itself, but:

For most bash cases where I've found myself wanting something like
this, I've usually wound up using hashes instead. The first hash,
abc, contains a list of variable names, abcstatus, abctime, abcdate,
abcowner, etc. that can then be referrenced to refer to other valures
or hashes as needed.
I agree with everything said so far, but would like to toss in a chapter 
from the Advanced Bash-Scripting Guide (ABS).
The dangers are of course still present, but might give a bit better 
explanation about what is going on.  I actually do have scripts I use 
that utilize the \$$var method.


http://www.tldp.org/LDP/abs/html/ivr.html


need bash variable syntax help

2014-06-06 Thread ToddAndMargo

Hi All,

In Bash script language, how do I create a variable name
from a variable?

I am trying to create a variable called abcStatus

x=abc
$xStatus=xyz

obviously doesn't work.  What am I doing wrong?

Many thanks,
-T


Re: need bash variable syntax help

2014-06-06 Thread Brandon Vincent
There is probably a better way, (bash declare perhaps?), but:

x=abc
eval $(echo $x)Status=This works!

Brandon Vincent

On Sat, Jun 7, 2014 at 3:52 AM, ToddAndMargo toddandma...@zoho.com wrote:
 Hi All,

 In Bash script language, how do I create a variable name
 from a variable?

 I am trying to create a variable called abcStatus

 x=abc
 $xStatus=xyz

 obviously doesn't work.  What am I doing wrong?

 Many thanks,
 -T


Re: need bash variable syntax help

2014-06-06 Thread ToddAndMargo

On Fri, Jun 6, 2014 at 8:52 PM, ToddAndMargo toddandma...@zoho.com
mailto:toddandma...@zoho.com wrote:

Hi All,

In Bash script language, how do I create a variable name
from a variable?

I am trying to create a variable called abcStatus

x=abc
$xStatus=xyz

obviously doesn't work.  What am I doing wrong?

Many thanks,
-T




On 06/06/2014 09:09 PM, Patrick J. LoPresti wrote:

x=abc
eval ${x}Status=xyz
echo $abcStatus

  - Pat



Hi Pat,

  Wow.  I would have never figured that out on my own.

  Thank you!


Follow on question: this works, but I would like
to clean it up:

xx=ech
eval $xxo abc
abc

-T

--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~


Re: need bash variable syntax help

2014-06-06 Thread ToddAndMargo

On Sat, Jun 7, 2014 at 3:52 AM, ToddAndMargo toddandma...@zoho.com wrote:

Hi All,

In Bash script language, how do I create a variable name
from a variable?

I am trying to create a variable called abcStatus

x=abc
$xStatus=xyz

obviously doesn't work.  What am I doing wrong?

Many thanks,
-T




On 06/06/2014 09:14 PM, Brandon Vincent wrote: There is probably a 
better way, (bash declare perhaps?), but:


 x=abc
 eval $(echo $x)Status=This works!

 Brandon Vincent


Hi Brandon,

   I completely blanked on eval.  Thank you!

-T

--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~


Re: need bash variable syntax help

2014-06-06 Thread Brandon Vincent
Actually, it would be good if someone with a good deal of bash
scripting experience chimed in.

eval is not safe to use in a bash script. If the input is crafted
cleverly, the script can execute unwanted commands. Not to mention the
above examples don't work great with characters like ! which are
evaluated as the built-in history command by eval. You need to
actually escape my example with backslashes for that reason.

Brandon Vincent

On Sat, Jun 7, 2014 at 4:25 AM, ToddAndMargo toddandma...@zoho.com wrote:
 On Sat, Jun 7, 2014 at 3:52 AM, ToddAndMargo toddandma...@zoho.com
 wrote:

 Hi All,

 In Bash script language, how do I create a variable name
 from a variable?

 I am trying to create a variable called abcStatus

 x=abc
 $xStatus=xyz

 obviously doesn't work.  What am I doing wrong?

 Many thanks,
 -T



 On 06/06/2014 09:14 PM, Brandon Vincent wrote: There is probably a better
 way, (bash declare perhaps?), but:


 x=abc
 eval $(echo $x)Status=This works!

 Brandon Vincent


 Hi Brandon,

I completely blanked on eval.  Thank you!


 -T

 --
 ~~
 Computers are like air conditioners.
 They malfunction when you open windows
 ~~


Re: need bash variable syntax help

2014-06-06 Thread ToddAndMargo

On 06/06/2014 09:23 PM, ToddAndMargo wrote:

On Fri, Jun 6, 2014 at 8:52 PM, ToddAndMargo toddandma...@zoho.com
mailto:toddandma...@zoho.com wrote:

Hi All,

In Bash script language, how do I create a variable name
from a variable?

I am trying to create a variable called abcStatus

x=abc
$xStatus=xyz

obviously doesn't work.  What am I doing wrong?

Many thanks,
-T




On 06/06/2014 09:09 PM, Patrick J. LoPresti wrote:

x=abc
eval ${x}Status=xyz
echo $abcStatus

  - Pat



Hi Pat,

   Wow.  I would have never figured that out on my own.

   Thank you!


Follow on question: this works, but I would like
to clean it up:

xx=ech
eval $xxo abc
abc

-T




How do I echo the variable?

echo $(eval ${x}Status)

isn't working

--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~


Re: need bash variable syntax help

2014-06-06 Thread Brandon Vincent
eval variable=${x}Status
echo ${!variable}

Brandon Vincent


Re: need bash variable syntax help

2014-06-06 Thread zxq9
I think this is probably an X-Y problem.
http://mywiki.wooledge.org/XyProblem

Something missing is a description what the OP is trying to accomplish.

At least in shell scripts, every time I've thought I needed to declare a 
variable name dynamically it turned out I was missing a better solution 
somewhere. (In the case of metaprogramming I've never found Bash to be the 
right tool.)

On Saturday 07 June 2014 04:41:14 Brandon Vincent wrote:
 eval variable=${x}Status
 echo ${!variable}
 
 Brandon Vincent


Re: need bash variable syntax help

2014-06-06 Thread Brandon Vincent
As zxq9 mentioned, there is probably a better way to do this.
Honestly, the examples with eval should not be used.

Here is the article I was looking for:

http://mywiki.wooledge.org/BashFAQ/048

Brandon Vincent