Section Next | Prev


003 repeat - コマンドの繰り返し実行

概要
指定した回数だけ、つづくジョブを繰り返し実行する。

スクリプトと解説 - [text]
#スクリプト解説
1#! /bin/bash #
2# command repeater, usage: REPEAT number command.. #
3num=$1 最初の引数を num とする
4shift 引数全体を1つ左へずらす
($2を$1,$3を$2へ、元の$1は失われる)
5while (($num>0)) num 回数繰り返す(7と関連して)
6do #
7        let num=num-1 #
8        eval "$*" 引数を実行
9done #

実行例
$ repeat 10 dice 1
1
3
4
6
6
3
3
5
6
2

# diceの動作検証
# 100回サイコロをふり、結果をソート(sort)し発生頻度を数える(uniq)
$ repeat 100 dice 1 | sort -n |uniq -c
     18 1
     16 2
     17 3
     17 4
     16 5
     16 6

$ repeat 100 dice 2 | sort -n |uniq -c
      2 2
      6 3
      9 4
     12 5
     10 6
     19 7
     16 8
      9 9
      9 10
      4 11
      4 12

Section Next | Prev