LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   linux flow control exit to terminal on failure (https://www.linuxquestions.org/questions/linux-newbie-8/linux-flow-control-exit-to-terminal-on-failure-4175485912/)

123raajesh 11-26-2013 06:41 AM

linux flow control exit to terminal on failure
 
I have a query with flow control. I want to exit the script to terminal when message is " HOSTNAME NOT ADDED IN /tmp/hosts EXITING", which is failure message.

Code:

if grep -o 'abcdefgh01.was.db.dcbc' /tmp/hosts; then
  echo " HOSTNAME ADDED IN /tmp/hosts"
elif [ $?==1 ]; then
  echo " HOSTNAME NOT ADDED IN /tmp/hosts EXITING"; exit
fi|tee -a /tmp/log;

I tried above one but fails to exit on failure. Please suggest

Thanks

druuna 11-26-2013 07:37 AM

This won't work and is the reason why it doesn't exit properly:
Code:

if
.
.
fi | tee -a /tmp/log;

Try this instead (I also changed some other parts of your code):
Code:

if grep -q 'abcdefgh01.was.db.dcbc' /tmp/hosts
then
  echo " HOSTNAME ADDED IN /tmp/hosts" | tee -a /tmp/log
else
  echo " HOSTNAME not ADDED IN /tmp/hosts EXITING" | tee -a /tmp/log
  exit 1
fi

Here's another way of doing it:
Code:

( if grep -q 'abcdefgh01.was.db.dcbc' /tmp/hosts
then
  echo " HOSTNAME ADDED IN /tmp/hosts"
else
  echo " HOSTNAME not ADDED IN /tmp/hosts EXITING"
  exit 1
fi ) | tee -a /tmp/log


grail 11-26-2013 09:28 AM

I am curious on just how many times the OP is going to ask the same question??

october - http://www.linuxquestions.org/questi...ng-4175482373/

november - http://www.linuxquestions.org/questi...on-4175485133/

And now it would appear that we couldn't wait for december to roll around?

You do know that if you keep doing the same things and expecting the outcome to change that this would be considered a sign of insanity


All times are GMT -5. The time now is 08:02 PM.