我正在研究另一个问题,当我意识到我不明白幕后发生了什么,这些/dev/fd/*文件是什么以及子进程如何打开它们时。
我正在尝试errpipe使用stderr通过过滤器运行的简单 api编写实用程序脚本。起初我尝试使用 bash 的进程替换功能来实现它。
#!/bin/bash
com="$1"
errpipe="$2"
$com 2> >(1>&2 $errpipe)
Run Code Online (Sandbox Code Playgroud)
这样做的问题是当com不存在时输出看起来很奇怪。
如果我输入
sh-3.2$ ./errpipe foo cat
Run Code Online (Sandbox Code Playgroud)
我得到
sh-3.2$ ./errpipe foo cat
sh-3.2$ ./errpipe: line 6: foo: command not found
@
Run Code Online (Sandbox Code Playgroud)
用@代表光标。换句话说,shell 提示打印得太早了。我怀疑这是因为主 shell 脚本没有等待进程替换过程完成。在wait脚本末尾添加a似乎并不能解决问题。
我打开一个解决方案,使用bash,ksh,zsh或可能有些疯狂的awk功能。我想我知道如何使用 C 或 Perl 之类的东西将它们连接在一起,这些东西公开了更丰富的 API 来操作进程和文件描述符,但我想避免使用它,除非没有替代方案。
一种“几乎有效”的解决方案是使用在$$shell fork 时没有改变的事实,并在 errpipe 完成时向父级发出信号。
#!/bin/bash
com="$1"
errpipe="$2"
$com 2> >(1>&2 $errpipe; kill -SIGUSR1 $$)
while true; do …Run Code Online (Sandbox Code Playgroud) 我有一段使用进程替换的代码。
当我运行它时,它工作正常,但它不会退出 -只是挂在那里等待输入。
为什么以及如何克服?
prepro() {
in=$(< <(cat) ) # capture input
echo -e "$in"
}
echo -e "some words" > >(prepro)
echo "FINISHED"
Run Code Online (Sandbox Code Playgroud)
相反,如果我这样做,它会立即退出。
echo -e "$in" >>"test.log"
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么?
澄清: BASH 4.4.19(1)-release
在我的电脑上,我看到了 runningecho -e "$in"和 running之间的区别echo -e "$in" >>"test.log"。
前者不给我提示(但我可以正常运行命令)。我得到:
user@server:~$ ./test.sh
FINISHED
user@server:~$ some words
?
Run Code Online (Sandbox Code Playgroud)
我必须按 ENTER 才能恢复提示。
后者正常给我提示。
user@server:~$ ./test.sh
FINISHED
user@server:~$ ?
Run Code Online (Sandbox Code Playgroud)
我很好奇我在这里做错了什么。
I'm a new Linux user and I was doing some experiments and trying to understand Process Substitution. I believe I already have a basic understanding of it. But here is a case that I don't know why. I'm using Bash on Ubuntu 20.04.
echo hi just sends the string hi with a newline character to stdout.
root@u2004:~# echo hi | od -a
0000000 h i nl
0000003
root@u2004:~#
Run Code Online (Sandbox Code Playgroud)
cat can read from the pipeline as its stdin, and send …