-
Notifications
You must be signed in to change notification settings - Fork 2
/
bash_loops
executable file
·111 lines (73 loc) · 3 KB
/
bash_loops
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env bash
# Bash can iterate through a list of strings with space or newline separators.
VALS="one two three four five"
# a simple For loop iterates with space separators.
for ITEM in $VALS
do
echo "value: $ITEM"
done
# or simply, written with one fewer line, given that bash commands
# can be written on a single line, seperated by a semicolon, rather
# than one line at a time.
for ITEM in $VALS; do
echo "value: $ITEM"
done
echo # -------------------------------
# A formal Array object would look like this.
VALS=( 'five' 'four' 'three' 'two' 'one' )
# A For loop that iterates through a formal array. Note the [@] referend
# in ITEMS, this is represents *every* entry in the array, much like $@
# represents the entirety of the command line arguments. Also note that
# there can be no spaces between the array variable and the curly braces
# that surround it within the quotes.
for ITEM in "${VALS[@]}"; do
echo "value: $ITEM"
done
echo # -------------------------------
# A for loop can also be written with a given range of values and
# increments. Note that the numerical range is inline to the command.
# This is a requirement, the range cannot be placed in a variable.
# The range, however, can go in any direction; asecending or decescending
# as necessary. Also note that there can be no spaces between the range
# value and the curly braces that surround it.
for ITEM in {5..1}; do
echo "count: $ITEM"
done
# or with an increment value other than 1. Note that because we're
# counting backwards here, our increment value is a negative number.
for ITEM in {10..1..-2}; do
echo "count: $ITEM"
done
# or with a variable for a start and end value.
# Note that we're just calling the 'seq' bash command. It's output is
# the equivalent of all of the values output one-by-one with a newline
# in between each value. This is similar to the first example in this
# example script but with newlines instead of spaces between each value
# iterated over. Either a newline or a space can be used here with the
# same end result.
START=1
END=10
for ITEM in $( seq $START $END ); do
echo "count: $ITEM"
done
# or backwards, decremented by a value other than 1 at a time. Note that
# the the 'seq' command has the increment value, if one is required,
# between the start and end values. Also note that because we're counting
# backwards, our increment is a negative number.
START=10
END=1
INCREMENT=-2
for ITEM in $( seq $START $INCREMENT $END ); do
echo "count: $ITEM"
done
echo # -------------------------------
# A for loop that resembles a conditional For loop such as those used in
# C, Java, or JavaScript. The value contained in LIMIT can be either
# inline or it can be placed into a variable. Of course, this type of
# loop can also be ascending or descending. Also note that only the
# LIMIT variable requires the $ preceding it's name here. Also, spaces
# are allowed within the variables and conditionals, for legibility.
LIMIT=5
for (( IDX = 1; IDX <= $LIMIT; IDX++ )); do
echo "count: $IDX"
done