DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 10th March 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Thanked 0 Times in 0 Posts
Default repeat same command n times

hello,
how do I let unix repeat the same command n times, without having to do it manually?
I need this for latex, which gets my page formatting properly only after running it quite e few times.
tks
Reply With Quote
  #2   (View Single Post)  
Old 10th March 2009
DutchDaemon's Avatar
DutchDaemon DutchDaemon is offline
Real Name: Ben
Spam Refugee
 
Join Date: Jul 2008
Location: Rotterdam, The Netherlands
Posts: 337
Thanked 32 Times in 30 Posts
Default

Code:
num=0; while [ $num -le 10 ]; do some_command; num=$(expr $num + 1); sleep 2; done
Will run some_command ten times with 2 second intervals.

In a script named 'run':

Code:
num=0; while [ $num -le $1 ]; do $2; num=$(expr $num + 1); sleep 2; done
Call as 'run 10 ls' or 'run 3 "ps ax"' (commands containing a space must be quoted to be seen as one argument).

Mind you, if it's always the exact same command you have to run five times, you can just put the command in a shell script 5 times and run the shell script

Last edited by DutchDaemon; 10th March 2009 at 03:33 PM.
Reply With Quote
  #3   (View Single Post)  
Old 10th March 2009
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is online now
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,051
Thanked 198 Times in 156 Posts
Default

You can use a shell script, something like:

Code:
#!/bin/sh

for i in $(jot 10); do
  echo "Running command for the ${i}nth time"
done
This will run 10 times.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #4   (View Single Post)  
Old 10th March 2009
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is online now
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,051
Thanked 198 Times in 156 Posts
Default

We Dutch people are fast!

DutchDaemon's solution will work too, personally I think `my' solution is cleaner, but I guess that's a matter of personal preference.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #5   (View Single Post)  
Old 10th March 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Thanked 0 Times in 0 Posts
Default

Cool!
They all work perfectly. I also thought I could simply write the same command in a script many times, but it is a good way to learn something new.
Thanks a lot.
Reply With Quote
  #6   (View Single Post)  
Old 10th March 2009
DutchDaemon's Avatar
DutchDaemon DutchDaemon is offline
Real Name: Ben
Spam Refugee
 
Join Date: Jul 2008
Location: Rotterdam, The Netherlands
Posts: 337
Thanked 32 Times in 30 Posts
Default

Yeah, I have a blind spot for jot, I guess. I always forget it exists when I need enumerations
Anyway, another fine example of 'there's always more than one way with Unix'.
Reply With Quote
  #7   (View Single Post)  
Old 10th March 2009
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

And here's another one :P

perl -e 'print "date;sleep 1;"x10' | sh

Last edited by ephemera; 10th March 2009 at 09:14 PM.
Reply With Quote
  #8   (View Single Post)  
Old 10th March 2009
DutchDaemon's Avatar
DutchDaemon DutchDaemon is offline
Real Name: Ben
Spam Refugee
 
Join Date: Jul 2008
Location: Rotterdam, The Netherlands
Posts: 337
Thanked 32 Times in 30 Posts
Default

Now you're just showing off.

Reply With Quote
  #9   (View Single Post)  
Old 12th March 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

Quote:
Originally Posted by gosha View Post
Cool!
I also thought I could simply write the same command in a script many times, but it is a good way to learn something new.
Thanks a lot.

generally the thought "repeat" and the word "loop" are synonymous, inclusive of the old "I don't want to type this thing X amount of times" factor ;-)
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Reply

Thread Tools
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Last message repeated x times delboy FreeBSD General 6 25th June 2009 10:08 AM
run same command many times with diff args gosha Programming 21 30th May 2009 11:51 AM
How to modify the ls command? bsdnewbie999 OpenBSD General 9 16th May 2009 08:20 AM
Thanked X times in X posts johnlvs2run Feedback and Suggestions 6 18th May 2008 01:21 PM
Daemonforums very slow, times out a lot Weaseal Feedback and Suggestions 15 16th May 2008 12:44 AM


All times are GMT. The time now is 07:19 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick