LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-16-2008, 05:56 AM   #1
snr8fl3
LQ Newbie
 
Registered: Apr 2008
Posts: 7

Rep: Reputation: 0
script to execute a command multiple times


I wish to have a command that executes a specified command a specified number of times.

For example,
Code:
multi 3 echo abc
should execute 'echo abc' three times.

Closest I can get is this bash function:
Code:
function multiq { i=$1; while [ $i -gt 0 ]; do $2; i=$(( $i - 1 )); done; }
,which is used like :
Code:
multiq 3 'echo abc'
.

I also want to have a command that opens a specified x application a specified number of times. For example,
Code:
multix 3 xterm python
should open three xterm windows with python as shell, that is same as executing 'xterm python &' three times.

I tried this bash function:
Code:
function multix { i=$1; while [ $i -gt 0 ]; do $2 & ; i=$(( $i - 1 )); done; }
, but this results in bash syntax error.

How should I implement multi and multix? Are there programs that does similar thing?
 
Old 11-16-2008, 06:14 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
this looks a *lot* like homework as these functions seem completely arbitrary and straightforward, but if they are you've re-written it pretty well... Either way, please don't ask homework questions

a couple of pointers though. while loops are ok, but not really what you want. Try a for loop instead, and also read up on the "shift" command.
 
Old 11-16-2008, 01:00 PM   #3
snr8fl3
LQ Newbie
 
Registered: Apr 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Code:
function multi { n=$1; shift; for ((i=0;i<n;i++)) do $@; done; }
function background { $@ &}
function multibg { n=$1; shift; for ((i=0;i<n;i++)) do background $@; done; }
This is not really arbitrary (not homework). One can open several remote shells quickly using such commands.

Code:
ssh -X username@hostname multibg 4 xterm -someoption
 
Old 11-16-2008, 01:12 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
Code:
function background { $@ &}
Why use one character for put something into the background when 10 will do?! Very useful.

Last edited by acid_kewpie; 11-16-2008 at 01:14 PM.
 
Old 11-17-2008, 12:11 AM   #5
snr8fl3
LQ Newbie
 
Registered: Apr 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Code:
$ function multibg { n=$1; shift; for ((i=0;i<n;i++)) do $@ &; done; }
The obvious approach like this doesn't work. Not familiar with bash programming.

Last edited by snr8fl3; 11-17-2008 at 12:15 AM.
 
Old 11-17-2008, 12:49 PM   #6
The_Kernel
LQ Newbie
 
Registered: Nov 2008
Posts: 19

Rep: Reputation: 0
So long as your args are in a fixed place this would work:

Code:
for i in $(seq 1 $1)
do
  $( sed "s:^$1 ::" <<< $@ ) &
done
Just remove the trailing '&' for the "multiq" version
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to execute Source command in a shell script UltraSoul Linux - General 3 09-23-2010 10:13 AM
perl script system() only execute one command knockout_artist Programming 4 11-11-2008 10:57 AM
Command would not execute within a script pramka Suse/Novell 2 07-16-2008 10:11 AM
How to execute a command line in a shell script orgazmo Programming 5 05-09-2005 09:16 AM
Tar command copied multiple times? cbriscoejr Linux - General 1 11-12-2004 02:09 PM


All times are GMT -5. The time now is 03:23 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration