LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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-19-2013, 09:11 AM   #1
123raajesh
LQ Newbie
 
Registered: Sep 2013
Posts: 12

Rep: Reputation: Disabled
Linux flow control exit or break on condition


Have query with following flow control. I need help run as per these conditions.

Conditions:
1 Successfull: Proceed to read message.
2 Fails: Exit to cat window or just exit without reading message.
3 Partialsuccess: Exit to cat window or just exit without reading message.

Code:
if grep -o INSTSUCCESS /tmp/log; then
  echo "installed SUCCESSFULLY, installing NEXT"
elif grep -o INSTFAILED /tmp/log; then
  echo "install FAILED exit further installation"; exit
elif grep -o INSTPARTIALSUCCESS /tmp/log; then
  echo "installed PARTIALLY, exit further installation"; exit
fi | tee -a /tmp/l; cat
read -p "Can verify installation in another session & then press [Enter] or Press [Enter] for NEXT Installation now"
The problem is what ever the condition is it cats window.

Thanks
 
Old 11-19-2013, 10:02 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Well I see a few issues:

1. Cause of your problem as you see it is due to the pipe causing a sub-shell, hence the exit is exiting from there and then continuing the rest of the code starting with the cat command

2. cat on its own would seem pointless as you will have to press ctrl-d or ctrl-c to exit from it

3. read -p, you prompt for a question and then do not store the output anywhere ... wouldn't seem to be much point for this line either??
 
Old 11-19-2013, 04:35 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,968
Blog Entries: 12

Rep: Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036
I'm not sure exactly what you're trying to do; however I think you're trying to do it all in too cute of manner.

Fundamentally write down the steps and rather than pipe off of the "fi"; consider that you can put the "tee" command within each "elif" statement.

Firstly, there is absolutely nothing wrong with punctuation, code separation, line feeds, and so forth.

grep returns a result code, which you're testing with the if-elif tests, sort of.

Maybe one way to do this, albeit lengthier would be:
Code:
grep -o INSTSUCCESS /tmp/log;
if [ $? == 0 ]; then
    echo "installed SUCCESSFULLY, installing NEXT"
else
    grep -o INSTFAILED /var/log;
    if [ $? == 0 ]; then
        echo "install FAILED exit further installation";
    fi
    grep -o INSTPARTIALSUCCESS /var/log;
    if [ $? == 0 ]; then
        echo "installed PARTIALLY, exit further installation";
    fi
    # not sure the semantics of the tee and cat, or if they apply;
    exit 1;
fi
My point there is that you have a single success case and two other cases of failure. However there are other cases of failure you maybe haven't considered; such as whether or not /var/log even exists. If the file isn't there, then the grep return will likely be error(2). But since you'd be testing solely for success(0) then you'll get the results you want. Partitioning it this way means:
  1. The SUCCESS case is by itself and can continue
  2. The TOTAL FAIL and PARTIAL FAIL cases are in the else clause and each can be handled uniquely but also commonly, as in where I've placed my comment about tee and cat and then placed the exit statement.
I just think rather than code these complicated lines; do you have to do it that way? I say code it fully drawn out and then if you choose to minimize either for efficiency or brevity, then make those changes. You may be just complicating yourself needlessly by trying to do too much within one line of statement.
 
Old 11-19-2013, 06:59 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
I agree with most of what was said by rtmistler, however, 'if' simply tests the return code of commands so whether it is the result of '[' or 'grep' makes no difference.
It would pay to understand what your requirement for the tee command is?
 
1 members found this post helpful.
  


Reply

Tags
grep forloop, linux command


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
condition to exit ofer4 Linux - Newbie 1 04-08-2012 06:34 PM
break out oa while loop once size condition is met casperdaghost Linux - Newbie 6 02-24-2012 02:11 AM
[SOLVED] Bash script to replace line break with condition spart1985 Linux - Newbie 8 02-23-2012 10:43 AM
LXer: C development on Linux - Flow Control - IV. LXer Syndicated Linux News 0 11-29-2011 01:41 PM
Cannot catch SIGINT while serial break condition occurs gzz Programming 1 09-17-2007 02:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:51 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