how-to: one command for repeat OR iterate shell OR bash command delay OR interval
Is there already one bash command to do what the following script does (poorly or incompletely), ie repeat "command" indefinitely every "x" seconds:
#!/bin/sh
# usage: repeat [x] <command>
while true ; do $2 ; sleep $1 ; done
if not, and you want to use the above in a script of your own, just copy the above 3 lines and do the following (typing the lines into the console or pasting them in shell, using konsole under kde or gpm under a tty shell; hit <Ctrl-c> to stop it from looping). The two examples are optional. The stuff in angle brackets, <>, is stuff you won't see in your shell output:
am [at] [bin]$ cat >repeat
#!/bin/sh
# usage: repeat [x] <command>
while true ; do $2 ; sleep $1 ; done
am [at] [bin]$ chmod +x repeat
<examples>
am [at] [bin]$ ./repeat 2 "cat /proc/loadavg"
0.46 0.92 1.72 9/125 24171
0.46 0.92 1.72 2/125 24173
0.59 0.94 1.72 1/125 24181
<Ctrl-c>
am [at] [bin]$ cd ..
am [at] [~]$ repeat 2 uname
Linux
Linux
<Ctrl-c>
btw: i've already posted the same to:
http://www.linuxquestions.org/questions/showthread.php?s=&th readid=343892
some answers might show up there before i get back
--
AD Marshall
Tel: +84 (0)903871313
eM: admarshall [at] gmail.com
Web: http://h0lug.sourceforge.net
Zone: ICT (IndoChina Time; GMT/UTC+7)
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo [at] vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: how-to: one command for repeat OR iterate shell OR bash commanddelay OR interval
Hi,
On Sun, 17 Jul 2005 11:57:34 +0700
AD Marshall <admarshall [at] gmail.com> wrote:
> Is there already one bash command to do what the following script
> does (poorly or incompletely), ie repeat "command" indefinitely
> every "x" seconds:
>
> #!/bin/sh
> # usage: repeat [x] <command>
> while true ; do $2 ; sleep $1 ; done
>
> < rest deleted >
The watch command might tbe what you want.
Or maybe not, if you don't want anything output to screen.
Try man watch for details.
regards,
John Kelly
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo [at] vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: how-to: one command for repeat OR iterate shell OR bash command delay OR interval
On Sunday 17 July 2005 12:35, John Kelly wrote:
> Hi,
> On Sun, 17 Jul 2005 11:57:34 +0700
> AD Marshall <admarshall [at] gmail.com> wrote:
>
> > Is there already one bash command to do what the following script
> > does (poorly or incompletely), ie repeat "command" indefinitely
> > every "x" seconds:
> >
> > #!/bin/sh
> > # usage: repeat [x] <command>
> > while true ; do $2 ; sleep $1 ; done
> >
> > < rest deleted >
>
> The watch command might tbe what you want.
> Or maybe not, if you don't want anything output to screen.
>
> Try man watch for details.
>
<cut>
Ya. Thanks. I'd (long) forgotten about "watch".
but, actually, i should be more specific. what i'm trying to do is
something like this -- though i'm screwing up on quoting or something
am [at] [~]$ repeat 2 "echo $(cat /proc/loadavg ; date +%H:%m:%S)"
0.05 0.14 0.24 6/127 27711 12:07:29
0.05 0.14 0.24 6/127 27711 12:07:29
0.05 0.14 0.24 6/127 27711 12:07:29
as you can see, only one instance of load average and time are repeated.
i want a running record that can be redirected to a file
i just tinkered with backslashes, back-quotes, double-quotes and
single-quotes, but all the quoting stuff still confuses me.
and, imho, i would have thought someone would have written a simple
tool to do this ages ago. no?
thanx again,
andi
--
AD Marshall
Tel: +84 (0)903871313
eM: admarshall [at] gmail.com
Web: http://h0lug.sourceforge.net
Zone: ICT (IndoChina Time; GMT/UTC+7)
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo [at] vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: how-to: one command for repeat OR iterate shell OR bash command delay OR interval
my apologies, if necessary. but it's way past lunchtime in saigon and
i'm STARVING. i'll check back later. thanx again. - best, andi
On Sunday 17 July 2005 13:07, AD Marshall wrote:
> On Sunday 17 July 2005 12:35, John Kelly wrote:
> > Hi,
> > On Sun, 17 Jul 2005 11:57:34 +0700
> > AD Marshall <admarshall [at] gmail.com> wrote:
> >
> > > Is there already one bash command to do what the following script
> > > does (poorly or incompletely), ie repeat "command" indefinitely
> > > every "x" seconds:
> > >
> > > #!/bin/sh
> > > # usage: repeat [x] <command>
> > > while true ; do $2 ; sleep $1 ; done
> > >
> > > < rest deleted >
> >
> > The watch command might tbe what you want.
> > Or maybe not, if you don't want anything output to screen.
> >
> > Try man watch for details.
> >
> <cut>
> Ya. Thanks. I'd (long) forgotten about "watch".
>
> but, actually, i should be more specific. what i'm trying to do is
> something like this -- though i'm screwing up on quoting or something
>
> am [at] [~]$ repeat 2 "echo $(cat /proc/loadavg ; date +%H:%m:%S)"
> 0.05 0.14 0.24 6/127 27711 12:07:29
> 0.05 0.14 0.24 6/127 27711 12:07:29
> 0.05 0.14 0.24 6/127 27711 12:07:29
>
> as you can see, only one instance of load average and time are repeated.
> i want a running record that can be redirected to a file
>
> i just tinkered with backslashes, back-quotes, double-quotes and
> single-quotes, but all the quoting stuff still confuses me.
>
> and, imho, i would have thought someone would have written a simple
> tool to do this ages ago. no?
>
> thanx again,
> andi
>
--
AD Marshall
Tel: +84 (0)903871313
eM: admarshall [at] gmail.com
Web: http://h0lug.sourceforge.net
Zone: ICT (IndoChina Time; GMT/UTC+7)
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo [at] vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
[Solution] how-to: one command for repeat OR iterate shell OR bash command delay OR interval
I worked out a cheap, if buggy, hack for what i wanted this morning. =A0=
But thanks all the same... =A0
In case you're interested:
am [at] [sh]$ rewatch 2 'cat /proc/loadavg'
0.51 0.36 0.65 1/121 9072 09:56:08
0.51 0.36 0.65 1/121 9083 09:56:10
0.47 0.36 0.65 1/121 9088 09:56:13
am [at] [sh]$ rewatch 2 'cat /proc/loadavg' 'mozilla' | tee rewatch-mozilla
0.43 0.35 0.65 1/121 9100 09:56:20
am =A0 =A0 =A0 32106 =A00.0 =A00.3 =A0 3832 =A0 988 ? =A0 =A0 =A0 =A0S =
=A0 =A007:37 =A0 0:00 /bin/sh /usr/bin/mozilla
am =A0 =A0 =A0 32112 10.0 25.7 141688 66084 ? =A0 =A0 =A0 =A0Sl =A0 07:=
37 =A013:54 /opt/mozilla/lib/mozilla-bin
0.40 0.34 0.64 3/121 9109 09:56:22
am =A0 =A0 =A0 32106 =A00.0 =A00.3 =A0 3832 =A0 988 ? =A0 =A0 =A0 =A0S =
=A0 =A007:37 =A0 0:00 /bin/sh /usr/bin/mozilla
am =A0 =A0 =A0 32112 10.0 25.7 141688 66092 ? =A0 =A0 =A0 =A0Sl =A0 07:=
37 =A013:54 /opt/mozilla/lib/mozilla-bin
am [at] [sh]$ cat ~/bin/rewatch
#!/bin/sh
# rewatch, v. 0.01
#
# Usage: rewatch <n> <cmdline> [process-to-watch]
# =A0 n: seconds between repetitions
# =A0 cmdline: command-line to repeat
# =A0 process-to-watch: process for which "ps aux" info is to be displa=
yed
#
# Purpose: repeat and, optionally, watch and record the "ps aux" info o=
f
# that or any other process
#
# Bkgd: original purpose was to be able to input a command-line (in sin=
gle
# quotes) along with an interval in seconds for its repetion and print =
a
# list of its stdouts labelled by the times they were printed until <Ct=
rl-c>
# is hit, and be able to capture all the output to stdout or a file.
#
# initial use was for watching and recording load averages as programs =
were
# loaded, eg, "re_watch 2 'cat /proc/loadavg' konqueror"
#
# Future: unknown
#
# Known Bugs: no input validation or user feedback for input errors
#
# Of course, bash operators will have to be properly quoted or escaped =
or
# not... -- which i still don't clearly understand
n=3D$1
cmdline=3D$2
proc2watch=3D$3
if test -z "$3"
then
=A0 while true ; do echo $( $cmdline && echo "$( date +%H:%M:%S )" ) ; =
sleep $n ; done
else
while true ; do echo $( $cmdline && echo "$( date +%H:%M:%S )" ) ; echo=
"$( ps aux | grep -vE "(rewatch |grep |tee )" | grep $proc2watch )" ; =
sleep $n ; done
fi
am [at] [sh]$
On Sunday 17 July 2005 13:16, AD Marshall wrote:
> my apologies, if necessary. but it's way past lunchtime in saigon an=
d
> i'm STARVING. i'll check back later. thanx again. - best, andi
>
> On Sunday 17 July 2005 13:07, AD Marshall wrote:
> > On Sunday 17 July 2005 12:35, John Kelly wrote:
> > > Hi,
> > > On Sun, 17 Jul 2005 11:57:34 +0700
> > > AD Marshall <admarshall [at] gmail.com> wrote:
> > >
> > > > Is there already one bash command to do what the following scri=
pt
> > > > does (poorly or incompletely), ie repeat "command" indefinitely
> > > > every "x" seconds:
> > > >
> > > > #!/bin/sh
> > > > # usage: repeat [x] <command>
> > > > while true ; do $2 ; sleep $1 ; done
> > > >
> > > > < rest deleted >
> > >
> > > The watch command might tbe what you want.
> > > Or maybe not, if you don't want anything output to screen.
> > >
> > > Try man watch for details.
> > >
> > <cut>
> > Ya. Thanks. I'd (long) forgotten about "watch".
> >
> > but, actually, i should be more specific. what i'm trying to do is
> > something like this -- though i'm screwing up on quoting or somethi=
ng
> >
> > am [at] [~]$ repeat 2 "echo $(cat /proc/loadavg ; date +%H:%m:%S)"
> > 0.05 0.14 0.24 6/127 27711 12:07:29
> > 0.05 0.14 0.24 6/127 27711 12:07:29
> > 0.05 0.14 0.24 6/127 27711 12:07:29
> >
> > as you can see, only one instance of load average and time are repe=
ated.
> > i want a running record that can be redirected to a file
> >
> > i just tinkered with backslashes, back-quotes, double-quotes and
> > single-quotes, but all the quoting stuff still confuses me.
> >
> > and, imho, i would have thought someone would have written a simple=
> > tool to do this ages ago. no?
> >
> > thanx again,
> > andi
> >
>
--
AD Marshall
Tel: +84 (0)903871313
eM: admarshall [at] gmail.com
Web: http://h0lug.sourceforge.net
Zone: ICT (IndoChina Time; GMT/UTC+7)
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie"=
in
the body of a message to majordomo [at] vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs