LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-26-2013, 07:41 AM   #1
123raajesh
LQ Newbie
 
Registered: Sep 2013
Posts: 12

Rep: Reputation: Disabled
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
 
Old 11-26-2013, 08:37 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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
 
1 members found this post helpful.
Old 11-26-2013, 10:28 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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
 
  


Reply

Tags
linux, scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux flow control exit or break on condition 123raajesh Linux - Newbie 3 11-19-2013 06:59 PM
LXer: C development on Linux - Flow Control - IV. LXer Syndicated Linux News 0 11-29-2011 01:41 PM
[SOLVED] exit in script files cause konsole terminal itself to exit Star_Gazer Programming 11 04-12-2010 09:32 AM
flow control rehan999 Linux - Networking 3 05-05-2008 03:58 AM
Serial flow control in 2.6.8 kidzmom3 Linux - General 1 12-08-2004 09:47 AM


All times are GMT -5. The time now is 09:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration