Executes many times


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executes many times
# 1  
Old 06-20-2010
Executes many times

The code prints many times, "1st loop" and "2nd loop".
How can I come out the if the condition is matched.
Match should be only once and it should come out of the loop if the match is found

Code:
time=$(date +"%H:%M:%S")

echo $time
while :
do
if [ $(date +"%H:%M:%S") == '10:30:00' ]
then
echo "1st loop"
fi

if [ $(date +"%H:%M:%S") == '10:30:30' ]
then
echo "2nd loop"
fi
echo "Move to first step"
done

# 2  
Old 06-20-2010
Code:
while :
do
  jtime=$(date +"%H:%M:%S")
  if [ "$jtime" = '10:30:00' ]
  then
    echo "1st loop"
    break
  fi

  if [ "$jtime" = '10:30:30' ]
  then
    echo "2nd loop"
    break
  fi
  sleep 30
done

You are creating a new thread for every single progression of your question. Please stop doing this, otherwise you will get various answers in the various threads, and it'll all get rather confusing.
# 3  
Old 06-20-2010
You have to use a break command to stop the loop.
# 4  
Old 06-21-2010
Quote:
Originally Posted by Franklin52
You have to use a break command to stop the loop.
Code:
while :
do
  jtime=$(date +"%H:%M:%S")
  if [ "$jtime" = '10:30:00' ]
  then
    echo "1st loop"
    break
  fi

  if [ "$jtime" = '10:30:30' ]
  then
    echo "2nd loop"
    break
  fi
  sleep 30
done

When the if condition is true, then it should print "1st loop" and go to second if condtion. But the script now is completely stops executing after first if condtion is reached.

output:
1st loop
2nd loop
Again wait till next day 10:30:00
1st loop
2nd loop
and so on
# 5  
Old 06-21-2010
You must define a condition to break out the loop something like:
Code:
if [ .. ]
then
  break
fi

Have a read of this:

Ksh basics
# 6  
Old 06-21-2010
Code:
if [ "$jtime" = '10:30:00' ]
  then
    echo "1st loop"
    break
  fi

  if [ "$jtime" = '10:30:30' ]
  then
    echo "2nd loop"
    break
  fi

If I use break, then

if [ "$jtime" = '10:30:30' ]

is not executed. It completely comes out of the loop.
All the condition should be executed but only once.
output:
1st loop
2nd loop
Again wait till next day 10:30:00
1st loop
2nd loop
and so on
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash executes first part of script but not second

My bash below verifies the integrity of all .bam in a directory and writes the out output to a .txt file. That is part one of the script that works. The second part of the bash searches each one of the .txt files for a string "(SUCCESS)" and if found display a message and if it is not found... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Will shell script executes in sequence

I have a shell script scheduled in cron job to run at every 1 minute which transfers files to remote machine and then move the files to backup folder. cd /u01/app/ftp_tmp sftp user@hostname <<-EOF cd /home/user/ftp mput * bye EOF mv /u01/app/ftp_tmp/* /u01/app/ftp_bkp Now the problem is... (6 Replies)
Discussion started by: Bhavi
6 Replies

3. UNIX for Dummies Questions & Answers

Script partially executes??

Hi All, I am calling a shell script from another shell script, however, it only executes part of it (the echo commands only). What could be some causes for that? For example: ShellScriptA.sh: ... ... ... . ShellScriptB.sh ShellScriptB.sh contents: echo date echo... (7 Replies)
Discussion started by: DBnixUser
7 Replies

4. Shell Programming and Scripting

script executes some time but not always.

I have following script to send email when a USB is attached. #!/bin/bash NUMBER=`/bin/cat /u/numberoflines` LINES=`/usr/bin/wc -l < /var/log/messages` DIFFERENCE=$(($LINES-$NUMBER)) if ; then tail -n $DIFFERENCE /var/log/messages |while read line do if $( echo $line | grep --quiet... (2 Replies)
Discussion started by: kashif.live
2 Replies

5. AIX

what user executes init on boot?

I have a command in init that trys to start a daemon and open a log, but a get an error error that the file access permissions do not allow the specified action. My permissions on the log file are: -rw-r--r-- 1 root system 4434 Mar 22 15:13 dsmerror.log The logfile is written to... (2 Replies)
Discussion started by: pong3d
2 Replies

6. Programming

Problem with implementing the times() function in C (struct tms times return zero/negative values)

Hello, i'm trying to implement the times() function and i'm programming in C. I'm using the "struct tms" structure which consists of the fields: The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime structure... (1 Reply)
Discussion started by: g_p
1 Replies

7. Shell Programming and Scripting

FTP script - 'quit' never executes

I have wrote a script to get a very large encrypted file from a remote ftp server to a local debian box. The file downloads successfully, but the script never exits, or quits. None of code executes after the ftp get command. The file is approx 291M Here is the code: !/bin/sh... (3 Replies)
Discussion started by: jstrahm
3 Replies

8. Windows & DOS: Issues & Discussions

Batch script executes twice

Hi, Batch script gets executed without any error, but on execution some of the partial contents of the batch file gets appended at the end of the file which is currently in execution, hence the script tries to execute again for the second time , which should not happen. How to get it... (5 Replies)
Discussion started by: milink
5 Replies

9. Shell Programming and Scripting

root executes a script as another user

Hi All, Am using the below command to start my application using the root user su - bin -c "/home/bin/test/start.sh" but am getting the error becaue i have set some environment varibales in bin's .profile when i execute the command start.sh by logging directly into bin account it's... (9 Replies)
Discussion started by: ravi.sri24
9 Replies

10. AIX

how would you know your server was rebooted 3 times or 5 times

Is there such location or command to know how many times did you reboot your server in that particular day?in AIX. (3 Replies)
Discussion started by: kenshinhimura
3 Replies
Login or Register to Ask a Question