Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Conditional alias and/or command

SOLVED
Go to solution
john guardian
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
Peter Godron
Honored Contributor

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
john guardian
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.
Peter Godron
Honored Contributor
Solution

Re: Conditional alias and/or command

Joe,
can you please try and replace your ????? with
eval $CMD
Highlighted
john guardian
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