Nested if else


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested if else
# 1  
Old 01-10-2013
Nested if else

Hi,

i m trying to create script which logic is like below.

Code:
if [ -s ${LOGFILE} ]; then
x=`cat /tmp/testoutput.log | grep STOP | wc -l`
y=`cat /tmp/testoutput.log | grep RUN | wc -l`
if [ "$x" -gt 0 ]; then
echo "process stop"
if [ "$y" -gt 0 ]; then
echo "process running "
else
echo "file not found"
fi

----------------
This syntax does not work , could somebody suggest anyway to do it.

Thanks.

Last edited by Scrutinizer; 01-10-2013 at 07:32 PM.. Reason: code tags
# 2  
Old 01-10-2013
That is Useless Use of Cat and Useless Use of wc -l

Try this code:
Code:
if [ -f ${LOGFILE} ]; then
        x=$( grep -c STOP /tmp/testoutput.log )
        y=$( grep -c RUN  /tmp/testoutput.log )
        if [ $x -gt 0 ]; then
                echo "process stop"
        fi
        if [ $y -gt 0 ]; then
                echo "process running "
        fi
else
        echo "file not found"
fi

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-10-2013
Further optimization:

Code:
if grep -q STOP /tmp/testoutput.log; then
  echo "process stop"
fi

Code:
if grep -q START /tmp/testoutput.log; then
  echo "process start"
fi

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

nested if else -error

HI everyone, I am not able to find error in the script, when i run the script till line No. 20 i.e, read var4 everything runs fine. After that the script exits out. #!/bin/bash echo -e "Want dryrun OR merge: \n " read var1 if ] ; then echo -e "\n Please select from the given... (10 Replies)
Discussion started by: rishi.aradhya
10 Replies

2. Shell Programming and Scripting

Nested if loop

Hi Team, I just want to check whether my nested if loop used is correct or not. if ] if ] export1 else export2 fi else if ] export3 else export4 fi fi Thanks Shiva (5 Replies)
Discussion started by: shivashankar_S
5 Replies

3. Shell Programming and Scripting

Optimize the nested IF

Hi, I have to assign a value for a varaiable based on a Input. I have written the below code: if then nf=65 elif then nf=46 elif then nf=164 elif then nf=545 elif then nf=56 elif then (3 Replies)
Discussion started by: machomaddy
3 Replies

4. Shell Programming and Scripting

Nested case

Hi there, I have nested case in my script. I am asking user, want to continue? if user press y/Y then my inner case should continue, rather than that my code start from beginning. I would like to continue my inner case until user press n or N. Is any one tell me how can I do? Thanking You,... (2 Replies)
Discussion started by: kasparov
2 Replies

5. Shell Programming and Scripting

Nested if in KSH

Trying to clean up the last little thing in this script which is that the mv always performs no matter whether it is needed or not. It doesn't cause any bugs just prints an unsightly "can't mv source and dest are the same". Obviously I want to get rid of it but having issues getting the... (4 Replies)
Discussion started by: Calbrenar
4 Replies

6. Shell Programming and Scripting

Nested for loops

Greetings All, The following script attempts to enumerate all users in all groups in the group file(GROUP) and echo the following information: GROUP ---> USER The script is as follows: IFS="," for GROUP in `ypcat -k group | cut -d" " -f1` do for USER in `ypcat -k group... (13 Replies)
Discussion started by: jacksolm
13 Replies

7. Shell Programming and Scripting

nested for loops

I need help getting over this bump on how nested for loops work in shell. Say i was comparing files in a directory in any other language my for loop would look like so for(int i=0;to then end; i++) for(int y = i+1; to the end; y++) I can't seem to understand how i can translate that... (5 Replies)
Discussion started by: taiL
5 Replies

8. Shell Programming and Scripting

nested variables

Is there any way to do variable nesting using sh? For example: example_1="a test string" example_2="another test" example_3="etc..." i=2 echo ${example_$i} The shell reports: sh: ${example_$i}: bad substitution If not, maybe someone could suggest another method. Thanks in... (3 Replies)
Discussion started by: kevinl33
3 Replies
Login or Register to Ask a Question