nullByteMe
nullByteMe

Reputation: 6391

Unable to redirect stderr/stdout to /dev/null in script

In my script I basically need to wait for the network interface (e.g. en0, eth0, etc) on a linux machine to become fully initialized before my script should continue. I don't want any output while waiting, and I'm doing the wait like this:

#!/bin/bash

printf "\nWaiting for the network to start...\n"

ping_status=$(ping -c 1 server &>/dev/null | egrep -m 2 -e "[0-9]+")
i=0
while [ -z "$status" ]
do
   if [ "$i" -eq 30 ]
   then
      printf "\nServer unresponsive for 30 seconds.  Quitting script\n"
      exit 1
   else
      sleep 1
      i=$((i+1))
      ping_status=$(ping -c 1 server &>/dev/null | egrep -m 2 -e "[0-9]+")
   fi
done

The problem is I get the following output until my network is initalized:

connect: Network is unreachable
connect: Network is unreachable

The command ping -c 1 server &>/dev/null | egrep -m 2 -e "[0-9]+" correctly parses the second line of a successful ping and tells me the bytes, so I know the network isn't up if the return is null.

For some reason, though, I get an actual egrep file in my current directory and I still get the above output connect: Network is unreachable

Upvotes: 0

Views: 2171

Answers (2)

mohit
mohit

Reputation: 6064

&> operator is functional in all bash starting from version 4. You might want to check this link (see in the examples).

That said, since you only want to redirect error, I suggest you should use:

ping -c 1 server 2> /dev/null

That's because output of ping command is already stored in ping_status.

Upvotes: 1

Antarus
Antarus

Reputation: 1621

ping -c 1 127.0.0.1 | awk 'NR==2{print $1}'

Use this result. this will solve the problem

Upvotes: 1

Related Questions