Search code examples
bashpipestdoutmonitormode

Problems with stdout


I'm designing a shell bash script. It has a lot of functions and one of them is to detect if a network card is in monitor mode. Not sure if there is a "pro" method to do that. My rudimentary method is working but has a problem. Is the next function:

function monitor() {

    WIFI="wlan0" #hardcoded wlan0 for the example
    mode=`iwconfig $WIFI|cut -d ' ' -f 6`

    if [[ $mode == "Mode:Monitor" ]]; then
        echo "Your interface $WIFI is in monitor mode already"
        return
    fi
    #Here is the rest of the function... not relevant
}

The problem is printing in screen the stdout of the command and I don't want anything printed in screen. So the first I thought is to redirect the stdout to /dev/null doing this:

mode=`iwconfig $WIFI|cut -d ' ' -f 6 > /dev/null 2>&1`

But if I do that, it stop to working... I think because it needs the stdout to pipe one command to other to work.

If I select an already monitor mode card everything is ok. The problem is if the network interface is not in monitor mode (eth0 for example), it prints this:

eth0 no wireless extensions.

What can I do to use the stdout for the pipe and prevent printing anything in screen?

Thank you in advance.

Cheers.


Solution

  • Silence stderr of your iwconfig command (using 2> /dev/null redirection):

    iwconfig $WIFI 2> /dev/null | cut -d ' ' -f 6