256

Is there a way to run a command (e.g. ps aux|grep someprocess) for n times?

Something like:

run -n 10  'ps aux|grep someprocess'

I want to use it interactively.

Update: The reason I am asking this is, that I do work on a lot of machines and I don't want to import all my adaped scripts etc into every box to get the same functionality accross every machine.

mahatmanich
  • 3,124
  • 3
  • 25
  • 24

10 Answers10

340

I don't think a command or shell builtin for this exists, as it's a trivial subset of what the Bourne shell for loop is designed for and implementing a command like this yourself is therefore quite simple.

Per JimB's suggestion, use the Bash builtin for generating sequences:

for i in {1..10}; do command; done

For very old versions of bash, you can use the seq command:

for i in `seq 10`; do command; done

This iterates ten times executing command each time - it can be a pipe or a series of commands separated by ; or &&. You can use the $i variable to know which iteration you're in.

If you consider this one-liner a script and so for some unspecified (but perhaps valid) reason undesireable you can implement it as a command, perhaps something like this on your .bashrc (untested):

#function run
run() {
    number=$1
    shift
    for i in `seq $number`; do
      $@
    done
}

Usage:

run 10 command

Example:

run 5 echo 'Hello World!'
mikemaccana
  • 3,710
45

ps aux | grep someprocess looks like you want to watch changes of a program for a fixed time. Eduardo gave an answer that answer your question exactly but there is an alternative: watch:

watch 'ps aux | grep someprocess'

Note that I've put the command in single quotes to avoid the shell from interpreting the command as "run watch ps aux" and pipe the result through grep someprocess. Another way to do the previous command would be:

watch ps aux \| grep someprocess

By default, watch refreshes every two seconds, that can be changed using the -n option. For instance, if want to have an interval of 1 second:

watch -n 1 'ps aux | grep someprocess'
Lekensteyn
  • 6,445
29

Just for fun

pgrep ssh
!!;!!;!!;!!;!!;!!

NOTE: You must run the command pgrep ssh first, separately, and then enter the !!;!!;!!;!!;!!;!! string. Otherwise, the !! are replaced with the command that was previously executed.

; is a command separator and !! replay last command in bash. So this runs pgrep ssh and then replays it 6 times.

JESii
  • 103
hdaz
  • 299
27

Similar to previous replies, but does not require the for loop:

seq 10 | xargs -I{} -- echo "hello"

This will run your command (after the --) 10 times. The only caveat is that any occurrences of the literal string {} in your command will be replaced by the number from seq.

sparrowt
  • 319
19

Try this:

yes ls | head -n5 | bash

This requires the command to be executed in a sub-shell, a slight performance penalty. YMMV. Basically, you get the "yes" command to repeat the string "ls" N times; while "head -n5" terminated the loop at 5 repeats. The final pipe sends the command to the shell of your choice.

Incidentally csh-like shells have a built-in repeat command. You could use that to execute your command in a bash sub-shell!

Jakuje
  • 10,363
android42
  • 191
17

With ZSH (the default in MacOS Terminal) you can use the repeat command.

repeat 10 ps aux | grep someprocess
Neil
  • 281
9

POSIX way

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04_01

x=10
while [ $x -gt 0 ]; do
    command
    x=$(($x-1))
done

which could of course be made into a one liner:

x=10; while [ $x -gt 0 ]; do command; x=$(($x-1)); done

and seems to be the most portable way, and thus less likely to make you install programs.

The following are less portable:

  • brace expansion {1..10}: bash specific, and would generate a huge line if the argument is large
  • seq: GNU
  • yes: GNU

And if you get tired of portability, consider GNU parallel:

sudo apt-get install parallel
seq 100 | parallel echo {}

which runs commands in parallel and has many cool options.

See also: https://stackoverflow.com/questions/169511/how-do-i-iterate-over-a-range-of-numbers-defined-by-variables-in-bash

3

A fish shell implementation of @eduardo-ivanec's run function above

function run
  set number $argv[1]
  for i in (seq $number)
    eval $argv[2..-1]
  end
end
chicks
  • 3,915
  • 10
  • 29
  • 37
masukomi
  • 101
2

To run ps aux multiple times searching for a different known string each time:

for str in {str1,str2,...}; do ps aux | grep $str | grep -v " grep "; done

The grep -v " grep " part will remove grep itself from the result, which is a bad idea if one of your search strings is grep.

1

That would be 'watch' - should be part of almost every unix system using procps

You can see fs changes or otherwise interesting movement in a system. For example,

watch "df -hP | grep /tmp" 

Just put your commands as an argument (see man watch)