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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Pure sh bible – Posix sh alternatives to external processes

137 点作者 IA21超过 4 年前

7 条评论

klodolph超过 4 年前
I appreciate that this isn’t full of Bash-isms—it’s sometimes hard to find out how to do something in a shell script, because you get a bunch of Bash results.
评论 #24830526 未加载
评论 #24829083 未加载
评论 #24830670 未加载
ljm超过 4 年前
This is a nice resource. Many moons ago when I first heard of Kakoune [1], I wanted to write some of my own plugins for it. Whether or not the philosophy has changed, back then it was &#x27;just use shell scripts and make sure they&#x27;re POSIX compliant&#x27;.<p>That&#x27;s when I learned about things like named pipes, the `mkfifo` command, and that it does take quite a lot of conscious effort to not accidentally include a convenient bashism. That said, there was nothing stopping you from writing the main functionality in another language and just shelling out to it. No need for the editor&#x27;s config language to include primitives to do most of what a programming language would give you.<p>[1] <a href="http:&#x2F;&#x2F;kakoune.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;kakoune.org&#x2F;</a>
评论 #24830591 未加载
评论 #24831923 未加载
adrianmonk超过 4 年前
On the subject of optimizing tput, this article mentions using hard-coded strings. Another approach (for some tput commands) is to run the command once, save its output in a variable, and re-use it.<p>For example, a slow version of printing a blank chess board:<p><pre><code> #! &#x2F;bin&#x2F;sh for rowpair in 1 2 3 4 do for colpair in 1 2 3 4 do echo -n &quot; $(tput rev) $(tput sgr0)&quot; done echo for colpair in 1 2 3 4 do echo -n &quot;$(tput rev) $(tput sgr0) &quot; done echo done </code></pre> And a faster version:<p><pre><code> #! &#x2F;bin&#x2F;sh rev=$(tput rev) sgr0=$(tput sgr0) for rowpair in 1 2 3 4 do for colpair in 1 2 3 4 do echo -n &quot; $rev $sgr0&quot; done echo for colpair in 1 2 3 4 do echo -n &quot;$rev $sgr0 &quot; done echo done </code></pre> In many cases, tput is doing doing anything more than looking up a string and printing it. Though in other cases like &quot;tput cols&quot;, it is doing more. (And anyway, the number of columns isn&#x27;t a constant.)
评论 #24835051 未加载
评论 #24833085 未加载
emmelaich超过 4 年前
General comment ... I&#x27;m hesitant to fiddle with IFS; if the function exits early it can make the rest of the script nonsensical.<p>Same goes for other &#x27;globals&#x27; or externals such as globbing and using tput. You have to be careful to trap errors and return things to sanity.
评论 #24833959 未加载
emmelaich超过 4 年前
This uses e.g. i=$(($i + 1)) to increment.<p>The simpler and possibly faster way is just<p><pre><code> ((i++))</code></pre> or ((i+=1))<p>This syntax works in macos ksh, so pretty sure it&#x27;s POSIX.<p>Also, ksh93 supports sequence generation.. so as an alternative to the loops, use<p><pre><code> start=0 end=10 for i in {$start..$end}; do echo $i; done</code></pre>
评论 #24833065 未加载
ak217超过 4 年前
I wish Bash had proper support for in-process subshells. I timed my code recently, and a variety of loop constructs was dramatically faster without subshells - but often required crazy contortions to avoid spawning one.
senorsmile超过 4 年前
they also have a pure bash bible: <a href="https:&#x2F;&#x2F;github.com&#x2F;dylanaraps&#x2F;pure-bash-bible" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dylanaraps&#x2F;pure-bash-bible</a>