blob: 62587953958b939a7b7fc21eeb666a24ec9a568b (
plain)
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
|
# Bash scripting
## String manipulation
Remove shortest from end
${VAR%substr}
# e.g.
${FILEPATH%.*}.out # change extension from whatever to .out
Remove longest from start
${VAR##substr}
# e.g.
${FILEPATH##*/} # get only file name portion
%% - longst from end, # - shortest from start
## Control flow
Loop over files
for file in glob/*; do something $file; done
Loop over integers
for i in `seq 10`; do echo $i; done
## Conditionals
* `-z`: is empty string
* `-n`: non-empty string
* `-f`: is regular file
* `$A == $B`: string equality, accepts globs
* `$A != $B`: string inequality
## Heredocs
cat << EOF > file
bla bla
EOF
Ignoring the initial indent:
...
cat <<- EOF > file
bla bla
EOF
...
Don't interpolate variables:
cat << 'EOF' > file
bla bla
EOF
## Arrays
Declaring indexed arrays:
declare -a name
Associative arrays:
declare -A name
Assignment:
array=([bla]=foo [ble]=bar...)
All values:
${array[*]} # one word, elements separated with first character of $IFS
${array[@]} # separate words when double quoted
All keys:
${!array[*]}
${!array[@]}
## Getopts example
while getopts 'a:b:c' flag; do
case "${flag}" in
a) do_something $OPTARG ;;
b) b_option=$OPTARG ;;
c) c_flag=1 ;;
*) error "Unexpected option ${flag}" ;;
esac
done
### With positional arguments
Positional arguments will have to be supplied after flag options (`command
[options] <args>`):
shift $((OPTIND-1))
|