Bash script - if grep won't work either

Since this community seems really enjoyable, so I thought I asked a question -

I have this little script, but it will not just exit the dropdown list:

#!/bin/bash
echo -n "Enter ntp server address: "
read SERVER
if ntpdc -n -c monlist $SERVER | grep "timed out"
then 
  echo "Server won't let You use monlist."
  exit 0
else 
  echo "Server will let You use monlist."
fi

Any ideas? Thank:)

+3
source share
1 answer

In this case, the “problem” is that it ntpdcsends a message with a timeout to a standard error, and not a standard error, so it grepdoes not see it at the entrance to the pipe. You can eliminate the standard routing error of the command ntpdcto standard output:

if ntpdc -n -c monlist $SERVER 2>&1 | grep "timed out"
then 
  echo "Server won't let You use monlist."
  exit 0
else 
  echo "Server will let You use monlist."
fi
+5
source

All Articles