27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

2<br />

3<br />

4<br />

5<br />

bash$ seq −s : 5<br />

1:2:3:4:5<br />

Both jot and seq come in handy in a for loop.<br />

Example 12−39. Using seq to generate loop arguments<br />

#!/bin/bash<br />

# Using "seq"<br />

echo<br />

for a in `seq 80` # or for a in $( seq 80 )<br />

# Same as for a in 1 2 3 4 5 ... 80 (saves much typing!).<br />

# May also use 'jot' (if present on system).<br />

do<br />

echo −n "$a "<br />

done # 1 2 3 4 5 ... 80<br />

# Example of using the output of a command to generate<br />

# the [list] in a "for" loop.<br />

echo; echo<br />

COUNT=80 # Yes, 'seq' may also take a replaceable parameter.<br />

for a in `seq $COUNT` # or for a in $( seq $COUNT )<br />

do<br />

echo −n "$a "<br />

done # 1 2 3 4 5 ... 80<br />

echo; echo<br />

BEGIN=75<br />

END=80<br />

for a in `seq $BEGIN $END`<br />

# Giving "seq" two arguments starts the count at the first one,<br />

#+ and continues until it reaches the second.<br />

do<br />

echo −n "$a "<br />

done # 75 76 77 78 79 80<br />

echo; echo<br />

BEGIN=45<br />

INTERVAL=5<br />

END=80<br />

for a in `seq $BEGIN $INTERVAL $END`<br />

# Giving "seq" three arguments starts the count at the first one,<br />

Chapter 12. External Filters, Programs and Commands 205

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!