动态建立在bash命令 [英] Dynamically building a command in bash
问题描述
我在bash动态construcing的命令。这工作得很好:
I am construcing a command in bash dynamically. This works fine:
COMMAND="java myclass"
${COMMAND}
现在我要动态地构建redirectes输出的命令:
Now I want to dynamically construct a command that redirectes the output:
LOG=">> myfile.log 2>&1"
COMMAND="java myclass $LOG"
${COMMAND}
该命令还调用java程序,但输出不重定向到myfile.log
The command still invokes the java process, but the output is not redirected to myfile.log
此外,如果我做的:
BACKGROUND="&"
COMMAND="java myclass $BACKGROUND"
${COMMAND}
该命令在后台运行。
The command isn't run in the background.
如何让日志重定向和背景位工作任何线索? (bash的-x显示正在构建的命令如预期)
Any clues on how to get the log redirect, and background bits working? (bash -x shows the commands being constructed as expected)
(在现实中,不象本实施例中,LOG和背景的值被动态地设置)
(In reality, unlike this example, the values of LOG and BACKGROUND are set dynamically)
推荐答案
它不起作用,因为报价禁用的特殊含义>
和&安培;
。您必须执行该实施外壳这些功能的命令。
It doesn't work because quotes disable the special meaning of >
and &
. You must execute the commands which implement these features of the shell.
要重定向,呼叫 EXEC>> myfile.log 2 - ;&放大器; 1
要记录的命令之前
To redirect, call exec >> myfile.log 2>&1
before the command you want to log.
要在后台运行一个程序,使用的nohup
( CMD的nohup ARGS ...
)。
To run a program in the background, use nohup
(nohup cmd args...
).
这篇关于动态建立在bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!