shell脚本输入重定向古怪 [英] Shell scripting input redirection oddities

查看:151
本文介绍了shell脚本输入重定向古怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释这种现象?\r
运行:

Can anyone explain this behavior? Running:

#!/bin/sh
echo "hello world" | read var1 var2
echo $var1
echo $var2

\r
\r

结果没有被输出中,而

results in nothing being ouput, while:

#!/bin/sh
echo "hello world" > test.file
read var1 var2 < test.file
echo $var1
echo $var2

\r
\r

产生预期的输出结果:

produces the expected output:

hello
world

\r
\r

不应该管在一个步骤做什么重定向到TEST.FILE在第二个例子那样?我试图在同一code用破折号和Bash shell,并从他们两个人得到了相同的行为。

Shouldn't the pipe do in one step what the redirection to test.file did in the second example? I tried the same code with both the dash and bash shells and got the same behavior from both of them.

推荐答案

最近除了庆典 lastpipe 选项,允许最后一个命令在管道在当前shell中运行,而不是一个子shell当作业控制被停用。

A recent addition to bash is the lastpipe option, which allows the last command in a pipeline to run in the current shell, not a subshell, when job control is deactivated.

#!/bin/bash
set +m      # Deactiveate job control
shopt -s lastpipe
echo "hello world" | read var1 var2
echo $var1
echo $var2

确实会输出

hello
world

这篇关于shell脚本输入重定向古怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