Solving real-world problems with Linux's proc filesystem

  • In the "redirect harder" section, the solution given to a program that needs an explicitly named file for standard input is:

      echo hello | python crap-prog.py /proc/self/fd/0
    
    In zsh the same thing can be accomplished with a more concise syntax:

      python crap-prog.py <(echo hello)
    
    And, if your process wanted to seek the file (which isn't possible to do with a simple redirection of stdin), you could do:

      python crap-prog.py =(echo hello)
    
    The main difference is that here, behind the scenes zsh creates and deletes a temporary file with the contents of the stdout of the program between the =().

    Another neat thing you can do in zsh to solve the original problem is to create a global alias for stdin:

      alias -g STDIN=/proc/self/fd/0
    
    Then, whenever you typed "STDIN" on the command line, zsh would convert it to "/proc/self/fd/0". Then to make the exact equivalent of the original solution (but one that's easier to remember and type), you could simply write:

      echo hello | python crap-prog.py STDIN
    
    A regular alias won't work here, since regular aliases only expand when they're the first part of a command. Global aliases will expand anywhere in the command line. So you just have to be a bit more careful with them.

    ---

    The solution in the "phantom progress bar" section is cute, and a good demonstration of what's possible to do with /proc/$PID/fdinfo, but you could just use a ready made solution like either of these:

    http://clpbar.sourceforge.net/

    http://www.ivarch.com/programs/pv.shtml

  • undefined