photo
Jordan Sissel
geek

Tue, 21 Oct 2003

bourne shell goodness.

So until now I never knew how to pipe things into my shell scripts - I'm smacking myself on the head now for not having figured this out sooner.

In unix, the standard file descriptors stdin, stdout, and stderr are numbered 0, 1, and 2, respectively. I've always done descriptor redirection, such as:

grep ^Poop myfile.txt 2>&1 > /dev/null

This will redirect stderr (fd #2) to stdout (fd #1). stdout is then redirected to /dev/null.

What I never bothered trying was redirecting stdin

#!/bin/sh

echo "Printing output:"
sed -e 's/^./Output: &/' <&0 

Note the <&0 - this will use our stdin as the stin for sed.
Now, let's try running it:
ls | ./myscript

The output is just how I want:

whack(~/bin) [590] > /bin/ls | sh myls.sh
Printing output:
Output: Mrandom
Output: fix
Output: hostlist
Output: intarweb
Output: logtail
Output: malbums
Output: mclear
Output: minfo
Output: myls.sh

Comments: 0 (view comments)
Tags:
Permalink: /geekery/77
posted at: 18:38

Search this site

Navigation

Metadata

Home About Resume My Code (SVN)

Articles

ARP Security Dynamic DNS with DHCP OpenLDAP+Kerberos+SASL PPP over SSH SSH Security: /bin/false Week of Unix Tools Work Efficiency

Projects

fex firefox tabsearch firefox urledit grok keynav liboverride newpsm (FreeBSD) nis2ldap pam_captcha poor man's backup Solaris audio utility xboxproxy xdotool xmlpresenter xpathtool misc scripts

Presentations

Yahoo! Hack Day '08 Yahoo! Hack Day '06 Unix Essentials Vi/Vim Essentials SSH Tunneling (Video)

Tag Cloud

Calendar

< October 2003 >
SuMoTuWeThFrSa
    1 2 3 4
5 6 7 8 91011
12131415161718
19202122232425
262728293031 

Friends

BarCamp Kent Brewster Tantek Çelik John Resig Wesley Shields Tyler Shields

Technorati