long command executed via a variable fails

Geoffrey KRETZ gk@4js.com
Wed Nov 17 17:46:00 GMT 2004


Actually my original unix script use :

if ! $cmd; then
...
fi

AND

if ! eval $cmd; then
...
fi

And it works perfectly

I'm forced to use eval because I'm of the structure of my $cmd variable.

Just a few samples :
- cmd="$LDPRELOAD $FGLRUN ${mainname}.42r -db $dbname -dt $dbtype -ho 
$QAHOST $dvmversionopt -dc $dbcode -dynamic -tn 99",
with LDPRELOAD = "LD_PRELOAD='$FGLDIR/bin/dbmdb28x.so'" and $FGLRUN is a 
runner use to run an executable
- cmd ="$COMPILATOR fglmksdl -d $dbcode $mksdlopt"
with $COMPILATOR = "INFORMIXC='$FGLCC -shared'", $FGLCC is a compilator 
with some option (ie: gcc -mlp64 or cc -Wl,-G,-dy) and fglmksdl a (my 
company) command that generate shared libraries.

I've got to use carefully the quotes and double-quotes and without eval, 
I doesn't work on Unix.

Whatever, I'll try : eval "$cmd" and read the sh man page ;)

Geo


Igor Pechtchanski wrote:

>On Wed, 17 Nov 2004, Reini Urban wrote:
>
>  
>
>>Geoffrey KRETZ schrieb:
>>    
>>
>>>I've got a problem with a shell script used with Cygwin 1.5.10-3 on W2000
>>>SP4 and W XP SP 2.
>>>
>>>The following part of code works on all the Unix I've tested (HP-UX/AIX/Sun
>>>Solaris/Linux).
>>>
>>>With Cygwin,  it doesn't :(
>>>
>>>*Code:*
>>>
>>>cmd="long shell command with differents parameters"
>>>if [ ! eval $cmd ]; then
>>> echo "Error : $cmd"
>>> exit 1
>>>fi
>>>      
>>>
>
>This doesn't do what you thought it would.  Please read the sh manual on
>the "if" builtin command.  You're actually invoking the "[" command with
>the parameters "! eval $cmd", which is not what you want.
>
>  
>
>>>instead of eval $cmd, i've tried :
>>>- `eval $cmd`
>>>- eval `$cmd`
>>>- $cmd
>>>- `$cmd`
>>>      
>>>
>
>Most of the above (except $cmd) show that you don't understand much about
>command substitution.  Please read the sh man page carefully.
>
>BTW, given that sh's "eval" behaves differently from bash's, you should
>actually use 'eval "$cmd"' (note the double quotes).
>
>  
>
>>>I've also try with a function without more success.
>>>
>>>*Code:*
>>>
>>>execCmd()
>>>{
>>> eval $cmd
>>> return $?
>>>}
>>>
>>>cmd="long shell command with differents parameters"
>>>if [ ! execCmd ]; then
>>> echo "Error : $cmd"
>>> exit 1
>>>fi
>>>      
>>>
>
>This doesn't work since $cmd is unset in the function.  Either define the
>function after setting cmd, or pass $cmd as a parameter, e.g.,
>
>execCmd() {
>  cmd="$1"; shift
>  eval "$cmd"
>  return $?
>}
>
>BTW, your "if" test suffers from the same problem as your original one.
>Ditch the "[".."]".
>
>  
>
>>>The only way I've find is :
>>>
>>>*Code:*
>>>
>>>cmd="long shell command with differents parameters"
>>>eval $cmd
>>>if [ $? -ne 0 ]; then
>>> echo "Error : $cmd"
>>> exit 1
>>>fi
>>>      
>>>
>
>Note that this works because you're actually using "[".."]" in the right
>way here.
>
>  
>
>>>Is it possible to make it work like the two first exemple or I'm
>>>obliged to use the third solution ?
>>>      
>>>
>>FAQ: http://cygwin.com/faq/faq_3.html#SEC43
>>cygwin's /bin/sh is ash, on most other platforms it is /bin/bash.
>>
>>If you want it to behave it exactly like on other platforms, and you use
>>bash specific constructs, use the /bin/bash shebang.
>>    
>>
>
>Well, eval is not a bash-specific construct.  It does behave slightly
>differently in ash than in bash, but in this particular case, switching to
>bash wouldn't have helped the OP.  Reading the man pages for bash and sh
>would have. :-)
>HTH,
>	Igor
>  
>


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list