Operating System - HP-UX
1822420 Members
3107 Online
109642 Solutions
New Discussion юеВ

Executing commands with parameters in variables

 
SOLVED
Go to solution
Henrik Rasmussen
Occasional Advisor

Executing commands with parameters in variables

I am making a shell script (/usr/bin/sh which I think is actually a POSIX Shell on HPUX 11.11).

The script is used in a cluster when cron is executing a command, to determine whether the cluster package is present (or has failed over to another node), before actually executing the desired command.

The shell script itself takes arguments to determine whether it has to change user, source the users profile etc. before executing the command, and it logs the output from the command to a file, giving the logfile the same name as the actual command or Oracle SQL script with the extention .log

When all the check etc. is processed, the command is executed using 2 variables

$COMMAND $RUNCOMMAND

where the content of the variable

$COMMAND may be /bin/su - USER -c
$RUNCOMMAND is for example who -r

The echoed variables, just before execution, contains all desired commands and options, but the command which is executed after changing user, is only the command without options (in this case it only executes the who command, but is missing the -r option)

I have tried a lot of different ways of quoting (both with and without escaping quotes with backslash). I also tried using eval and exec, but this doesn't help.

How can I make sure that the command is executed with the options contained in the $RUNCOMMAND variable?

Henrik
5 REPLIES 5
Dennis Handly
Acclaimed Contributor

Re: Executing commands with parameters in variables

The arguments to su are the arguments to sh. And you need to quote the argument to -c:
$ $COMMAND "$RUNCOMMAND"

>The echoed variables, just before execution, contains all desired commands and options

You need to write yourself a printenv script that prints all of argv. You'll see in your case you had two parms, who and -r.
After sh's -c, that -r may be a sh option.
Steven E. Protter
Exalted Contributor

Re: Executing commands with parameters in variables

Shalom,

try runing in sh -x mode or with set -x

Environment problem likely.

Please post output:
echo the variables just prior to execution.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Dennis Handly
Acclaimed Contributor

Re: Executing commands with parameters in variables

A follow up, depending on COMMAND, sometimes you need to quote RUNCOMMAND and most of the time you don't.
Dennis Handly
Acclaimed Contributor
Solution

Re: Executing commands with parameters in variables

>depending on COMMAND, sometimes you need to quote RUNCOMMAND

A simple solution is to have a variable per argument, then you always quote them. You can use an array:
$ $COMMAND "${RUNCOMMAND[@]}"
Henrik Rasmussen
Occasional Advisor

Re: Executing commands with parameters in variables

Thanks to both of you for replies.

Steven for the input.

Dennis, I already tried a variaty of quoting

${COMMAND} ${RUNCOMMAND}
${COMMAND} "${RUNCOMMAND}"
${COMMAND} \"${RUNCOMMAND}\"

But it seems that using the version you wrote (without array though):

$COMMAND "${RUNCOMMAND}"

and executing the script using quotation on of the who -r command in the commandline, did the trick:

/usr/contrib/bin/cmcpp.sh -P SERVER -p -u USER -c "who -r"

Thanks.