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?

eval 'HAPROXY_LISTENER_PORT=$HAPROXY_LISTENER_rabbitmq_project-test-BRHM_PORT'
HAPROXY_LISTENER_PORT=-test-BRHM_PORT
  • 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.

1 Like
  • is not a valid character for a variable name.:Is this restriction in Unix?

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.

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:-

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:-

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

1 Like

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:

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:

HAPROXY_LISTENER_PORT=$HAPROXY_LISTENER_search-engine_PORT

But actually it evaluates to this:

HAPROXY_LISTENER_PORT=-engine_PORT

Any suggestion would be deeply appreciated.

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.

1 Like

OK, but this would only make sense if "search-engine" would be a variable with some content. This is not the case (and can't be, see above), so i still do not understand how this eval-construct came to pass.

Could you please explain:

where your values come from (say: a file, a network stream, whatever)
what you want to accomplish (in terms of goals, not techniques)

I am sure we could suggest a better method to accomplish what you want to do then what you came up with right now.

I hope this helps.

bakunin

1 Like

Hi Bakunin,

The value of HAPROXY_LISTENER_NAME is "search-engine" and just search-engine doesnt makes any sense it gets combined with HAPROXY_LISTENER
& PORT to form a variable HAPROXY_LISTENER_search-engine_PORT.This variable is exported and has a value so that it could be fetched.

You have restated the method you have chosen to solve your goal. Not your goal.

No it cannot. The command:

export HAPROXY_LISTENER_search-engine_PORT

is used to export variables, but the above command returns the following diagnostic:

export: HAPROXY_LISTENER_search-engine_PORT: is not an identifier

In the shell command language, in the C programming language, in the FORTRAN programming language, in the awk programming language, and in hundreds of other languages, "-" is not a valid character in a variable name.

If you are using a language where HAPROXY_LISTENER_search-engine_PORT is a valid variable name and you want to write a shell script to create names like this to be included in source code for that language, we can write shell scripts to do that. We cannot write a shell script that can use HAPROXY_LISTENER_search-engine_PORT as a shell variable name.