[NTLUG:Discuss] printing bash commands as output

Patrick R. Michaud pmichaud at pobox.com
Fri Dec 7 00:17:12 CST 2007


On Fri, Dec 07, 2007 at 05:52:56AM +0000, Leroy Tennison wrote:
> There are situations where it would be nice to document a process and 
> it's results, for example:
> 
> grep -v ^# /etc/inittab | tr -s \\n
> 			(with the following output)
> si::sysinit:/etc/rc.d/rc.sysinit
> l0:0:wait:/etc/rc.d/rc 0
> l1:1:wait:/etc/rc.d/rc 1
> l2:2:wait:/etc/rc.d/rc 2
> l3:3:wait:/etc/rc.d/rc 3
> l4:4:wait:/etc/rc.d/rc 4
> l5:5:wait:/etc/rc.d/rc 5
> l6:6:wait:/etc/rc.d/rc 6
> ca::ctrlaltdel:/sbin/shutdown -t3 -r now
> pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
> pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
> 1:2345:respawn:/sbin/mingetty tty1
> 2:2345:respawn:/sbin/mingetty tty2
> 3:2345:respawn:/sbin/mingetty tty3
> 4:2345:respawn:/sbin/mingetty tty4
> 5:2345:respawn:/sbin/mingetty tty5
> 6:2345:respawn:/sbin/mingetty tty6
> x:5:respawn:/etc/X11/prefdm -nodaemon
> 
> If I redirect the output of a bash script containing the above command 
> pipeline to a file it will contain the results but I won't know what 
> produced the results (the command pipline doesn't end up in the output 
> file).  Is there an easy way to get both?

In bash, try "set -x".  Then every command gets echoed as part of
the output (preceded by a '+').

I do this often in script files when I want to capture the commands
that produce the output as well as the output itself.

Pm



More information about the Discuss mailing list