Operating System - HP-UX
1753853 Members
7722 Online
108808 Solutions
New Discussion юеВ

Echo and execute a command

 
Ed Hon
Regular Advisor

Echo and execute a command

Inside a Unix script, is there a way of echoing and executing a Unix command in a single line?
4 REPLIES 4
Madhu Sudhan_1
Respected Contributor

Re: Echo and execute a command

Ed :

#!/usr/bin/sh
echo date | sh

...Madhu


Think Positive
Bill Hassell
Honored Contributor

Re: Echo and execute a command

Perhaps what is needed is a way to show a command then show the results. How about something like:

CMD="vgdisplay -v | grep -i name | grep -iv lv"
echo "$CMD \n\n$(eval $CMD)"

Change the last value from lv to pv:

CMD="vgdisplay -v | grep -i name | grep -iv pv"
echo "$CMD \n\n$(eval $CMD)"

I use this technique to document inventory scripts that report on system components. That way, when someone notices an interesting listing, they don't have to ask "How'd you do that?"

The string of command(s) can contain variables so you can even customize the command, document it and then run it.

BTW: The above 2 command strings do a great job in laying out LVM structures in an easy to read format.


Bill Hassell, sysadmin
Tom Rosenfeld
Occasional Advisor

Re: Echo and execute a command

What about plain old 'set -v' this will echo all commands before execution.
Philip Chan_1
Respected Contributor

Re: Echo and execute a command

Just another option for you to consider, do nothing to your script but run shell with the -x switch,

eg 1.
ksh -x
eg 2.
sh -x
This way the command being executed will be printed out first then follow by the actual execution.