Else Loop Exiting Early | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Else Loop Exiting Early

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-20-2008
GregWold GregWold is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 20 June 2008, 11:58 AM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Question Else Loop Exiting Early

All,

I'm having a problem w/this function. Specifically, I want to call another function (get_stats) when the process in the else completes (the initial if and the elsif seem to work fine). But what's happening is the get_stats function call is running after the else runs only once, NOT when it completes. I need the else condtion to run until the process completes, THEN call get_stats. Thanks in advance!

check_ul_process()
{
PROGRAM=8675309
LONG_SLEEP=5
SLEEP_TIME=1
UL_PROCESS=$(ps -eaf | grep $PROGRAM | grep -v grep | wc -l)

# if 8675309 isn't running
# and it's not being run by cron,
# re-check

if [ $UL_PROCESS -eq 0 ]
then
while [ $LONG_SLEEP -gt 0 ]
do
if test $STARTED_BY_CRON = "False"
then
echo "\rTime to next check - $LONG_SLEEP\c"
fi
let LONG_SLEEP=LONG_SLEEP-1
sleep $SLEEP_TIME
done

LONG_SLEEP=5
check_ul_process
elsif [ $UL_PROCESS -eq 0 -a $UL_FILE_TYPE = TEMP ]

# if a ul_temp file exists
# and 8675309 is not running,
# log error and exit.

echo $(expr substr $(hostname) 5 2 | tr 'a-z' 'A-Z') "::" $(date +'%D %T') ":: Exiting. A .ul_temp file exists, but the upload process isn't running." >> /ops/log/ulgen_report.log #~/test_report.log
exit
else

# if 8675309 is running
# and it's not being run by cron,
# check it until it completes

while [ $LONG_SLEEP -gt 0 ]
do
if test $STARTED_BY_CRON = "False"
then
echo "\rTime to next check - $LONG_SLEEP\c"
fi
let LONG_SLEEP=LONG_SLEEP-1
sleep $SLEEP_TIME
done

LONG_SLEEP=5
get_stats
exit
fi
}
Sponsored Links
    #2  
Old 03-20-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 6 October 2014, 11:01 AM EDT
Location: Out running a Marathon.
Posts: 2,332
Thanks: 73
Thanked 155 Times in 144 Posts
Question Can you edit/re-submit with code tags?

Being able to see the script indented makes if - then - else and other stuctures easier to read and follow.
Sponsored Links
    #3  
Old 03-20-2008
GregWold GregWold is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 20 June 2008, 11:58 AM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
RE: Else Loop Exiting Early

Sure (I didn't realize 1) that you could use the code tag and 2) that when posting, it would remove my formatting. Sorry!


Code:
check_ul_process()
{
   PROGRAM=8675309
   LONG_SLEEP=5
   SLEEP_TIME=1
   UL_PROCESS=$(ps -eaf | grep $PROGRAM | grep -v grep | wc -l)

   # if 8675309 isn't running
   # and it's not being run by cron,
   # re-check

   if [ $UL_PROCESS -eq 0 ]
   then
      while [ $LONG_SLEEP -gt 0 ]
      do
         if test $STARTED_BY_CRON = "False"
         then
            echo "\rTime to next check - $LONG_SLEEP\c"
         fi
         let LONG_SLEEP=LONG_SLEEP-1
         sleep $SLEEP_TIME
      done

      LONG_SLEEP=5
      check_ul_process

   elsif [ $UL_PROCESS -eq 0 -a $UL_FILE_TYPE = TEMP ]

   # if a ul_temp file exists
   # and 8675309 is not running,
   # log error and exit.

      echo $(expr substr $(hostname) 5 2 | tr 'a-z' 'A-Z') "::" $(date +'%D %T') ":: Exiting. A .ul_temp file exists, but the upload process isn't running." >> /ops/log/ulgen_report.log #~/test_report.log
      exit

   else

   # if 8675309 is running
   # and it's not being run by cron,
   # check it until it completes

      while [ $LONG_SLEEP -gt 0 ]
      do
         if test $STARTED_BY_CRON = "False"
         then
            echo "\rTime to next check - $LONG_SLEEP\c"
         fi
         let LONG_SLEEP=LONG_SLEEP-1
         sleep $SLEEP_TIME
      done

      LONG_SLEEP=5
      get_stats
      exit
   fi
}

    #4  
Old 03-20-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 6 October 2014, 11:01 AM EDT
Location: Out running a Marathon.
Posts: 2,332
Thanks: 73
Thanked 155 Times in 144 Posts
Question A couple of things I noticed


Code:
      check_ul_process

   elsif [ $UL_PROCESS -eq 0 -a $UL_FILE_TYPE = TEMP ]

(1) Your first if condition calls itself - you are nesting a restart of the program.
(2) I do not think this 'elsif' can execute since the simpler first part [$UL_PROCESS -eq 0] would always be caught above.
(3) I cannot say for sure, but your "if then elsif else fi" does not quite feel right. Perhaps it requires "elif then" to make computer logic correct.
Sponsored Links
    #5  
Old 03-20-2008
GregWold GregWold is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 20 June 2008, 11:58 AM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
RE: Else Loop Exiting Early

Joey,

1) Correct. I want it to run until 8675309 starts. Then on the next check it should go to else.

