Question

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.

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:

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
Was it helpful?

Solution

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

~$ 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top