office microsoft outlook manage tips Microsoft Windows 7 Ultimate 64-bit microsoft office final exam microsoft office turorials Microsoft Office Visio Professional 2007 microsoft mouse driver for windows xp windows media center microsoft english Microsoft Windows 7 Home Premium 64 Bit microsoft windows start up tone microsoft office xp pro with frontpage Microsoft Windows 7 Professional beta information microsoft office system office xp microsoft outlook sp3 vista Microsoft Office Outlook 2007 microsoft office for windows xp microsoft office x mac Microsoft Windows 7 Ultimate (32 bit) microsoft windows user microsoft office 2007 training video Microsoft Windows XP Professional SP3 32-bit microsoft office setup cannot continue microsoft remote tools framework windows Microsoft Windows 7 Professional 64 Bit microsoft office standard 2003 key generator microsoft windows media player upgrade Microsoft Office 2003 Professional microsoft office 2003 upgrade requirements microsoft windows me repair Microsoft Office Project Professional 2003 microsoft windows network not accessible
msgbartop
I will happily conduct a FREE basic web security scan for any genuine organization interested in my services to point out whether or not I can find vulnerabilities in your application. Just contact me.
Need a PHP Programmer, PHP staff or project manager? Contact me now.
msgbarbottom

07 Mar 09 Linux piping and redirecting stdin stderr stdout

We have three relevant streams when dealing with passing data around on the command line. STDIN (0), STDOUT (1) and STDERR (2)

echo “hello” will return “hello” to STDOUT

echo “hello” | sed s/llo/y/g
Returns: ‘hey’

echo “hello” will print “hello” to STDOUT which we pipe to sed’s STDIN. The shell will fork both processes, echo and sed, and create a pipe between one’s STDOUT to the other’s STDIN. A ‘broken pipe’ will occur when one terminates unexpectedly.

strace echo “hello” will print the system calls that the command makes. Lets say I just want to print out open() calls.

strace echo “hello” | grep open does not work. It seems that the grep is ignored.

This is because strace sends it’s output to STDERR and not STDOUT. In this case we must redirect STDERR to STDOUT so grep can pick it up on it’s STDIN.

strace echo “hello” 2>&1 | grep open will work successfully.

What if we want to redirect STDOUT and STDERR to a file? We simply redirect STDOUT to a file and then redirect STDERR to STDOUT.

strace echo “hello” >/tmp/strace.output 2>&1

A nonstandard method of achieving the same by redirecting everything in one go is strace echo “hello” &>/tmp/strace.output however this is not guaranteed to work across all implementations.

* Post edited thanks to observations from Adam Bolte (16/11/09)

Tags: , , , , , , ,