repeat – замена for (( … ))
- #!/bin/bash -e
- # $1: count
- # stdin: action(s)
- let i="$1"
- unset commands
- while read cmd; do
- commands+=("$cmd")
- done
- until [[ "$i" == "0" ]]; do
- for cmd in "${commands[@]}"; do
- eval "$cmd"
- done
- (( i-- ))
- done
Примеры использования:
- $ repeat 3 <<< ls ~
- bin configs desktop devel downloads github linux miscellanea multimedia music pictures public secure
- bin configs desktop devel downloads github linux miscellanea multimedia music pictures public secure
- bin configs desktop devel downloads github linux miscellanea multimedia music pictures public secure
- $ mkcd /tmp/test
- mkdir: created directory '/tmp/test'
- $ repeat 5 <<< 'echo "$RANDOM" > "$RANDOM"'
- $ ls
- 1188 12704 13387 19403 8285
- $ repeat 2 <<-"EOF"
- echo foo
- echo bar
- EOF