-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathothers-mainstream.sh
More file actions
74 lines (59 loc) · 1.47 KB
/
others-mainstream.sh
File metadata and controls
74 lines (59 loc) · 1.47 KB
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
### [function]
function say () {
echo $1;
}
### [function]
### [ifElse]
if true; then
say "it is true";
else
say "it is not true";
fi
### [ifElse]
### [loops]
for i in 1 2 3 4 5; do
say $i;
done
for ((i=1; i <= 5; i++)); do
say $i;
done
# the `until true` loop can be thought of as a `while false` loop
until true; do
say "this is never executed";
done
# the `while true` loop can be thought of as a `until false` loop
while true; do
say "this is an infinite loop";
done
### [loops]
### [short]
true && say "it is true" || say "it is not true"
### [short]
### [recursion]
function while_loop () {
say "yes" && true && while_loop;
}
function for_loop () {
let i++;
say $i;
test $i == 5 || for_loop;
}
for_loop # <1>
while_loop # <2>
# (1) execute for loop first
# (2) since the while loop will enter an infinite loop,
# you will need to cancel the execution by pressing CTRL-C
### [recursion]
# == Functional constructs
alias grep="grep --extended-regexp"
{ # <1> <2>
tr a-z A-Z \
| tr ' ' "\n" \
| grep --invert-match 'O|S'; # <3>
} <<< "One Two Three Four Five Six"
shopt -s expand_aliases
# (1) map lower-case letters to upper-case letters
# (2) replace space with newlines
# (3) filter away lines containing 'O' or 'S'
alias ls="ls -l --color=always --almost-all --group-directories-first --human-readable --inode --sort=time" # <4>
# (4) partially apply flags to ls