问题描述
这是我上一个关于 SO .我仍在尝试从另一个脚本shallowScript
中命令一个脚本deepScript
并处理其输出,然后再显示在终端上.这是一个代码示例:
This is a follow-up to my previous question on SO. I am still trying to command a script deepScript
from within another script shallowScript
and process its output before display on terminal. Here is a code sample:
deepScript.sh
#!/bin/zsh
print "Hello - this is deepScript"
read "ans?Reading : "
print $ans
shallowScript.sh
#!/bin/zsh
function __process {
while read input; do
echo $input | sed "s/e/E/g"
done }
print "Hello - this is shallowScript"
. ./deepScript.sh |& __process
(此语法的结果以及下面粘贴的2个替代方法的结果)
[UPDATE]
我为上次重定向. ./deepScript.sh |& __process
尝试了其他语法,每种语法都有不同的结果,但是当然,它们都不是我想要的.我将粘贴每种语法以及./shallowScript.sh
的结果输出(在read
等待输入时我在其中键入"input"),以及到目前为止的发现.
I have tried alternative syntaxes for last redirection . ./deepScript.sh |& __process
and each syntax has a different outcome, but of course none of them is the one I want. I'll just paste each syntax and the resulting output of ./shallowScript.sh
(where I typed "input" when read
was waiting for an input), together with my findings so far.
选项1: . ./deepScript.sh |& __process
通过此链接,似乎正在运行. ./deepScript.sh
从子shell,但不是__process
.输出:
From this link, it seems that . ./deepScript.sh
is run from a subshell, but not __process
. Output:
zsh : ./shallowScript.sh
Hello - this is shallowScript
HEllo - this is dEEpScript
input
REading : input
基本上,前两行按预期方式打印,然后脚本不等待提示REading :
的打印,而是直接等待stdin输入,然后然后打印提示并执行print $ans
Basically, the first two lines are printed as expected, then instead of printing the prompt REading :
, the script directly waits for the stdin input, and then prints the prompt and executes print $ans
.
选项2: __process < <(. ./deepScript.sh)
Zsh的联机帮助页表示(. ./deepScript.sh)
将运行作为子过程.在我看来,这类似于选项1 .输出:
Zsh's manpage indicates that (. ./deepScript.sh)
will run as a subprocess. To me, that looks similar to Option 1. Output:
Hello - this is shallowScript
Reading : HEllo - this is dEEpScript
input
input
因此,内 . ./deepScript.sh
,它将在打印(脚本第2行)之前打印read的提示(脚本第3行).奇怪.
So, within . ./deepScript.sh
, it prints read's prompt (script line 3) before the print (script line 2). Strange.
选项3: __process < =(. ./deepScript.sh)
根据相同的联机帮助页,(. ./deepScript.sh)
这里将其输出发送到一个临时文件,然后将其注入__process
(我不知道是否有子进程).输出:
According to the same manpage, (. ./deepScript.sh)
here sends its output to a temp file, which is then injected in __process
(I don't know if there is a subprocess or not). Output:
Hello - this is shallowScript
Reading : input
HEllo - this is dEEpScript
input
同样,deepScript的第3行在第2行之前打印到终端,但是现在它等待读取完成.
Again, deepScript's line 3 prints to the terminal before line 2, but now it waits for the read to be complete.
两个问题:
- 这应该在意料之中吗?
- 是否有修复程序或解决方法?
推荐答案
观察到的延迟源于两个因素:
The observed delay stems from two factors:
-
deepScript.sh
和process
异步运行 -
read
在返回之前先读完整行
deepScript.sh
andprocess
run asynchronouslyread
reads a complete line before returning
deepScript.sh
将提示写入标准错误,但没有换行符.然后,它会等待您的输入,而process
继续等待写整行,以便对read
的调用可以完成.
deepScript.sh
writes the prompt to standard error, but without a newline. It then waits for your input while process
continues to wait for a full line to be written so that its call to read
can finish.
这篇关于重定向/管道到读取命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!