willemdh
willemdh

Reputation: 856

How to get the result of a command in a HERE_doc in Bash

I'm using the at command to schedule a job in the future.

DoCurlAt () {
    if [ -n "${AuthToken:-}" ] ; then
        $4 << 'EOF'
curl -s -H "${AuthHeader:-}" -H "$1" --data-urlencode "$2" "$3"
EOF
        Exitcode=$?
    fi
    WriteLog Output Info "AT Output: $AtOutput Exitcode: $Exitcode"
}

How can I capture the result of the at in a variable called $AtOutput?

I tried with

AtOutput=$(bash $4 << EOF
curl -s -H "${AuthHeader:-}" -H "$1" --data-urlencode "$2" "$3"
EOF
)

But that does't really give any result.

Also tried with:

AtOutput=$(curl -s -H "${AuthHeader:-}" -H "$1" --data-urlencode "$2" "$3" | at "$4")

But I would prefer to use the HERE-doc. The function is called with

DoCurlAt "$AcceptJson" "argString=$ArgString" "$ApiUrl/$ApiVersion/job/$JobUid/run" "$OneTime"

$OneTime ($4) could be for example "at 15:19 today" The output is mostly something like this:

job 7 at 2016-08-16 15:30

Upvotes: 2

Views: 300

Answers (1)

chepner
chepner

Reputation: 530960

at writes to standard error, not standard output. Use the 2>&1 redirection to copy standard error to standard output first.

$ at_output=$( echo "cmd" | at "$when" 2>&1 )

Upvotes: 1

Related Questions