What is wrong with this indirect command when run with eval ?
#!/bin/bash
OS=AIX
host=myhost
CMD_AIX="(o=\`host \"$host\" \`)"
CMD=\$CMD_$OS
echo $CMD
eval echo $CMD
eval "$CMD"
Ouput:
$ myscript.sh
$CMD_AIX
(o=`host "myhost" `)
myscript.sh: line 12: (o=`host: command not found
EDIT - NOTES: It's in a very heterogeneous structure with about 40 servers, mixing SCO, AIX and Linux, several versions of each, including variations of 32 and 64 bits. SCO and AIX cannot be updated. Bash and Korn shell are common in all servers but I'm limited to version 3 of bash (in SCO), Ksh has several differences between them, so we're preferring bash, which haven't support to associative arrays in v3 (it cannot be updated and it's outside of my decisions, and these servers will begin to be replaced by AIX 7 and Linux in a few months).
There are a big system to be maintained which includes some scripts already based in sh and bsh, we won't and cannot reconstruct it now, just tittle maintenance to resist some months until the end of replacement.
The approach used in these scripts is largely spread in a big solution which generates some scripts, which we cannot change it from scratch.
The sample code above is just an adopted snippet, it isn't the real code. Please don't consider the logic, just the problem of indirection (eval).
SOLUTION: I got a solution that worked very fine just chaining the eval, like:
torun=`eval $CMD`
output=`eval torun`
This answer and that another one, which worked very fine and answered the question, were oddly down voted a lot.
$(...)
for subshells instead of backticks. It will avoid the obvious escaping nightmare you're getting yourself into. Since you run the command locally, what's wrong witho=$(host "$host)"
? – 0xC0000022L May 25 '15 at 21:54eval
- and am rather fond of it personally - I can say that it is theeval
code that would give me pause - more likely than any other kind - before I went snipping and sticking. – mikeserv Jun 12 '15 at 18:33eval is evil
and the fact that it also call eval two times, one on it's output, which is a bit silly...although it work in this case. – Nordine Lotfi Mar 23 '21 at 15:23