Search code examples
bashperlerror-handlingoutputbackticks

Perl - stop error appearing in command line output


What I am doing:

I have the following backticks command that executes in a simple foreach loop and saves the command output to a variable and I then perform string matching actions on that variable:

$ciphertestoutput = `echo -n | openssl s_client -cipher $tlsCipher -connect $ipaddress:443 2>/dev/null`;

The problem:

However, when I run my script in the output I get an error message that I cannot seem to stop appearing. I am not bothered that the error happens, but I do not want the error showing in the middle of the pretty command line output I have made.

My output and the error:

EXP-DES-CBC-SHA CIPHER IS SUPPORTED on 192.168.1.22:443

EXP-EDH-DSS-DES-CBC-SHA CIPHER IS NOT SUPPORTED on 192.168.1.22.443

EXP-RC2-CBC-MD5 CIPHER IS NOT SUPPORTED on 192.168.1.22:443
connect: Connection refused          <--- the error I cant get rid of
connect:errno=111                    <--- the error I cant get rid of

EXP-RC4-MD5 CIPHER IS NOT SUPPORTED on 192.168.1.22:443

What I have tried:

I have tried and experimented with all manner of ways I know of to suppress error messages in the output, but nothing I try prevents this error from appearing. I have done many similar things in the past and have never come across this issue with backticks. Is there something obvious I am missing here?


Solution

  • Try to reopen STDERR in your program like this:

    open STDERR, '>/dev/null';    
    # your command
    

    Errors of your qx-command will not be shown. So you don't have to worry about how will you call your program.

    P.S. Also you can save your STDERR if you need it:

    open OLDERR, ">&", \*STDERR; # or die "$!";
    open STDERR, ">/dev/null"; # or die "$!";
    # your command
    open STDERR, ">&OLDERR"; # restoring your stderr
    close OLDERR;
    # other code