如何将重定向符号分配给用于组装命令行的变量 [英] How to assign redirection symbol to a variable for assembling a command line

查看:43
本文介绍了如何将重定向符号分配给用于组装命令行的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个BASH脚本,这些命令会启动程序,这些程序通常与常用参数一起使用,这些常用参数使用命令和参数的变量.调用将显示在末尾,如下所示:

I have a couple of BASH scripts that launch programs I commonly use with common arguments using variables for the command and arguments. The invocation appears at the end like so:

$PROGRAM $ARG1 $ARG2 &

现在,我想默认将输出重定向到stderr,将stdout重定向到/dev/null .我希望能够通过切换到脚本来禁用它( -v = verbose).但是,如果我尝试将"2>& 1>/dev/null" 分配给var,请说 REDIRECTION (如果指定了详细信息,则将其清除),然后尝试像这样调用:

Now I want to redirect output to stderr and stdout to /dev/null by default. I want to be able to disable that with a switch to the script (-v = verbose). But if I try to assign "2>&1 > /dev/null" to a var, say REDIRECTION, (clearing it if verbose is specified) and try to invoke like so:

$PROGRAM $ARG1 $ARG2 $REDIRECTION &

重定向指令作为参数传递给程序.有没有办法做到这一点?还是我必须使用我的解决方案,即有2条单独的调用行,其中一条带有重定向"指令,另一条没有重定向"指令,这取决于详细程度"?

The redirection directives are passed as arguments to the program. Is there a way to do this? Or do I have to use my solution which is to have 2 separate invocation lines, one with and one without the redirection directives, depending on "verbosity"?

推荐答案

一个选项是使用 eval 内置函数,该函数像Bash命令一样处理其自变量,处理诸如重定向运算符之类的事情.但是, eval 很有风险,因为它将重新处理其所有的参数,即使是已经正确处理的参数,如果不执行,也会导致异常或不安全的行为.完美.

One option is to use the eval builtin, which processes its arguments like a Bash command, handling things like redirection operators. However, eval is pretty risky, since it will reprocess all of its arguments, even ones that have already been processed properly, and this can cause bizarre or unsafe behavior if not done perfectly.

由于命令的两个版本之间唯一的问题区别是是否存在重定向,因此更好的选择是使用内置的 exec .此命令:

Since the only problematic difference between the two versions of your command is the presence or absence of redirections, a better option is to use the exec builtin. This command:

exec >/dev/null 2>&1

将外壳程序脚本其余部分的STDOUT和STDERR重定向到/dev/null .此命令:

redirects the STDOUT and STDERR of the remainder of a shell-script to /dev/null. This command:

[[ "$IS_VERBOSE" ]] || exec >/dev/null 2>&1

将运行 exec>/dev/null 2>& 1 ,除非 $ IS_VERBOSE 是非空白的,在这种情况下什么也没做.(您可以将 [[" $ IS_VERBOSE]] 替换为您已经用来检测详细程度的任何条件表达式.)

will run exec >/dev/null 2>&1 unless $IS_VERBOSE is non-blank, in which case it does nothing. (You can replace [[ "$IS_VERBOSE" ]] with whatever sort of conditional expression you're already using to detect verbosity.)

所以,您想要的是这样的东西:

So, what you'd want is something like this:

(
    [[ "$IS_VERBOSE" ]] || exec >/dev/null 2>&1
    "$PROGRAM" "$ARG1" "$ARG2" &
)

括号(...)用于设置子外壳,因此 exec 命令(如果运行)仅会影响到).

The parentheses ( ... ) are to set up a subshell, so the exec command (if run) only affects up until the ).

(顺便说一句,我将您的 2>& 1>/dev/null 更改为>/dev/null 2>& 1 :顺序很重要.)

(By the way, note that I changed your 2>&1 > /dev/null to > /dev/null 2>&1: the order matters.)

这篇关于如何将重定向符号分配给用于组装命令行的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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