[Cialug] Having a "this should work" moment

Josh More morej at alliancetechnologies.net
Thu Oct 1 20:58:02 CDT 2009


Unix systems have what are called "standard streams".  This is what
allow us to do pipe magic and feed input and output to various loops and
do scary complex things like "cat smb.actions | smbclient
//192.168.0.100/micads -A /root/backups/smb.connect > smb.log 2>&1".  (I
did one earlier today that took four lines and three loops and wound up
telling me that my system wasn't hacked :)

The three streams are:

0) Standard Input
1) Standard Output
2) Standard Error

You can reference them by their file descriptor (the number) or various
variables depending on which standard libraries you wish to use.  What
Ken did here was redirect Standard Error (2) into Standard Out (1) using
the Bash method of 2>&1.  (Other shells may use other methods.)

Thus, though it seems backwards, you have to remember that Bash parses
the entire command line before it passes anything to the commands on it.
 By moving the 2>&1 to the end, Bash doesn't see it as an argument to
the commands, but does the redirection AND THEN runs the commands.  In
effect, 

For more reading, start here: 
http://en.wikipedia.org/wiki/Standard_streams , then spend the next four
hours clicking through obscure POSIX history.  (Alternatively, if you
just care about the specific issues that could arise from improper
stream redirection, you may wish to start here: 
http://en.wikipedia.org/wiki/Proton_pack#Crossing_the_Streams )





-Josh More, RHCE, CISSP, NCLP, GIAC 
 morej at alliancetechnologies.net 
 515-245-7701

>>> Tim Champion <timchampion at gmail.com> 10/01/09 8:43 PM >>>
On Thu, Oct 1, 2009 at 5:03 PM, Ken MacLeod <ken at bitsko.slc.ut.us>
wrote:

> On Thu, Oct 1, 2009 at 4:42 PM, Paul Gray <gray at cs.uni.edu> wrote:
>
>> cat smb.actions | smbclient //192.168.0.100/micads -A\
>> /root/backups/smb.connect 2>&1 > smb.log
>>
>
> Other way around,
>
>     >smb.log 2>&1
>
>
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/listinfo/cialug
>
>
Ah yes, Ken's solution worked. Thanks.

Not like it was a huge deal, but it just should have worked!  Now who
wants
to go about explaining what exactly 2>&1 means?

Tim Champion
timchampion at gmail.com



More information about the Cialug mailing list