Repeating commands in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Repeating commands in a script
# 1  
Old 03-14-2005
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  
Old 03-14-2005
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  
Old 03-14-2005
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  
Old 03-14-2005
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  
Old 03-14-2005
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 ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to rename the repeating strings

All, I have a sample text like below. Key (Header) Key1 ABC Key2 ABC Key3 ABC ABC Key4 ABC Key5 ABC ABC ABC Required Output Key (Header) Key1 (2 Replies)
Discussion started by: ks_reddy
2 Replies

2. Shell Programming and Scripting

Deleting Repeating lines from a txt file via script

Hi, I'm having trouble in achieving the following scenario. There is a txt file with thousands of lines and few lines are repeated, which needs to be removed using a script. File.txt 20140522121432,0,12,ram Loc=India From=ram@xxx.com, To=ravi@yyy.com,, 1 2 3 4 . . 30... (18 Replies)
Discussion started by: Gautham
18 Replies

3. Shell Programming and Scripting

Creating repeating record in between file through script

apprecieate your help to resove this. My source file looke like 1001 000 HEADER 1001 001 RAJESH 1001 002 100 1001 002 200 1001 002 500 1001 006 FOOTER 1002 000 HEADER 1002 001 RAMESH 1002 002 100 1002 002 200 1002 002 500 1002 006 FOOTER my... (8 Replies)
Discussion started by: Ganesh L
8 Replies

4. UNIX for Dummies Questions & Answers

Need help with repeating variables in a shell script

I should preface this by saying I have never worked with shell scripts before so this is all new to me. I was able to make something that worked, but is terribly optimized, and I have no idea how to improve it. If anything it's a pretty hilarious script: #/bin/bash get_char() { ... (4 Replies)
Discussion started by: ricco19
4 Replies

5. Shell Programming and Scripting

Shell script to extract data in repeating tags from xml

Hi, I am new to shell scripting. I need to extract data between repeating tags from an xml file and store the data in an array to process it further. <ns1:root xmlns:ns1="http://example.com/config"> <ns1:interface>in1</ns1:interface> <ns1:operation attribute1="true" attribute2="abd"... (2 Replies)
Discussion started by: sailendra
2 Replies

6. Shell Programming and Scripting

repeating ftp script question

so i got this request to do this: - Script should check for the file ever 15 minutes on the FTP server…if the file is not found, then the whole script exits. File will only be created one a week at random. i have gotten this far, but am kind of stuck, also sleep command doesnt work... (3 Replies)
Discussion started by: zapatur23
3 Replies

7. Shell Programming and Scripting

Repeating groups problem

Hi I am trying to locate all strings seperated by the string -CR-, the string will have an unknown number of these -CR- strings, I've used the regex: ^(?:(.*?)-CR-)+$ As shown in the test code: my ($myString)="This is the first line-CR-second line-CR-third line-CR-fourth... (3 Replies)
Discussion started by: joncoop
3 Replies

8. Shell Programming and Scripting

Repeating "vi" ex-editor 'command mode' commands

Hi, How to repeat the command which we typed and executed inside the "vi" editor 'command mode' (will be get by pressing "ESC" and ":" keys), since it cannot be repeated using the "." key? Because I'm typing the lengthy command at the command mode and do not know the way to repeat it often.... (0 Replies)
Discussion started by: royalibrahim
0 Replies

9. UNIX for Advanced & Expert Users

unix script for repeating a command with a variable

Hi need urgent help , for creating unix script . To collect system name,This is command i want to execute n (integer) no. of times for for a differnt IP addresses .IP is variable in every execution. Other string & collecter name is constant . snmpGet %IP% sysName.0 -c <string> -S <datacollecter... (2 Replies)
Discussion started by: langdatyagi
2 Replies

10. Shell Programming and Scripting

Repeating variables in the code

Hi all, I had written 3 KSH scripts for different functionalities. In all these 3 files there are some 30 variables in common. So I want to reduce the code by placing these variables in a common properties file named (dataload.prop/dataload.parms/dataload.txt) or txt file and access it... (1 Reply)
Discussion started by: mahalakshmi
1 Replies
Login or Register to Ask a Question