-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
17-for-loops.sh
78 lines (64 loc) · 1.39 KB
/
17-for-loops.sh
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
echo "
##########################
## Example 17.1: #
## basic for loop syntax #
##########################
"
# you can just list the words you want to loop over!
for i in panda swan
do
echo "$i"
done
echo "
######################
## Example 17.2: #
## a 1-line for loop #
######################
"
# usually when I write for loops on the command line, I just
# press enter and type
# for i in ...
# do
# ....
# done
# But you can also write the for loop on one line if you want!
for i in banana mango pear; do echo "$i"; done
echo "
###########################
## Example 17.3: #
## looping over filenames #
###########################
"
set -x
# this converts all .svg files in files/ to .pngs
for i in files/*.svg
do
convert "$i" "${i/svg/png}"
done
set +x
echo "
#########################################
## Example 17.4: #
## for loops loop over words by default #
#########################################
"
# notice that "filename with spaces" gets listed in 3 different lines, not 1 line
for i in $(ls files/)
do
echo $i
done
echo "
#################################
## Example 17.5: #
## loop over a range of numbers #
#################################
"
for i in $(seq 1 10)
do
echo $i
done
echo 'or with {1..5}:'
for i in {1..5}
do
echo $i
done