The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Repeating "vi" ex-editor 'command mode' commands royalibrahim Shell Programming and Scripting 0 09-25-2007 03:21 AM
unix script for repeating a command with a variable langdatyagi UNIX for Advanced & Expert Users 2 07-26-2007 08:55 AM
Repeating variables in the code mahalakshmi Shell Programming and Scripting 1 02-08-2007 07:33 AM
Filter out repeating messages antalexi SUN Solaris 2 01-31-2005 02:19 PM
repeating kernel message progressdll UNIX for Advanced & Expert Users 2 07-29-2002 04:18 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-14-2005
Dave2874 Dave2874 is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 3
Repeating commands in a script

I have written a script that I want to be repeated. The script that I wrote outputs the date, how many people are on the system, how many people logged in before me, and how many people logged in after me. Than what I want it to do is after it outputs the 4 lines I want it to go back to the beginning and start over. How would I go about doing this without using a loop. Is there a command in unix that does this? Thanks for your help.
  #2 (permalink)  
Old 03-14-2005
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2004
Location: USA
Posts: 511
Not sure , why are you trying to do this with out loop.

You have to run the script having 4 lines manually whenever you want to ,
with out the help of loops.
  #3 (permalink)  
Old 03-14-2005
Just Ice's Avatar
Just Ice Just Ice is offline Forum Advisor  
Lights on, brain off.
  
 

Join Date: Mar 2005
Location: in front of my computer
Posts: 637
if you really want to loop without a loop ...

make sure the commands you want to run are in a function in the script ... then at the end of the function ... add a line to call the function again ...

call the function ...

be forewarned that like regular loops, not putting in an escape clause or the proper conditional parameters in the function will put you into an indefinite loop which most likely is not what you want ...

Code:
#! /bin/ksh
# yourscript.ksh

not_a_loop_function(){
    date
    who | wc -l
    ...
    ...
    not_a_loop_function
}

not_a_loop_function
exit 0
or ... if you want to just use your script ... add a line towards the end of your script that calls your script again ... same caveats as above ... also might not be want you want as it might create too many subshells depending on how your script is written ... better off using the function method above ...

Code:
#! /bin/ksh
# yourscript.ksh

command
command
...
...

yourscript.ksh
exit 0
you might "really" want to put in some code to pause the non-loop --- i.e., sleep 5 --- as well as to break it based on some input to control it better ... (the code below will keep rechecking until you give it "n" or "q" when asked the recheck question at which point it will exit)

Code:
#! /bin/ksh
# yourscript.ksh

not_a_loop_function(){
    date
    who | wc -l
    ...
    ...
    echo "check again? (y,n,q) \c"
    read ans
    case $ans in
    n|q) echo "bye!"
         exit 0 ;;
    *)   not_a_loop_function ;;
    esac
}

not_a_loop_function
exit 0
  #4 (permalink)  
Old 03-14-2005
muthukumar muthukumar is offline
Registered User
  
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
Quote:
Originally Posted by Dave2874
I have written a script that I want to be repeated. The script that I wrote outputs the date, how many people are on the system, how many people logged in before me, and how many people logged in after me. Than what I want it to do is after it outputs the 4 lines I want it to go back to the beginning and start over. How would I go about doing this without using a loop. Is there a command in unix that does this? Thanks for your help.
You can do infinite loop of same commands as,

#way1 - with infinite while loop
#!/bin/sh
while true; do

date
who | wc -l
..
# Sleep for some time to make loop again
sleep 2
done
exit 0

# way2 - with cron
#!/bin/sh
date
who | wc -l
..

exit 0

# chmod 755 <script>
crontab -e
* * * * * <script with absolute path> 1>/dev/null 2>&1

so that script will be executed everyday on everyday hour..

HTH.
  #5 (permalink)  
Old 03-14-2005
Just Ice's Avatar
Just Ice Just Ice is offline Forum Advisor  
Lights on, brain off.
  
 

Join Date: Mar 2005
Location: in front of my computer
Posts: 637
Quote:
Originally Posted by muthukumar
crontab -e
* * * * * <script with absolute path> 1>/dev/null 2>&1

so that script will be executed everyday on everyday hour..

HTH.
actually you might want to do this ... or else it will be run every minute on every hour ...

Code:
0 * * * * <script with absolute path> 1>/dev/null 2>&1
btw, running the script through cron will send the output somewhere else other than the console so you might want to send the output to a file somewhere so you can read it ... otherwise you'll get emails from root regarding the output of your cron job ...
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0