TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Solving real-world problems with Linux's proc filesystem

23 点作者 nitefly超过 14 年前

1 comment

gnosis超过 14 年前
In the "redirect harder" section, the solution given to a program that needs an explicitly named file for standard input is:<p><pre><code> echo hello | python crap-prog.py /proc/self/fd/0 </code></pre> In zsh the same thing can be accomplished with a more concise syntax:<p><pre><code> python crap-prog.py &#60;(echo hello) </code></pre> And, if your process wanted to seek the file (which isn't possible to do with a simple redirection of stdin), you could do:<p><pre><code> python crap-prog.py =(echo hello) </code></pre> 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 =().<p>Another neat thing you can do in zsh to solve the original problem is to create a global alias for stdin:<p><pre><code> alias -g STDIN=/proc/self/fd/0 </code></pre> 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:<p><pre><code> echo hello | python crap-prog.py STDIN </code></pre> 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.<p>---<p>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:<p><a href="http://clpbar.sourceforge.net/" rel="nofollow">http://clpbar.sourceforge.net/</a><p><a href="http://www.ivarch.com/programs/pv.shtml" rel="nofollow">http://www.ivarch.com/programs/pv.shtml</a>