2) Should I change it to
if [ $UL_FILE_TYPE = TEMP -a $UL_PROCESS -eq 0 ]
That is check the file type first and then check for the process?

3) elif is correct; my misspelling. Thanks for the catch!
Sponsored Links
    #6  
Old 03-20-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 6 October 2014, 11:01 AM EDT
Location: Out running a Marathon.
Posts: 2,332
Thanks: 73
Thanked 155 Times in 144 Posts
Question Clarifications..

1) Correct. I want it to run until 8675309 starts. Then on the next check it should go to else.

2) Should I change it to
if [ $UL_FILE_TYPE = TEMP -a $UL_PROCESS -eq 0 ]
That is check the file type first and then check for the process?

3) elif is correct; my misspelling. Thanks for the catch!

In response:
1) I think this is odd as you will have two occurrences of the program then running. You run, catch the condition, wait, and start again "as a sub-process". This 2nd run may have different results at "if" statements. Assuming it too does not get caught by the first loop - thus creating a third instance of the script running - control would be returned to the first run at the "elsif" line. Perhaps rethink the logic to a "do while" or "do until" set of commands?
2) I was not commenting on the order within the if, more the logic that I do not believe the program could ever find your "elsif" logic true and able to be executed. The first if is true (UL -eq 0) meaning the "elsif" would not be exexcuted. The first is false (UL -eq 0) meaning the "elsif" would be analyzed, but how could it be now true? Unless this is all to catch the sub-process I referred to in (1)?
3) Concur that "elsif" should be "elif". My other point is that an "elif" should then have its own "then". "elif" expects a "then", so does the program skip a bunch of logic until it finds your next "then" occurrence?
Sponsored Links
    #7  
Old 03-20-2008
GregWold GregWold is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 20 June 2008, 11:58 AM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Program Flow/Logic Question

Sorry, I'm new to this and am surprised I've made it this far.

Can I do a


Code:
UL_PROCESS=$(ps -eaf | grep $PROGRAM | grep -v grep | wc -l)

while [ $UL_PROCESS -ne 0 ]
do
...
done

or should it be


Code:
while [ `ps -eaf | grep $PROGRAM | grep -v grep | wc -l` -ne 0]
do
...
done

?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Loop Forever Script Strangely Exiting marvinwright Shell Programming and Scripting 3 07-26-2010 06:07 PM
Problem in exiting a loop anijan Shell Programming and Scripting 3 03-23-2010 09:57 AM
"while read ..." loop exiting after reading only one record twk UNIX for Advanced & Expert Users 2 02-03-2010 06:30 AM
exiting from a loop hcclnoodles Shell Programming and Scripting 13 11-26-2008 07:36 PM
Bash: Exiting while true loop when terminal is not the focus window acclaypool Shell Programming and Scripting 2 02-25-2008 05:02 PM



All times are GMT -4. The time now is 10:09 PM.