Chris Brown
Chris Brown

Reputation: 377

How to redirect the output of a script to a file, but not the error?

I want to append the output of wpa_passphrase to a file if the parameters are valid, and leave the err msg to the screen otherwise.

I use

wpa_passphrase 1 111 2>&1 >>file
wpa_passphrase 1 111111111 2>&1 >>file

but the file still contains the msg and screen did not:

Passphrase must be 8..63 characters

thanks to you all

Upvotes: 2

Views: 278

Answers (1)

Alepac
Alepac

Reputation: 1831

The problem is that wpa_passphrase writes errors to stdout not to stderr. This code should solve the problem:

out=$(wpa_passphrase 1 1111111111) && echo "$out" >> file || echo "$out"

The code assigns the output to a variable and echos the variable to the file only if the preceding command is successful, otherwise it prints output to screen.

Upvotes: 4

Related Questions