Usage of '-' in eval command.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Usage of '-' in eval command.
# 1  
Old 12-04-2013
Computer Usage of '-' in eval command.

Hi,

I am trying to use eval command to evaluate a variable(HAPROXY_LISTENER_rabbitmq_project-test-BRHM_PORT) which consists of '-' but unfortunately the eval command is unable to interpret the value of variable and trims the variable name after '-' and produces the string output rather than the actual value of the variable.Is it kind a restriction in eval command?What other options do we have?

Code:
eval 'HAPROXY_LISTENER_PORT=$HAPROXY_LISTENER_rabbitmq_project-test-BRHM_PORT'
HAPROXY_LISTENER_PORT=-test-BRHM_PORT


Last edited by Scrutinizer; 12-04-2013 at 02:44 PM.. Reason: code tags
# 2  
Old 12-04-2013
- is not a valid character for a variable name. If you include it by forcing it via ${variable-name}, it will take that to mean "default to the value of 'name' when 'variable' is undefined".

There are probably much, much, much better ways to accomplish your goal than 80-character variable names and mucking with eval. Can you please tell me your actual goal? And I don't mean "dynamic variable names", I mean the actual problem you are trying to solve.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 12-04-2013
- is not a valid character for a variable name.:Is this restriction in Unix?
# 4  
Old 12-04-2013
I can't think of any programming languages which allow it, shell or compiled, UNIX or otherwise.

You look like you are using Bourne shell. This is not UNIX, though it is often used inside UNIX. Don't mistake the channel for the TV.
# 5  
Old 12-06-2013
The hyphen can be used in CoBOL code, but it is considered as plain text for Unix shells. I've fallen across this the other way around when I wanted to set a variable to a value built from another variable like this:-
Code:
fileheader=myfile
rundate=`date +%Y%m%d`
filename=$fileheader_$rundate

I wanted to end up with myfile_20131206 as the filename format and I got very confused because it always evaluated to just 20131206 (or whatever the date was)

This was because the underscore is considered part of the variable name and the variable fileheader_ was undefined. This is not pretty, but I had to change the code to read:-
Code:
fileheader=myfile
rundate=`date +%Y%m%d`
filename=${fileheader}_$rundate

.... and learn from my error.

I too would like to know what you are actually trying to achieve. We might have a better way to do it, save you some hassle and provide something for others to read and learn from. Indeed, I'm quite happy to learn myself if there is a better way than I am currently using.



Robin
This User Gave Thanks to rbatte1 For This Post:
# 6  
Old 12-06-2013
Hi Robin/Corona688,
In your example its an underscore which works well but in my case the dynamic variable has a value which consist of a dash(-)
Actually am using eval to evaluate dynamic variables and one of the variable has the value with "-"(Dash).

Lets take the following example:
Code:
eval HAPROXY_LISTENER_PORT=\$HAPROXY_LISTENER_${HAPROXY_LISTENER_NAME}_PORT

where HAPROXY_LISTENER_NAME is a variable with value "search-engine" so the above variable HAPROXY_LISTENER_PORT should evaluate to this:
Code:
HAPROXY_LISTENER_PORT=$HAPROXY_LISTENER_search-engine_PORT

But actually it evaluates to this:

Code:
HAPROXY_LISTENER_PORT=-engine_PORT


Any suggestion would be deeply appreciated.

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags yourself. Thank you.

Last edited by bakunin; 12-06-2013 at 01:28 PM..
# 7  
Old 12-06-2013
Mostly, "don't do that". Dynamic variable names through eval hacks are a horrible idea.

If you have KSH, you can use associative arrays, so you can store A["really long string"] and get the results you want.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (13 Replies)
Discussion started by: vivek d r
13 Replies

2. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Help with perl eval command .....

Hi All, I read the above written code (perl code) in another perl script and evaluates this code for each line of text file,but using exit statement in code make this not to work and i could not get the desired results. However if i use return it works fine. I just need to know why it doesn't... (1 Reply)
Discussion started by: sarbjit
1 Replies

4. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

5. Shell Programming and Scripting

Eval command help

I have file called myfile which has the text "myserver" in it. I need to have a command to ping "myserver". How would I do that? I tried when I type at the terminal I get the output as . How do I do something like a ? thanks, Nick (5 Replies)
Discussion started by: nikhilfake
5 Replies

6. UNIX for Dummies Questions & Answers

Command to display the space usage (memory usage) of a specific directory.

Hi all, Can you please tell me the command, with which one can know the amount of space a specific directory has used. df -k . ---> Displays, the amount of space allocated, and used for a directory. du -k <dir name> - gives me the memory used of all the files inside <dir> But i... (2 Replies)
Discussion started by: abhisheksunkari
2 Replies

7. Shell Programming and Scripting

assignment to variable from eval command

Hi Gurus, I am having 2 parameters as below parm1=value1 parm2=parm1 I want to evaluate parm1 value using eval echo \$$parm2 and later i want to assign this value to other variable which i will be using in if statement like : if ]; then do this....... fi could you please suggest... (5 Replies)
Discussion started by: k_vikash
5 Replies

8. Shell Programming and Scripting

Help with eval usage for string containing Environment Variable

Help !! First, Thanks in Advance Here is what I have I have an environment Variable, let's call it v_VALUE. v_VALUE="\$ORACLE_HOME/bin" Hence, the location is ORACLE_HOME is not evaluated. ORACLE_HOME happens to be /app/oracle/product/10.1.2 I need a method of returning the... (1 Reply)
Discussion started by: dhangliter
1 Replies

9. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

10. Shell Programming and Scripting

Access to database/eval command

Hi i have the following code: if(($line!=1) and (@field!='\$')){ print ( "\nTRY TO CONNECT TO DATABASE................\n"); my $dbh = DBI->connect($dsn, $user, $pass); print ("CONNECTED TO DATABASE\n"); eval ... (1 Reply)
Discussion started by: chriss_58
1 Replies
Login or Register to Ask a Question