3

As this community seems to be really nice so I thought I'd ask a question -

I have this little script but it won't just grep the timed out from the output:

#!/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? Thanks :)

2
  • 3
    What is the output of ntpdc -n -c monlist $SERVER if you run it manually? Commented Feb 18, 2014 at 14:18
  • I think you should put brackets around your if arguments Commented Feb 18, 2014 at 14:21

1 Answer 1

5

In this case, the "problem" is that ntpdc sends the timeout message to standard error, not standard out, so grep doesn't see it on the input of the pipe. You can remedy by routing standard error of the ntpdc command to 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.