forked from ItzCog/1340-cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
277 lines (274 loc) · 12.4 KB
/
index.html
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="style.css" rel="stylesheet">
<title>ENGG1340</title>
</head>
<body>
<div class="page">
<h1 class="main-title">ENGG1340 Cheat Sheet</h1>
<section class="sub-section">
<h2 class="sub-header">Common Command Line Tools</h2>
<ul class="section-list">
<li>underline indicates an argument; arguments with wavy underline are optional</li>
<li><b>echo</b> output (usage: echo <span class="arg">string</span>)</li>
<li><b>sort</b> sort the lines in input by <span class="arg">column</span>(usage: sort <span class="opt-arg">options</span>
<span class="arg">input</span>
-k <span class="arg">column</span>)</li>
<li><b>uniq</b> remove repeated adjacent lines (usage: uniq
<span class="opt-arg">options</span>
<span class="arg">input</span>
<span class="arg">output</span>))</li>
<li><b>grep</b> find line(s) that matches with the given pattern (usage: grep
<span class="arg">pattern</span>
<span class="arg">input</span>)</li>
<li><b>ls</b> list all files under current directory (usage: ls
<span class="opt-arg">options</span>)</li>
<li><b>spell</b> returns all misspelled word in input (usage: spell
<span class="arg">input</span>)</li>
<li><b>diff</b> differentiate two files (usage: diff
<span class="arg">file1</span>
<span class="arg">file2</span>)</li>
<li><b>chmod</b> edit file access (usage: chmod
<span class="arg">access</span>
<span class="arg">file</span>)</li>
<li><b>cp</b> copy file1 into file2 (usage: cp
<span class="arg">file1</span>
<span class="arg">file2</span>)</li>
<li><b>mv</b> move/rename file (usage: mv
<span class="arg">filename</span>
<span class="arg">target</span>)</li>
<li><b>tar</b> archive tool (usage: tar
<span class="arg">option</span>
<span class="arg">target file</span>
<span class="opt-arg">files</span>)</li>
<li><b>rm</b> remove file/directory (usage: rm
<span class="opt-arg">options</span>
<span class="arg">file</span>)</li>
</ul>
</section>
<section class="sub-section">
<h2 class="sub-header">Bash Scripting Tools & Syntax</h2>
<ul class="section-list">
<li><b>read <span class="arg">varname</span></b>
read user input and store it into a variable called <span class="arg">varname</span></li>
<li><b>let <span class="arg">string</span></b> treat the string as bash command(s)</li>
<li>
<b>string comparison</b>
equal [ $a == $b ]; unequal [ $a != $b ]; lesser [ $a \< $b ]; greater [ $a \> $b ]
</li>
<li>
<b>number comparison</b>
equal [ $a -eq $b ];
unequal [ $a -ne $b ];
lesser [ $a -lt $b ];
not greater [ $a -le $b ];
greater [ $a -gt $b ];
not lesser [ $a -ge $b ]
</li>
<li>
<b>file/directory checking</b>
exists [ -e $name ];
is file [ -f $name ];
is directory [ -d $name ];
size > 0 (not empty) [ -s $name ];
is readable/writable/executable [ -r/-w/-x $name ]
</li>
<li>
<b>string operation</b>
string length ${#str};
substring starting from $pos with length of $len ${str:$pos:$len};
string with first occurrence of $from replaced with $to ${str/$from/$to}
</li>
<li class="indented">
string with all occurrences of $from replaced with $to ${str//$from/$to}
</li>
<li>
<b>command line arguments:</b>
<b>$#</b> number of arguments
<b>${n}</b> the n-th argument (the 0th is the shell script)
</li>
</ul>
<ul class="section-list">
<li><b>for loop</b> for <span class="arg">iterator</span> in <span class="arg">$iterable</span>;
do <span class="arg">loop body</span>; done</li>
<li class="indented"><b>or</b> for ((
<span class="arg">expr1</span>;
<span class="arg">expr2</span>;
<span class="arg">expr3</span>
));
do <span class="arg">loop body</span>; done</li>
<li><b>if statement</b>
if [ <span class="arg">condition1</span> ];
then <span class="arg">branch1</span>;
elif [ <span class="arg">condition2</span> ];
then <span class="arg">branch2</span>;
else;
<span class="arg">branch3</span>;
fi
</li>
</ul>
</section>
<section class="sub-section">
<h2 class="sub-header">sort options</h2>
<ul class="section-list">
<li><b>-b</b> ignore spaces at front</li>
<li><b>-d</b> handle only letters, numbers & spaces</li>
<li><b>-f</b> case insensitive</li>
<li><b>-n</b> numeric sort</li>
<li><b>-u</b> unique</li>
<li><b>-r</b> reverse order</li>
<li><b>-t</b> column separator</li>
<li><b>-s</b> stable sort</li>
</ul>
</section>
<section class="sub-section">
<h2 class="sub-header">chmod</h2>
<ul class="section-list">
<li><b>basic format</b> chmod
<span class="arg">group</span> <span class="arg">operator</span> <span class="arg">permission</span>
<span class="arg">file</span>
</li>
<li class="indented">no space between <span class="arg">group</span>, <span class="arg">operator</span> and
<span class="arg">permission</span></li>
<li><b>group</b> u: file owner; g: file owner's group; o: other users; a: all users</li>
<li><b>operator</b> +: add; -: remove; =: set</li>
<li><b>permission</b> r: read; w: write; x: execute</li>
</ul>
</section>
<section class="sub-section">
<h2 class="sub-header">Misc</h2>
<ul class="section-list">
<li>
<b>tar cvf <span class="arg">target</span>
<span class="arg">file1</span>
<span class="arg">file2</span>
<span class="arg">file3</span> ...</b>
creates archive file <span class="arg">target</span> out of the files
</li>
<li>
<b>tar xvf <span class="arg">target</span></b>
un-archives <span class="arg">target</span>
</li>
<li>
<b>uniq -c <span class="arg">input</span></b>
after removing repetition, prefix each line with a number representing the repetition count
</li>
<li>
<b>rm -rf <span class="arg">directory</span></b>
deletes <span class="arg">directory</span>, along with all files and subdirectories inside
</li>
<li>
<b>rm -f <span class="arg">target</span></b>
force delete, gives no prompt & does not raise errors if <span class="arg">target</span>
does not exist (good for Makefile clean up)
</li>
<li>
<b>cut -d <span class="arg">separator</span> -f <span class="arg">n</span>
<span class="arg">input</span></b>
returns the <span class="arg">n</span>th entry in <span class="arg">input</span> using
<span class="arg">separator</span> (index starts from 1)
</li>
<li>
<b>cut -c <span class="arg">n</span> <span class="arg">input</span></b>
returns the <span class="arg">n</span>th character in <span class="arg">input</span>
(index starts from 1)
</li>
<li>
<b>g++ <span class="arg">cpp</span> -o <span class="arg">o</span> > <span class="arg">err</span></b>
compiles <span class="arg">cpp</span> into <span class="arg">o</span> and stores error message (if any) into
<span class="arg">err</span> (use -f to check if there's an error)
</li>
<li>
<b>g++ -c <span class="arg">cpp</span></b>
compile & assembly without linking (use in makefile for .cpp function definition files)
</li>
<li>
<b>./<span class="arg">o</span> < <span class="arg">in</span> > <span class="arg">out</span></b>
runs <span class="arg">o</span> with <span class="arg">in</span> as input and stores the output into
<span class="arg">o</span>
</li>
</ul>
</section>
<section class="sub-section">
<h2 class="sub-header">regex</h2>
<ul class="section-list">
<li><b>.</b> all characters</li>
<li><b>\w</b> word (letters & digits); <b>\d</b> digits; <b>\s</b> whitespace</li>
<li><b>\W</b> not word; <b>\D</b> not digits; <b>\S</b> not whitespace</li>
<li><b>[abc]</b> any of a, b, c; <b>[^abc]</b> not a, b or c</li>
<li><b>[a-g]</b> character between a & g</li>
<li><b>^abc&</b> start / end of the line</li>
<li><b>\b</b> word boundary; <b>\B</b> not word boundary</li>
<li><b>a*</b> 0 or more; <b>a+</b> 1 or more; <b>a?</b> 0 or 1</li>
<li><b>a{5}</b> exactly 5; <b>a{2,}</b> 2 or more</li>
<li><b>a{1,3}</b> between 1 & 3</li>
<li><b>a+? a{2,}?</b> match as few as possible</li>
<li><b>ab|cd</b> ab or cd</li>
</ul>
</section>
</div>
<div class="page">
<section class="sub-section extra-margin-top">
<h2 class="sub-header">Additional Helpful Bash Features</h2>
<ul class="section-list">
<li><b>Arrays</b></li>
<li>declaration: <span class="arg">varname</span>=(<span class="opt-arg">elem0</span>
<span class="opt-arg">elem1</span>
<span class="opt-arg">elem2</span>...) </li>
<li>get value: ${<span class="arg">varname</span>[<span class="arg">index</span>]} (index starts from 0)</li>
<li class="indented">get all values: ${<span class="arg">varname</span>[*]}</li>
<li>set value: <span class="arg">varname</span>[<span class="arg">key</span>]=<span class="arg">element
</span></li>
<li>add item: <span class="arg">varname</span>+=(<span class="arg">elem</span>)</li>
<li>remove item: unset <span class="arg">varname</span>[<span class="arg">index</span>]</li>
<li><b>Associative Arrays</b></li>
<li>declaration: declare -A <span class="arg">varname</span>=([<span class="opt-arg">key0</span>]=<span class="opt-arg">value0</span>
[<span class="opt-arg">key1</span>]=<span class="opt-arg">value1</span>
[<span class="opt-arg">key2</span>]=<span class="opt-arg">value2</span>...)
</li>
<li>get value: ${<span class="arg">varname</span>[<span class="arg">key</span>]}</li>
<li class="indented">get all values: ${<span class="arg">varname</span>[*]}</li>
<li class="indented">get all keys: ${!<span class="arg">varname</span>[*]}</li>
<li>set value/add item: <span class="arg">varname</span>[<span class="arg">key</span>]=<span class="arg">value</span></li>
<li>check if key exists: [ ${<span class="arg">varname</span>[<span class="arg">key</span>]} ]</li>
<li>remove item: unset <span class="arg">varname</span>[<span class="arg">key</span>]</li>
<li><b>printf</b></li>
<li>usage: printf <span class="arg">format</span> <span class="opt-arg">arguments</span></li>
<li>format string uses the same style as C language printf</li>
<li>if there are more arguments than needed, formatted string will be printed multiple times, cycling
through the arguments</li>
<li>specifiers without arguments becomes 0 / empty string</li>
</ul>
</section>
<section class="sub-section">
<h2 class="sub-header">
Makefile
</h2>
<ul class="section-list">
<li>
<b><span class="arg">target</span>: <span class="arg">dependency1</span>
<span class="arg">dependency2</span> ...</b>
</li>
<li class="indented">
<b>command</b>
</li>
<li>
<b>macros</b>
define: <span class="arg">VARNAME</span> = value;
get value: $(<span class="arg">VARNAME</span>)
</li>
<li>
<b>special macros:</b>
$@ the target; $^ the dependency list; $< the leftmost dependency
</li>
<li>
<b>.PHONY: <span class="arg">target1</span> <span class="arg">target2</span> ...</b>
fake target; corresponding command will always run (good for clean up command)
</li>
</ul>
</section>
</div>
</body>
</html>