Re: Conditional alias and/or command (40 Views)
Reply
Super Advisor
john guardian
Posts: 309
Registered: ‎09-26-2003
Message 1 of 5 (40 Views)
Accepted Solution

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?
Please use plain text.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 2 of 5 (40 Views)

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
Please use plain text.
Super Advisor
john guardian
Posts: 309
Registered: ‎09-26-2003
Message 3 of 5 (40 Views)

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.
Please use plain text.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 4 of 5 (40 Views)

Re: Conditional alias and/or command

Joe,
can you please try and replace your ????? with
eval $CMD
Please use plain text.
Super Advisor
john guardian
Posts: 309
Registered: ‎09-26-2003
Message 5 of 5 (40 Views)

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
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation