在bash脚本中动态重定向标准输入 [英] Redirect standard input dynamically in a bash script

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

问题描述

我正在尝试执行此操作,以确定是否将stdin重定向到文件:

[ ...some condition here... ] && input=$fileName || input="&0"
./myScript < $input

但这不起作用,因为当变量$ input为& 0"时,bash会将其解释为文件名.

但是,我可以这样做:

if [ ...condition... ];then
    ./myScript <$fileName
else
    ./myScript

问题在于./myScript实际上是一个长命令行,我不想重复,也不想为其创建函数,因为它也不那么长(不值得).

然后我想到要这样做:

[ ...condition... ] && input=$fileName || input=  #empty
cat $input | ./myScript

但这需要再运行一个命令和一个管道(即子外壳).
还有另一种更简单,更有效的方法吗?

解决方案

首先stdin是文件描述符0(零)而不是1(stdout).

您可以像这样有条件地复制文件描述符或使用文件名:

[[ some_condition ]] && exec 3<"$filename" || exec 3<&0

some_long_command_line <&3

请注意,如果条件为错误的,则显示的命令将执行第二个exec,第一个exec失败.如果您不想这样做,那么应该使用if/else:

if [[ some_condition ]]
then
    exec 3<"$filename"
else
    exec 3<&0
fi

,但是如果第一次重定向失败(在条件为真之后),则随后从文件描述符3进行的重定向将失败.

I was trying to do this to decide whether to redirect stdin to a file or not:

[ ...some condition here... ] && input=$fileName || input="&0"
./myScript < $input

But that doesn't work because when the variable $input is "&0", bash interprets it as a filename.

However, I could just do:

if [ ...condition... ];then
    ./myScript <$fileName
else
    ./myScript

The problem is that ./myScript is actually a long command line that I don't want to duplicate, nor do I want to create a function for it because it's not that long either (it's not worth it).

Then it occurred to me to do this:

[ ...condition... ] && input=$fileName || input=  #empty
cat $input | ./myScript

But that requires to run one more command and a pipe (i.e. a subshell).
Is there another way that's simpler and more efficient?

解决方案

First of all stdin is file descriptor 0 (zero) rather than 1 (which is stdout).

You can duplicate file descriptors or use filenames conditionally like this:

[[ some_condition ]] && exec 3<"$filename" || exec 3<&0

some_long_command_line <&3

Note that the command shown will execute the second exec if either the condition is false or the first exec fails. If you don't want a potential failure to do that then you should use an if / else:

if [[ some_condition ]]
then
    exec 3<"$filename"
else
    exec 3<&0
fi

but then subsequent redirections from file descriptor 3 will fail if the first redirection failed (after the condition was true).

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

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