在bash脚本中分配“at”命令的输出

时间:2021-03-16 13:56:15

I'm trying to capture the results of the "at" command inside a Bash script. The various ways of capturing command output don't seem to work, but I'm not sure if it's the pipe in the command or something else.

我试图在Bash脚本中捕获“at”命令的结果。捕获命令输出的各种方法似乎不起作用,但我不确定是命令中的管道还是其他什么东西。

echo $cmd | at $deployat

produces the output

生成的输出

job 42 at 2014-04-03 12:00

And I'm trying to get at the time the job was set for.

我正试图在工作准备好的时候完成。

However, I expected something like

然而,我期望的是类似的事情

v=$($cmd | at $deployat)
echo $v

Would work, or

会工作,或

v=$(echo $cmd | at $deployat)
echo $v

Or

v=`$cmd | at $deployat`
echo $v

But all of those leave the script hung, looking like it's waiting for some input.

但是所有这些都让脚本挂起了,看起来像是在等待输入。

What is the proper way to do this to end up with a variable like:

怎样做才能得到一个变量,比如:

2014-04-03 12:00

============================

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

Edit:

One possible complication is that the $cmd has flags with it:

一个可能的麻烦是,$cmd有它的标志:

ls -l

for example.

为例。

The expanded command could be something like:

扩展的命令可以是:

echo ls -l | at noon tomorrow

Solution:

v=$(echo $cmd | at $deployat 2>&1)
echo $v

1 个解决方案

#1


2  

at prints its output to stderr not stdout. Use 2>&1 to pipe the stderr of at into stdout. Example:

at打印输出到stderr而不是stdout。使用2>和1将at的stderr导入stdout。例子:

~$ out=$(echo cat Hello | at -v 2014-04-03 2>&1 | head -n 1)
~$ echo $out
Thu Apr 3 01:21:00 2014

With -v it prints the execution time on the first line which is taken by head -n 1.

用-v打印第一行的执行时间,第一行由head - n1获取。

#1


2  

at prints its output to stderr not stdout. Use 2>&1 to pipe the stderr of at into stdout. Example:

at打印输出到stderr而不是stdout。使用2>和1将at的stderr导入stdout。例子:

~$ out=$(echo cat Hello | at -v 2014-04-03 2>&1 | head -n 1)
~$ echo $out
Thu Apr 3 01:21:00 2014

With -v it prints the execution time on the first line which is taken by head -n 1.

用-v打印第一行的执行时间,第一行由head - n1获取。