重击pwd并用管道打开不起作用 [英] Bash pwd and open with pipe not working
问题描述
我正在学习如何在bash中使用I/O重定向,并尝试了一些类似管道的示例:
I'm learning how to work with I/O Redirection in bash and tried some examples with the pipe like:
pwd | say
ls -l | grep "staff" > out.txt
但是当我尝试通过管道重定向同时使用pwd
和open
时,该命令将失败,并且我只会收到open
的用法.
But when I try using both pwd
and open
using pipe redirection, the command fails and I just receive open
usage.
我正在尝试的是:pwd | open
从bash打开当前目录的正确方法是什么?
What is the correct way to open the current directory from bash?
推荐答案
借助事后观察,有 2个不同的答案:
A:要回答OP的问题 ,以 了解管道和 I/O流 :
A: To answer the OP's question as asked with a view to understanding pipes and I/O streams:
echo . | xargs open
# Equivalent solution, using the special "$PWD" shell variable.
printf '%s\0' "$PWD" | xargs -0 open
是通过管道将当前目录路径传递到open
CLI 的最健壮方法,以便在OSX的文件系统浏览器GUI应用程序
is the most robust way to pass the current directory's path to the open
CLI via a pipe in order to have it open that directory in OSX's file-system-browser GUI application, Finder
.
请注意,pwd | xargs open
并不健壮,因为如果当前目录路径具有嵌入式空格,它将失败-请参见下文.
Note that pwd | xargs open
is NOT robust, because it fails if the current directory path has embedded whitespace - see below.
-
open
要求输入是通过命令行参数 提供的,而不是通过stdin
输入流提供的,例如就是这种情况(通过管道|
). - 因此,为了将标准输入转换为参数,需要标准的
xargs
实用工具. -
xargs
接受标准输入-在这种情况下来自管道-并调用指定为 its 参数(open
, (在这种情况下)使用标记化的stdin输入作为该命令的自变量 . -
xargs
默认按空白分隔:- 使用
.
作为输入,不会发生拆分(或由外壳解释),因此可以将其拆分为xargs
. - 相比之下,特殊的
$PWD
shell变量始终包含当前目录的完整路径,可能包含 embedded 空格,因此 extra需要采取步骤以确保将其作为单个参数传递给open
:-
printf '%s\0' "$PWD"
打印当前目录.以NUL字节(0x0
)终止的完整路径. - 互补地,
xargs -0
通过NUL将stdin输入拆分为令牌-在这种情况下,生成一个单个令牌,并保留单个输入行的值-包含$PWD
- as是.这是安全的,因为NUL不是文件名中的合法字节.open
requires input to be provided via command-line arguments rather than via thestdin
input stream, as is the case here (via a pipe,|
).- Thus, in order to translate stdin input to arguments, the standard
xargs
utility is needed. xargs
takes stdin input - coming from a pipe in this case - and invokes the command specified as its argument (open
, in this case) with the tokenized stdin input as that command's arguments.xargs
splits by whitespace by default:- With
.
as the input, no splitting (or interpretation by the shell) occurs, so it can just beecho
ed toxargs
. - By contrast, the special
$PWD
shell variable, which always contains the current dir.'s full path, may contain embedded whitespace, so extra steps are needed to ensure that it's passed toopen
as a single argument:printf '%s\0' "$PWD"
prints the current dir.'s full path terminated with a NUL byte (0x0
).- Complementarily,
xargs -0
splits stdin input into tokens by NUL - resulting in a single token in this case, preserving the value of the single input line - containing$PWD
- as is. This is safe to do, because NUL is not a legal byte in filenames.- Note:
-0
is a nonstandard extension to the POSIX standard forxargs
, but it's implemented in both BSDxargs
(as aused on OSX) and GNUxargs
(as used on Linux). Remaining POSIX-compliant[1], the next best thing is to use line-based tokenizing with the-I
option as follows:
printf '%s' "$PWD" | xargs -I % open %
因此,实际上上述解决方案与等效(
xargs
最终执行的操作-请参见下一节的解释):Thus, in effect the above solutions are equivalent to (what
xargs
ultimately executes - see next section for an explanation):open . # Equivalent solution. open "$PWD"
[1]
xarg
's-I
option requires a POSIX system that is also XSI-compliant. If anyone can explain to me what that means in practical terms, I'd be grateful.B:要为最佳答案提供不受技术限制的 :
B: To provide the best answer with no constraints on techniques employed:
open .
open
期望输入参数而不是stdin
,并且.
简洁有效地表示当前目录,从而导致open
在. open
expects arguments rather thanstdin
input, and.
succinctly and most efficiently represents the current directory, causingopen
to display the contents of the current folder inFinder
.等效但更冗长的解决方案是传递特殊的shell变量
$PWD
,该变量始终包含当前目录的完整路径(在 double 引号中引用,以防止它受到外壳扩展:An equivalent, but more verbose solution is to pass the special shell variable
$PWD
, which always contains the full path of the current directory (referenced in double quotes, so as to protect it from shell expansions:open "$PWD"
这篇关于重击pwd并用管道打开不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Note:
- With
-
- 使用