0

In some cases what I see in a console output is different from what I get recorded after redirection. I see this on Linux/bash but this example is ksh/OpenBSD. Is there a way around this?

For example:

# pfctl -ttable -Ttest 123.123.123.123 > result.txt
0/1 addresses match.
# more result.txt
result.txt (END)

In other words the "0/1 addresses match." is printed on the console, but I cannot for the life of me get it into a file, variable or anything. I've used $() and > which work for most commands, but every now and then there is a command that spits out stuff on the screen but I get nothing via the redirect/pipe. I hope someone can shed light on this peculiarity.

So again contrast this:

# OUTP=$(pfctl -tscanners -Ttest 123.123.123.123)
0/1 addresses match.
# echo $OUTP

#

(nothing echoing, the variable does not hold the console output) with this:

# OUTP=$(date)
# echo $OUTP
Sun Aug 21 08:33:37 PDT 2016
#

(the variable contains the entire console output)

Thanks again for any help.

1

1 Answer 1

1

Your command has 2 different output streams.
You need to rederict the second (stderr) to the first.

pfctl -ttable -Ttest 123.123.123.123 > result.txt 2>&1
Sign up to request clarification or add additional context in comments.

4 Comments

Omg I had tried 2>&1 but had put that right after the command. Putting it at the very end works! For both cases. Thanks!
I get confused every time I use it. I try to remember it with : connect 2 with the target of 1. When you first connect stderr to the console, redirecting stdout is out of the picture. When you first connect stdout to a file, connecting 2 to the target is the same file.
Just for completeness sake, here is how to get the output using command substitution: OUTP=$(pfctl -tscanners -Ttest 1.1.1.1 2>&1)
@Laine: When you want to use $OUTP (or better ${outp} lowercase and braces), do not forget quotes: echo "${OUTP}" and you get newlines when they were in the original output.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.