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 Closest I can get is this bash function: Code:
function multiq { i=$1; while [ $i -gt 0 ]; do $2; i=$(( $i - 1 )); done; } 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 I tried this bash function: Code:
function multix { i=$1; while [ $i -gt 0 ]; do $2 & ; i=$(( $i - 1 )); done; } How should I implement multi and multix? Are there programs that does similar thing? |
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. |
Code:
function multi { n=$1; shift; for ((i=0;i<n;i++)) do $@; done; } Code:
ssh -X username@hostname multibg 4 xterm -someoption |
Code:
function background { $@ &} |
Code:
$ function multibg { n=$1; shift; for ((i=0;i<n;i++)) do $@ &; done; } |
So long as your args are in a fixed place this would work:
Code:
for i in $(seq 1 $1) |
All times are GMT -5. The time now is 10:03 AM. |