Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Conditional alias and/or command

SOLVED
Go to Solution
Super Advisor

Conditional alias and/or command

Hi. I'm using a KSH script that incorporates functions and would like to assign a command to a var CMD for execution. For example, in one function within the script, I might assign the command var CMD to be 'echo -e' while in another function, I might assign the command var CMD to be 'set -x'. Once the command is assigned a value within the confines of the called function and returns to the main body of the shell script, it would need to be able to execute CMD.

Anyone?
4 REPLIES
Honored Contributor [Founder]

Re: Conditional alias and/or command

Joe,
test1()
{
var="b"
}
test2()
{
var="c"
}
var="a"
echo $var
test1
echo $var
test2
echo $var

would produce
a
b
c
Super Advisor

Re: Conditional alias and/or command

Yes, what you wrote is quite true. However, consider this example:

#!/usr/bin/ksh
function cmdA ()
{
if [ $DEBUG -eq 1 ]
then
CMD='set -x'
else
CMD=':'
fi
}

function cmdB
{
CMD='echo $$'
}

# Start of script
CMD=":"

#
# execute cmdB
cmdB

# Now execute what was assigned in cmdB
??????? <- this is what I'm interested in.

# execute cmdA
DEBUG=0
cmdA

# Now execute... as above
?????????

# execute cmdA
DEBUG=1
cmdA

# Now execute... as above
???????

exit 0

SO, what do I use in place of the ????? above.

Should it be

$CMD or `"$CMD"` or OR or...... maybe this will clarify what I'm actually asking.

Thanks.
Honored Contributor [Founder]

Re: Conditional alias and/or command

Joe,
can you please try and replace your ????? with
eval $CMD
Highlighted
Super Advisor

Re: Conditional alias and/or command

Thanks, Pete. I (could) swear I tried that on Monday (I'm bouncing back and forth between priorities). This happens to be in one of a series of shell scripts that I'm inheriting (where it has never worked).

JM