2

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
1
  • The command out=$(echo ls -l | at -v 2014-04-03 2>&1 | head -n 1) works fine for me... Commented Apr 2, 2014 at 23:33

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I get the following error when I try this: ./deploy: line 121: =job: command not found
How does your deploy file look like? (The lines around line number 121)
I accidentally wrote $v=... in your solution, when changed to "v=..." it works. Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.