TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Filenames and Pathnames in Shell: How to Do It Correctly (2010)

85 pointsby thefoxover 10 years ago

7 comments

Adaptiveover 10 years ago
One of the reasons I like zsh is its copious expansion flags. If you aren&#x27;t wedded to POSIX or bash syntax, consider zsh for this reason alone.<p>In the case of the first example he shows, the cat -n problem, you can do the following in zsh to expand the results of the wildcard globbing automatically:<p><pre><code> cat *(:A) </code></pre> This will give cat full paths. Not significantly different from the PWD referenced with .&#x2F;* in his example, but more universal in applicability, particularly when you start to use things like the file type filters:<p><pre><code> cat *(.:A) </code></pre> giving you only files, not directories, and also expanding to absolute paths, while<p><pre><code> cat **&#x2F;*(.:A) </code></pre> does the same for plain files in the working directory and all subdirectories as well.<p>Remember to test your patterns with a print statement first:<p><pre><code> print -l **&#x2F;*(.:A) </code></pre> before passing them to a command.
评论 #9024748 未加载
评论 #9024919 未加载
white-flameover 10 years ago
The original strength of Unix also ends up being such a commonly frustrating feature: Everything is marshalled through strings.<p>With human-manageable strings comes ambiguities, especially in concatenative situations like commandline expansion and SQL injection susceptible code.<p>There&#x27;s really no good universal solution. Judiciously adding explicit boilerplate as the article describes, or using less open-ended syntax which ends up adding common syntactic overhead as well, are both more painful to the user in common cases.
评论 #9024768 未加载
评论 #9024493 未加载
Scaevolusover 10 years ago
Shellcheck will find a broad variety of unsafe shell operations, including most (all?) of the issues on this page: <a href="http://www.shellcheck.net/" rel="nofollow">http:&#x2F;&#x2F;www.shellcheck.net&#x2F;</a>
deathanatosover 10 years ago
Today I learned globbing happens after word-splitting.<p>Can someone explain the following:<p><pre><code> for file in .&#x2F;* ; do # Prefix with &quot;.&#x2F;*&quot;, NEVER begin with bare &quot;*&quot; if [ -e &quot;$file&quot; ] ; then # Make sure it isn&#x27;t an empty match </code></pre> 1. Why prefix with a &quot;.&#x2F;&quot; ? Is that just to help avoid the `cat $filename` scenario? (i.e., that $filename will be &quot;.&#x2F;-n&quot; instead of &quot;-n&quot;, and that<p><pre><code> cat -- * </code></pre> is perfectly valid?)<p>2. What&#x27;s the -e check for? It says &quot;an empty match&quot; — -e means that the file exists, but * would only return files that exist, so -e must (with some caveats) be true. (The caveat being that there&#x27;s a race condition between the globbing and the test, but with the added test, there&#x27;s _still_ a race condition between the glob, the test, and the command execution. Are we just attempting to minimize the amount of race-condition by testing?)
评论 #9025819 未加载
bchover 10 years ago
Note also that some[1] commands will have a &quot;--&quot; (dash dash) flag indicating &quot;end of flags&quot;, so (eg): &quot;cat -- -n&quot; really would cat a file called &quot;-n&quot;[2].<p>[1] on my BSD system, many internal Tcl commands honor this convention. Damned if I can find a section 1 shell command that uses the convention, but I&#x27;m sure I&#x27;ve seen them.<p>[2] my version of cat doesn&#x27;t have a -- flag, so my example is contrived; not sure if GNU cat differs.<p>EDIT: typo, perl(1) supports &quot;--&quot;. See perlrun(1) for details.
swatowover 10 years ago
First step for doing things correctly:<p><pre><code> import shutil</code></pre>
artmageddonover 10 years ago
I&#x27;d love to know if there&#x27;s a Windows equivalent to this.
评论 #9024232 未加载
评论 #9024761 未加载