[vox-tech] quick, stupid bash question

vox-tech@lists.lugod.org vox-tech@lists.lugod.org
Wed, 29 May 2002 15:18:08 -0400


On Wed, May 29, 2002 at 11:29:29AM -0700, Peter Jay Salzman wrote:
> begin nbs <nbs@sonic.net> 
> >      strace lsof 2>&1 1> /dev/null | grep
>  
> ok, haven't tried this, but this looks to me like:
> 
> put stderr into stdout
> redirect stdout (and therefore stderr) into /dev/null
> pipe stdout (which should be null) to grep.

  The order of operation of file operators is right to left.  

  So the first operation "| grep" spawns a copy of grep on the 
stdout of a future output of a new pipe.  The second "1> /dev/null" 
takes stdout and throws it away.  The third "2>&1" takes stderr and 
redirects it to stdout.


  The command line you are looking for if you want stderr grepped
and stdout to still be displayed is some variant of this:

 ( strace ls -R / 2>&1 1>&3 | grep open; ) 3>&1


(which will show you each open call and the output that ls prints
after each, alternating... sample output below, kinda cool).

  here is a breakdown of the command-line above in order of operation...
=========
"( ... ) 3>&1"

  open a file descriptor 3 and attach it to my current stdout.

"( ... )"

  run the following in a subshell

"... | ..."

  run the thing on the left and direct it's stdout into the thing on the
  right.

"grep open"

  look for the word "open" in my stdin.

"1>&3"

  redirect stdout to file descriptor 3.

"2>&1"

  redirect stderr to stdout.
  
"strace ls -R /"

  strace the output of "ls -R /"
=========


  Note that the sample command above is just a sample, if you really
wanted all the times strace detected an open call then give strace the 
option "-e open" ... which is much better.

sample output from the new command:
=========
msimons@star:~/debian$ ( strace ls -R / 2>&1 1>&3 | grep open; ) 3>&1
open("/etc/ld.so.preload", O_RDONLY)    = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 4
open("/lib/librt.so.1", O_RDONLY)       = 4
open("/lib/libc.so.6", O_RDONLY)        = 4
open("/lib/libpthread.so.0", O_RDONLY)  = 4
open("/dev/null", O_RDONLY|O_NONBLOCK|O_DIRECTORY) = -1 ENOTDIR (Not a directory)
open("/", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY) = 4
open("/etc/mtab", O_RDONLY)             = 4
open("/proc/meminfo", O_RDONLY)         = 4
/:
bin    cdrom   etc     ibm2    lost+found  proc  share	tmp  vmlinuz
boot   cdrom0  floppy  initrd  mnt	   root  test	usr  vmlinuz.old
brick  dev     home    lib     opt	   sbin  thor	var

open("/bin", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY) = 4
/bin:
arch   dd	      fuser	ls	netstat    sed	      true	  zforce
ash    df	      grep	lspci	pidof	   setserial  umount	  zgrep
bash   dir	      gunzip	mkdir	ping	   sh	      uname	  zless
cat    dmesg	      gzexe	mknod	ps	   sleep      uncompress  zmore
chgrp  dnsdomainname  gzip	mktemp	pwd	   stty       vdir	  znew
chmod  echo	      hostname	more	rbash	   su	      zcat
chown  ed	      kill	mount	readlink   sync       zcmp
cp     egrep	      ln	mt	rm	   tar	      zdiff
cpio   false	      loadkeys	mt-gnu	rmdir	   tempfile   zegrep
date   fgrep	      login	mv	run-parts  touch      zfgrep

open("/boot", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY) = 4
/boot:
System.map-2.4.18	 boot-menu.b  boot.b		   map
System.map-2.4.18-bf2.4  boot-text.b  chain.b		   os2_d.b
boot-bmp.b		 boot.0200    config-2.4.18	   vmlinuz-2.4.18
boot-compat.b		 boot.0801    config-2.4.18-bf2.4  vmlinuz-2.4.18-bf2.4
=========