【发布时间】:2014-09-24 06:40:10
【问题描述】:
这是我之前在SO 上的问题的后续。我仍在尝试从另一个脚本shallowScript 中命令脚本deepScript 并在终端上显示之前处理其输出。这是一个代码示例:
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 个替代方法)
[更新]
我已经尝试了上次重定向. ./deepScript.sh |& __process 的替代语法,每种语法都有不同的结果,但当然它们都不是我想要的。我将粘贴./shallowScript.sh 的每个语法和结果输出(当read 等待输入时,我输入了“输入”),以及我目前的发现。
选项 1: . ./deepScript.sh |& __process
从这个link 看来,. ./deepScript.sh 似乎是从子shell 运行的,而不是__process。输出:
zsh : ./shallowScript.sh
Hello - this is shallowScript
HEllo - this is dEEpScript
input
REading : input
基本按预期打印前两行,然后不打印提示REading :,脚本直接等待stdin输入,然后打印提示并执行print $ans .
选项 2: __process < <(. ./deepScript.sh)
Zsh 的manpage 表示(. ./deepScript.sh) 将作为子进程运行。对我来说,这看起来类似于选项 1。输出:
Hello - this is shallowScript
Reading : HEllo - this is dEEpScript
input
input
所以,在. ./deepScript.sh,它在打印(脚本第2行)之前打印读取的提示(脚本第3行)。奇怪。
选项 3: __process < =(. ./deepScript.sh)
根据同样的manpage,(. ./deepScript.sh)这里将其输出发送到一个临时文件,然后注入__process(不知道有没有子进程)。输出:
Hello - this is shallowScript
Reading : input
HEllo - this is dEEpScript
input
再次,deepScript 的第 3 行在第 2 行之前打印到终端,但现在它等待读取完成。
两个问题:
- 应该这样吗?
- 是否有修复或解决方法?
【问题讨论】:
标签: bash shell scripting pipe zsh