40
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local pairs = Luan.pairs or error()
|
|
4 local Io = require "luan:Io.luan"
|
|
5 local Http = require "luan:http/Http.luan"
|
|
6 local Shared = require "site:/lib/Shared.luan"
|
|
7 local head = Shared.head or error()
|
|
8 local header = Shared.header or error()
|
|
9
|
|
10
|
|
11 local content = {
|
|
12 intro = {
|
45
|
13 title = [[Introduction]]
|
40
|
14 content = function()
|
|
15 %>
|
50
|
16 <p>I really don't want to write this tutorial, but all the existing Bash tutorials are so horrible that I have no choice. I looked at books, websites, and YouTube - all horrible. They don't start with the basics. They include all kinds of useless crap. And they don't explain core concepts. So I have no choice but to write this for my <a href="http://localhost:8080/learn.html#bash">Learn Reactionary Programming</a> Bash lesson.</p>
|
40
|
17
|
49
|
18 <p><a href="bash.html">Bash</a> is a <a href="https://en.wikipedia.org/wiki/Unix_shell">shell</a>, one of many, but the one I prefer. I will focus on Mac and Windows. I don't have Linux, and I hate Linux, so I won't discuss it. Most of Bash is the same on Mac and Windows, but where they differ, I will discuss both.</p>
|
40
|
19 <%
|
|
20 end
|
|
21 }
|
45
|
22 access = {
|
|
23 title = [[Running Bash]]
|
40
|
24 content = function()
|
|
25 %>
|
45
|
26 <p>How you access Bash depends on your operating system. If you are on a Mac then you access Bash through the Mac Terminal which is found in "Applications > Utilities > Terminal.app". Be sure to <a href="https://www.howtogeek.com/444596/how-to-change-the-default-shell-to-bash-in-macos-catalina/">set the default shell to Bash</a>. If you are on Windows then install <a href="https://www.msys2.org/">MSYS2</a>. The default terminal isn't so good, so I suggest <a href="https://www.msys2.org/docs/terminals/#windows-terminal">using the Windows Terminal</a>.</p>
|
40
|
27 <%
|
|
28 end
|
|
29 }
|
45
|
30 start = {
|
|
31 title = [[Getting Started]]
|
41
|
32 content = function()
|
|
33 %>
|
45
|
34 <p>When I start Bash on my Mac I see:</p>
|
|
35
|
|
36 <code block>
|
|
37 Last login: Thu Jan 4 23:25:35 on ttys004
|
|
38
|
|
39 The default interactive shell is now zsh.
|
|
40 To update your account to use zsh, please run `chsh -s /bin/zsh`.
|
|
41 For more details, please visit https://support.apple.com/kb/HT208050.
|
|
42 ~ $
|
44
|
43
|
45
|
44 </code>
|
|
45
|
|
46 <p>On Windows - MSYS2 I just see:</p>
|
44
|
47
|
45
|
48 <code block>
|
|
49 ~ $
|
|
50
|
|
51 </code>
|
|
52
|
|
53 <p>The line with the <b><code>$</code></b> is the command prompt. The cursor is at the end of it, and if I type, my text will go there. You may have different text before the <b><code>$</code></b> which is okay, but the line should end with <b><code>$</code></b>. If it doesn't, something is wrong.</p>
|
|
54
|
|
55 <p>Now type "qqq". When I say type "whatever", you should type return/enter at the end. Only when you type return/enter will Bash process what you typed. Now you should see:</p>
|
44
|
56
|
45
|
57 <code block>
|
|
58 ~ $ qqq
|
|
59 -bash: qqq: command not found
|
|
60 ~ $
|
|
61 </code>
|
|
62
|
|
63 <p>Bash doesn't know what "qqq" means and says so. Now try the following... Note that you type what is after the <b><code>$</code></b> and Bash should respond as shown.</p>
|
|
64
|
|
65 <code block>
|
|
66 ~ $ echo hi
|
|
67 hi
|
|
68 ~ $ echo how are you
|
|
69 how are you
|
|
70 ~ $ echo bye
|
|
71 bye
|
|
72 </code>
|
|
73
|
49
|
74 <p>The <code>echo</code> command just echoes what comes after. Now press the up-arrow on your keyboard. This should put the previous command where your cursor is. Up-arrow again brings the command before that. Try down-arrow and left-arrow and right-arrow. You can use this to navigate through your command history. The delete key also works for editing lines. And of course you can type. When you press return/enter then Bash will get your edited command and process it.</p>
|
45
|
75
|
|
76 <p>When you enter <code>echo how are you</code>, <code>echo</code> is the command. This command has 3 arguments: <code>how</code>, <code>are</code>, and <code>you</code>. Commands and arguments are separated with spaces. It doesn't matter how many spaces, so:</p>
|
|
77
|
|
78 <code block>
|
|
79 ~ $ echo how are you
|
|
80 how are you
|
|
81 </code>
|
|
82
|
|
83 <p><code>echo</code> just returns the arguments separated by one space.</p>
|
49
|
84
|
|
85 <code block>
|
|
86 ~ $ echo one; echo two
|
|
87 one
|
|
88 two
|
|
89 </code>
|
|
90
|
|
91 <p>You can put multiple commands on one line separated by a <code>;</code>.</p>
|
41
|
92 <%
|
|
93 end
|
|
94 }
|
45
|
95 man = {
|
|
96 title = [[The "man" Command]]
|
41
|
97 content = function()
|
|
98 %>
|
45
|
99 <p>Enter:</p>
|
|
100 <code block>
|
|
101 ~ $ man echo
|
|
102 </code>
|
|
103
|
49
|
104 <p>You should get something like:</p>
|
45
|
105
|
|
106 <code block>
|
|
107
|
|
108 ECHO(1) BSD General Commands Manual ECHO(1)
|
|
109
|
|
110 NAME
|
|
111 echo -- write arguments to the standard output
|
|
112
|
|
113 SYNOPSIS
|
|
114 echo [-n] [string ...]
|
|
115
|
|
116 DESCRIPTION
|
|
117 The echo utility writes any specified operands, separated by single blank
|
|
118 (` ') characters and followed by a newline (`\n') character, to the stan-
|
|
119 dard output.
|
|
120
|
|
121 The following option is available:
|
|
122
|
|
123 -n Do not print the trailing newline character. This may also be
|
|
124 achieved by appending `\c' to the end of the string, as is done by
|
|
125 iBCS2 compatible systems. Note that this option as well as the
|
|
126 effect of `\c' are implementation-defined in IEEE Std 1003.1-2001
|
|
127 (``POSIX.1'') as amended by Cor. 1-2002. Applications aiming for
|
|
128 maximum portability are strongly encouraged to use printf(1) to
|
|
129 suppress the newline character.
|
|
130 :
|
|
131 </code>
|
|
132
|
|
133 <p>But if you are on Windows, you may not have <code>man</code> installed. In that case, do:</p>
|
|
134
|
|
135 <code block>
|
|
136 ~ $ pacman -S man-db
|
|
137 </code>
|
|
138
|
|
139 <p>to install <code>man</code> as described <a href="https://packages.msys2.org/package/man-db">here</a> and then try <code>man echo</code> again.</p>
|
|
140
|
|
141 <p>The <code>man</code> command shows documentation of commands. Unfortunately it has a silly user interface based on memorizing keys, so I will just tell you the few keys you need. Down-arrow and up-arrow move down and up by one line. The space key moves down by one page. And most importantly, typing "q" quits and takes you back to Bash. You just have to memorize this.</p>
|
|
142
|
|
143 <p>Now try entering <code>man man</code>. You don't need all the stuff shown, but you can see what a complicated man page looks like. You can use <code>man</code> to get the documentation of other commands as I discuss them.</p>
|
41
|
144 <%
|
|
145 end
|
|
146 }
|
46
|
147 dirs = {
|
|
148 title = [[Directories]]
|
|
149 content = function()
|
|
150 %>
|
|
151 <p>You should be familiar with Mac Finder or Windows File Explorer, and you should know from this that directories (also called "folders") are organized into a tree.</p>
|
|
152
|
|
153 <p>On Mac:</p>
|
|
154
|
|
155 <code block>
|
|
156 ~ $ pwd
|
|
157 /Users/fschmidt
|
|
158 ~ $ open .
|
|
159 ~ $
|
|
160 </code>
|
|
161
|
|
162 <p>On Windows:</p>
|
|
163
|
|
164 <code block>
|
|
165 ~ $ pwd
|
|
166 /home/fschmidt
|
|
167 ~ $ explorer .
|
|
168 ~ $
|
|
169 </code>
|
|
170
|
49
|
171 <p>When using Bash, you are always in some directory, called the current directory or the working directory. <code>pwd</code> shows you the full path to this directory. Do <code>man pwd</code> for details. <code>open .</code> should open the Mac Finder for the current directory, and <code>explorer .</code> should open the Windows File Explorer for the current directory.<p>
|
46
|
172
|
|
173 <p>Continuing on my Mac:</p>
|
|
174
|
|
175 <code block>
|
|
176 ~ $ mkdir learn
|
|
177 ~ $ cd learn
|
|
178 ~/learn $ pwd
|
|
179 /Users/fschmidt/learn
|
|
180 </code>
|
|
181
|
|
182 <p><code>mkdir</code> makes a directory in the current directory. You should be able to see the created directory in Mac Finder or Windows File Explorer. <code>cd</code> stands for "change directory". This changes the current directory. <code>cd</code> is a built-in command (built into Bash), and <code>man</code> isn't useful with built-in commands, so instead of <code>man cd</code>, try <code>help cd</code>. Continuing...</p>
|
|
183
|
|
184 <code block>
|
|
185 ~/learn $ pwd
|
|
186 /Users/fschmidt/learn
|
|
187 ~/learn $ ls
|
|
188 ~/learn $ touch file1
|
|
189 ~/learn $ ls
|
|
190 file1
|
|
191 ~/learn $ touch file2
|
|
192 ~/learn $ touch file3
|
|
193 ~/learn $ ls
|
|
194 file1 file2 file3
|
|
195 ~/learn $ mkdir dir1
|
|
196 ~/learn $ ls
|
|
197 dir1 file1 file2 file3
|
|
198 ~/learn $ ls -F
|
|
199 dir1/ file1 file2 file3
|
|
200 ~/learn $ ls -a
|
|
201 . .. dir1 file1 file2 file3
|
|
202 ~/learn $ ls -a -F
|
|
203 ./ ../ dir1/ file1 file2 file3
|
|
204 ~/learn $ ls -aF
|
|
205 ./ ../ dir1/ file1 file2 file3
|
|
206 </code>
|
|
207
|
|
208 <p><code>ls</code> lists files and <code>touch</code> creates an empty file. Arguments that start with "-" are options. Do <code>man ls</code> to see what the options I used do. <code>-F</code> appends a "/" to directories, and <code>-a</code> shows files starting with "." which are usually hidden. Options can be combined.</p>
|
|
209
|
|
210 <code block>
|
|
211 ~/learn $ ls file1
|
|
212 file1
|
|
213 ~/learn $ ls qqq
|
|
214 ls: qqq: No such file or directory
|
|
215 ~/learn $ ls file1 qqq file2
|
|
216 ls: qqq: No such file or directory
|
|
217 file1 file2
|
|
218 ~/learn $ ls dir1
|
|
219 ~/learn $ touch dir1/d1file
|
|
220 ~/learn $ ls dir1
|
|
221 d1file
|
|
222 ~/learn $ ls -d dir1
|
|
223 dir1
|
|
224 ~/learn $ ls file1 file2 dir1
|
|
225 file1 file2
|
|
226
|
|
227 dir1:
|
|
228 d1file
|
|
229 ~/learn $ ls -d file1 file2 dir1
|
|
230 dir1 file1 file2
|
|
231 ~/learn $ ls -dF file1 file2 dir1
|
|
232 dir1/ file1 file2
|
|
233 </code>
|
|
234
|
|
235 <p>Without file arguments, <code>ls</code> lists files in the current directory. With file arguments, it lists those files if they exist. If the file is a directory, it will list what is in the directory unless the <code>-d</code> option is used.</p>
|
|
236
|
|
237 <code block>
|
|
238 ~/learn $ ls
|
|
239 dir1 file1 file2 file3
|
|
240 ~/learn $ ls .
|
|
241 dir1 file1 file2 file3
|
|
242 ~/learn $ ls -d .
|
|
243 .
|
|
244 ~/learn $ ls -dF .
|
|
245 ./
|
|
246 ~/learn $ ls ./file1
|
|
247 ./file1
|
|
248 ~/learn $ ls dir1
|
|
249 d1file
|
|
250 ~/learn $ ls ./dir1
|
|
251 d1file
|
|
252 ~/learn $ pwd
|
|
253 /Users/fschmidt/learn
|
|
254 ~/learn $ cd .
|
|
255 ~/learn $ pwd
|
|
256 /Users/fschmidt/learn
|
|
257 </code>
|
|
258
|
|
259 <p><code>.</code> is the current directory.</p>
|
|
260
|
|
261 <code block>
|
|
262 ~/learn $ pwd
|
|
263 /Users/fschmidt/learn
|
|
264 ~/learn $ cd dir1
|
|
265 ~/learn/dir1 $ pwd
|
|
266 /Users/fschmidt/learn/dir1
|
|
267 ~/learn/dir1 $ ls .
|
|
268 d1file
|
|
269 ~/learn/dir1 $ ls ..
|
|
270 dir1 file1 file2 file3
|
|
271 ~/learn/dir1 $ cd ..
|
|
272 ~/learn $ pwd
|
|
273 /Users/fschmidt/learn
|
|
274 ~/learn $ cd dir1
|
|
275 ~/learn/dir1 $ pwd
|
|
276 /Users/fschmidt/learn/dir1
|
|
277 ~/learn/dir1 $ cd ../..
|
|
278 ~ $ pwd
|
|
279 /Users/fschmidt
|
|
280 ~ $ cd learn
|
|
281 ~/learn $ pwd
|
|
282 /Users/fschmidt/learn
|
|
283 </code>
|
|
284
|
|
285 <p><code>..</code> is the parent directory.</p>
|
|
286
|
|
287 <code block>
|
|
288 ~/learn $ echo *
|
|
289 dir1 file1 file2 file3
|
|
290 ~/learn $ echo d*
|
|
291 dir1
|
|
292 ~/learn $ echo f*
|
|
293 file1 file2 file3
|
|
294 ~/learn $ echo *1
|
|
295 dir1 file1
|
|
296 ~/learn $ echo dir1/*
|
|
297 dir1/d1file
|
|
298 ~/learn $ echo */*
|
|
299 dir1/d1file
|
|
300 ~/learn $ echo qqq*
|
|
301 qqq*
|
|
302 </code>
|
|
303
|
|
304 <p><code>*</code> does wildcard matching of files. It is important to understand that Bash does the wildcard matching and then passes the resulting arguments to the command. <code>echo</code> never sees the "*" unless there is no match.</p>
|
|
305
|
|
306 <code block>
|
|
307 ~/learn $ ls *
|
|
308 file1 file2 file3
|
|
309
|
|
310 dir1:
|
|
311 d1file
|
|
312 ~/learn $ ls -dF *
|
|
313 dir1/ file1 file2 file3
|
|
314 ~/learn $ ls -dF d*
|
|
315 dir1/
|
|
316 ~/learn $ ls -dF f*
|
|
317 file1 file2 file3
|
|
318 ~/learn $ ls -dF *1
|
|
319 dir1/ file1
|
|
320 ~/learn $ ls dir1/*
|
|
321 dir1/d1file
|
|
322 ~/learn $ ls */*
|
|
323 dir1/d1file
|
|
324 ~/learn $ ls -dF qqq*
|
|
325 ls: qqq*: No such file or directory
|
|
326 </code>
|
|
327
|
|
328 <p>Should be self-explanatory.</p>
|
|
329
|
|
330 <code block>
|
|
331 ~/learn $ pwd
|
|
332 /Users/fschmidt/learn
|
|
333 ~/learn $ cd ~
|
|
334 ~ $ pwd
|
|
335 /Users/fschmidt
|
|
336 ~ $ cd learn/dir1
|
|
337 ~/learn/dir1 $ pwd
|
|
338 /Users/fschmidt/learn/dir1
|
|
339 ~/learn/dir1 $ cd
|
|
340 ~ $ pwd
|
|
341 /Users/fschmidt
|
49
|
342 ~ $ cd ~/learn
|
46
|
343 ~/learn $ pwd
|
|
344 /Users/fschmidt/learn
|
|
345 ~/learn $ echo ~
|
|
346 /Users/fschmidt
|
|
347 ~/learn $ echo .
|
|
348 .
|
|
349 ~/learn $ echo ..
|
|
350 ..
|
|
351 </code>
|
|
352
|
|
353 <p><code>~</code> means your home directory. <code>cd</code> without arguments is the same as <code>cd ~</code>. <code>~</code> is expanded into your home directory by Bash.</p>
|
|
354
|
|
355 <code block>
|
|
356 ~/learn $ ls -ltF
|
|
357 total 0
|
|
358 drwxr-xr-x 3 fschmidt staff 96 Jan 5 02:33 dir1/
|
|
359 -rw-r--r-- 1 fschmidt staff 0 Jan 5 02:21 file3
|
|
360 -rw-r--r-- 1 fschmidt staff 0 Jan 5 02:21 file2
|
|
361 -rw-r--r-- 1 fschmidt staff 0 Jan 5 02:21 file1
|
|
362 </code>
|
|
363
|
|
364 <p><code>-l</code> gives you this ugly techy format. You get the date that the file was last modified. Before the date is the file size. <code>-t</code> sorts by date descending.</p>
|
|
365
|
|
366 <p>Lastly I will describe autocompletion. I type <code>echo d</code> without enter/return but instead then press the tab key. It autocompletes to <code>echo dir1/</code>. I press tab again and it autocompletes to <code>echo dir1/d1file</code>. Pressing tab while entering a file or directory makes Bash try to autocomplete using matching file names. If I enter <code>echo f</code> and press tab, I get <code>echo file</code>. It doesn't know which to choose next. Another tab just beeps. And another tab shows me the options like this:</p>
|
|
367
|
|
368 <code block>
|
|
369 ~/learn $ echo file
|
|
370 file1 file2 file3
|
|
371 ~/learn $ echo file
|
|
372 </code>
|
|
373
|
|
374 <p>In general, you can press tab anytime while entering a file name and see what happens. Autocompletion saves a lot of typing.</p>
|
|
375 <%
|
|
376 end
|
|
377 }
|
|
378 files = {
|
|
379 title = [[Working with Files]]
|
|
380 content = function()
|
|
381 %>
|
|
382 <code block>
|
|
383 ~/learn $ ls -F
|
|
384 dir1/ file1 file2 file3
|
|
385 ~/learn $ cp file1 copied
|
|
386 ~/learn $ ls -F
|
|
387 copied dir1/ file1 file2 file3
|
|
388 ~/learn $ mv copied moved
|
|
389 ~/learn $ ls -F
|
|
390 dir1/ file1 file2 file3 moved
|
|
391 ~/learn $ rm moved
|
|
392 ~/learn $ ls -F
|
|
393 dir1/ file1 file2 file3
|
|
394 </code>
|
|
395
|
|
396 <p><code>cp</code> copies files or directories. <code>mv</code> moves files or directories. <code>rm</code> removes files or directories. See the <code>man</code> pages of these commands for details.</p>
|
|
397
|
|
398 <code block>
|
|
399 ~/learn $ ls -F
|
|
400 dir1/ file1 file2 file3
|
|
401 ~/learn $ mkdir dir2
|
|
402 ~/learn $ touch dir2/d2file
|
|
403 ~/learn $ ls -F
|
|
404 dir1/ dir2/ file1 file2 file3
|
|
405 ~/learn $ ls dir2
|
|
406 d2file
|
|
407 ~/learn $ rm dir2
|
|
408 rm: dir2: is a directory
|
|
409 ~/learn $ rm -d dir2
|
|
410 rm: dir2: Directory not empty
|
|
411 ~/learn $ rm dir2/d2file
|
|
412 ~/learn $ rm -d dir2
|
|
413 ~/learn $ ls -F
|
|
414 dir1/ file1 file2 file3
|
|
415 </code>
|
|
416
|
|
417 <code block>
|
|
418 ~/learn $ ls -F
|
|
419 dir1/ file1 file2 file3
|
|
420 ~/learn $ mkdir dir2
|
|
421 ~/learn $ touch dir2/d2file
|
|
422 ~/learn $ ls -F
|
|
423 dir1/ dir2/ file1 file2 file3
|
47
|
424 ~/learn $ rm -r dir2
|
46
|
425 ~/learn $ ls -F
|
|
426 dir1/ file1 file2 file3
|
|
427 </code>
|
|
428
|
|
429 <code block>
|
|
430 ~/learn $ ls -F
|
|
431 dir1/ file1 file2 file3
|
|
432 ~/learn $ cp dir1 dir2
|
|
433 cp: dir1 is a directory (not copied).
|
47
|
434 ~/learn $ cp -r dir1 dir2
|
46
|
435 ~/learn $ ls -F
|
|
436 dir1/ dir2/ file1 file2 file3
|
|
437 ~/learn $ ls dir2
|
|
438 d1file
|
|
439 ~/learn $ cp f* dir2
|
|
440 ~/learn $ ls dir2
|
|
441 d1file file1 file2 file3
|
47
|
442 ~/learn $ rm -r dir2
|
46
|
443 ~/learn $ ls -F
|
|
444 dir1/ file1 file2 file3
|
|
445 </code>
|
|
446
|
|
447 <code block>
|
|
448 ~/learn $ ls -F
|
|
449 dir1/ file1 file2 file3
|
|
450 ~/learn $ mkdir dir2
|
47
|
451 ~/learn $ cp -r dir1 dir2
|
46
|
452 ~/learn $ ls -F dir2
|
|
453 dir1/
|
|
454 ~/learn $ ls -F dir2/dir1
|
|
455 d1file
|
47
|
456 ~/learn $ rm -r dir2
|
46
|
457 ~/learn $ ls -F
|
|
458 dir1/ file1 file2 file3
|
|
459 </code>
|
|
460
|
|
461 <p>I could explain all this, but I won't. You should learn to understand commands and their options using <code>man</code> and by playing with them. Don't continue until you completely understand the above.</p>
|
|
462 <%
|
|
463 end
|
|
464 }
|
|
465 quote = {
|
|
466 title = [[Quoting]]
|
|
467 content = function()
|
|
468 %>
|
|
469
|
|
470 <code block>
|
|
471 ~/learn $ echo a b
|
|
472 a b
|
|
473 ~/learn $ echo "a b"
|
|
474 a b
|
|
475 ~/learn $ echo 'a b'
|
|
476 a b
|
|
477 ~/learn $ echo "a b" c
|
|
478 a b c
|
|
479 </code>
|
|
480
|
|
481 <p>Bash treats text in quotes as one argument. So in <code>echo a b</code>, <code>echo</code> has two arguments: "a" and "b". In <code>echo "a b"</code>, <code>echo</code> has one argument: "<span pre>a b</span>". In <code>echo 'a b'</code>, <code>echo</code> has one argument: "<span pre>a b</span>". In <code>echo "a b" c</code>, <code>echo</code> has two arguments: "<span pre>a b</span>" and "c".</p>
|
|
482
|
|
483 <code block>
|
|
484 ~/learn $ echo a\ \ \ b
|
|
485 a b
|
|
486 </code>
|
|
487
|
|
488 <p>Outside of quotes, <code>\ </code> is not treated as a separator, but rather is treated as a space character that is part of the argument.</p>
|
|
489
|
|
490 <%
|
|
491 end
|
|
492 }
|
|
493 vars = {
|
|
494 title = [[Variables]]
|
|
495 content = function()
|
|
496 %>
|
|
497
|
|
498 <code block>
|
|
499 ~/learn $ echo $X
|
|
500
|
|
501 ~/learn $ X="some text"
|
|
502 ~/learn $ echo $X
|
|
503 some text
|
|
504 ~/learn $ echo "X is: $X"
|
|
505 X is: some text
|
|
506 ~/learn $ echo 'X is: $X'
|
|
507 X is: $X
|
|
508 ~/learn $ X="$X and more"
|
|
509 ~/learn $ echo $X
|
|
510 some text and more
|
|
511 </code>
|
|
512
|
|
513 <p>Here <code>X</code> is a variable. You get its value with <code>$X</code>. This also works inside double-quotes but not inside single-quotes.</p>
|
47
|
514
|
|
515 <p>There are special variables called environment variables that are used by Bash.</p>
|
|
516
|
|
517 <code block>
|
|
518 ~/learn $ echo $PATH
|
|
519 /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/fschmidt/Dropbox/bin:/Users/fschmidt/hg/luan/scripts:/usr/local/opt/postgresql@9.5/bin
|
|
520 ~/learn $ which ls
|
|
521 /bin/ls
|
|
522 ~/learn $ cd /bin
|
|
523 /bin $ pwd
|
|
524 /bin
|
|
525 /bin $ ls
|
|
526 [ dd launchctl pwd test
|
|
527 bash df link rm unlink
|
|
528 cat echo ln rmdir wait4path
|
|
529 chmod ed ls sh zsh
|
|
530 cp expr mkdir sleep
|
|
531 csh hostname mv stty
|
|
532 dash kill pax sync
|
|
533 date ksh ps tcsh
|
|
534 /bin $ ls -F
|
|
535 [* dd* launchctl* pwd* test*
|
|
536 bash* df* link* rm* unlink*
|
|
537 cat* echo* ln* rmdir* wait4path*
|
|
538 chmod* ed* ls* sh* zsh*
|
|
539 cp* expr* mkdir* sleep*
|
|
540 csh* hostname* mv* stty*
|
|
541 dash* kill* pax* sync*
|
|
542 date* ksh* ps* tcsh*
|
|
543 /bin $ cd ~/learn
|
|
544 ~/learn $
|
|
545 </code>
|
|
546
|
53
|
547 <p><code>PATH</code> is an environment variable containing a list of directories separated by <code>:</code> that are searched for commands by Bash. The <code>which</code> command shows the full path to a command. <code>ls -F</code> appends a <code>*</code> to executable files.</p>
|
47
|
548
|
|
549 <code block>
|
|
550 ~/learn $ subl file1
|
|
551 -bash: subl: command not found
|
|
552 ~/learn $ "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" file1
|
|
553 ~/learn $ PATH="$PATH:/Applications/Sublime Text.app/Contents/SharedSupport/bin"
|
|
554 ~/learn $ echo $PATH
|
|
555 /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/fschmidt/Dropbox/bin:/Users/fschmidt/hg/luan/scripts:/usr/local/opt/postgresql@9.5/bin:/Applications/Sublime Text.app/Contents/SharedSupport/bin
|
|
556 ~/learn $ subl file1
|
|
557 ~/learn $
|
|
558 </code>
|
|
559
|
|
560 <p>Here I edit the file <code>file1</code> with <a href="http://localhost:8080/learn.html#editor">Sublime Text</a>, first by using the full path, and then by adding the directory to <code>PATH</code> so that Bash can find <code>subl</code>.</p>
|
|
561
|
|
562 <p>I have Microsoft Word on Windows. From the Windows Command Prompt (not Bash):</p>
|
|
563
|
|
564 <code block>
|
|
565 C:\Users\fschmidt>winword
|
|
566
|
|
567 C:\Users\fschmidt>where winword
|
|
568 C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE
|
|
569 </code>
|
|
570
|
|
571 <p><code>winword</code> runs Microsoft Word. The Command Prompt <code>where</code> command is like the Bash <code>which</code> command. So now on MSYS2:</p>
|
|
572
|
|
573 <code block>
|
|
574 ~ $ winword
|
|
575 bash: winword: command not found
|
|
576 ~ $ echo $PATH
|
|
577 /usr/local/bin:/usr/bin:/bin:/opt/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/c/Program Files/TortoiseHg:/c/Program Files/Java/jdk1.8.0_202/bin
|
|
578 ~ $ PATH="$PATH:/c/Program Files/Microsoft Office/root/Office16"
|
|
579 ~ $ echo $PATH
|
|
580 /usr/local/bin:/usr/bin:/bin:/opt/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/c/Program Files/TortoiseHg:/c/Program Files/Java/jdk1.8.0_202/bin:/c/Program Files/Microsoft Office/root/Office16
|
|
581 ~ $ winword
|
|
582 ~ $
|
|
583 </code>
|
|
584
|
|
585 <p>Returning to the Mac, there is another way to run applications found in Finder's "Applications" simply as applications instead of as commands.</p>
|
|
586
|
|
587 <code block>
|
|
588 ~/learn $ open -a 'Sublime Text' file1
|
|
589 </code>
|
|
590
|
|
591 <p>Another useful environment variable is <code>PS1</code> which controls the command prompt. I already have this set up, but if I didn't:</p>
|
|
592
|
|
593 <code block>
|
|
594 Franklins-MacBook-Pro:learn fschmidt$ echo $PS1
|
|
595 \h:\W \u\$
|
|
596 Franklins-MacBook-Pro:learn fschmidt$ PS1="\w $ "
|
|
597 ~/learn $ echo $PS1
|
|
598 \w $
|
|
599 ~/learn $
|
|
600 </code>
|
|
601
|
|
602 <p>Google "bash PS1" for more info.</p>
|
|
603
|
46
|
604 <%
|
|
605 end
|
|
606 }
|
|
607 bash_profile = {
|
|
608 title = [[.bash_profile]]
|
|
609 content = function()
|
|
610 %>
|
47
|
611 <code block>
|
|
612 ~/learn $ cd
|
|
613 ~ $ ls .bash_profile
|
|
614 .bash_profile
|
|
615 </code>
|
|
616
|
|
617 <p>If <code>.bash_profile</code> isn't found then do <code>touch .bash_profile</code> to create it. This file contains Bash commands that are run when Bash starts. If you already have this file, it is likely to contain comments that start with <code>#</code>. Comments are ignored like this:</p>
|
|
618
|
|
619 <code block>
|
|
620 ~ $ # comment line, does nothing
|
|
621 ~ $ echo whatever # end of line comment
|
|
622 whatever
|
|
623 ~ $
|
|
624 </code>
|
|
625
|
|
626 <p>To edit <code>.bash_profile</code> on a Mac, you can do:</p>
|
|
627
|
|
628 <code block>
|
|
629 ~ $ open -a 'Sublime Text' .bash_profile
|
|
630 </code>
|
|
631
|
|
632 <p>To edit <code>.bash_profile</code> on Windows, you can do:</p>
|
|
633
|
|
634 <code block>
|
|
635 ~ $ notepad .bash_profile
|
|
636 </code>
|
|
637
|
|
638 <p>Now try adding this line to <code>.bash_profile</code>:</p>
|
|
639
|
|
640 <code block>
|
|
641 echo hello there
|
|
642 </code>
|
|
643
|
|
644 <p>Now when you open a new Bash terminal, you should see "hello there". <code>.bash_profile</code> runs when Bash is started by opening a new Bash terminal.</p>
|
|
645
|
|
646 <p>I set <code>PS1</code> and <code>PATH</code> in <code>.bash_profile</code> to have the command prompt I want, and access to the commands that I want. I suggest that you make the <a href="https://www.sublimetext.com/docs/command_line.html">Sublime Text command</a> <code>subl</code> available in <code>PATH</code>.</p>
|
|
647
|
|
648 <%
|
|
649 end
|
|
650 }
|
48
|
651 find = {
|
|
652 title = [[The "find" Command]]
|
|
653 content = function()
|
|
654 %>
|
|
655 <code block>
|
|
656 ~/learn $ find .
|
|
657 .
|
|
658 ./file3
|
|
659 ./file2
|
|
660 ./file1
|
|
661 ./dir1
|
|
662 ./dir1/d1file
|
|
663 ~/learn $ find . -name 'file*'
|
|
664 ./file3
|
|
665 ./file2
|
|
666 ./file1
|
|
667 ~/learn $ find . -name '*file'
|
|
668 ./dir1/d1file
|
|
669 ~/learn $ find . -name 'd*'
|
|
670 ./dir1
|
|
671 ./dir1/d1file
|
|
672 ~/learn $ find . -name '*1' -or -name '*2'
|
|
673 ./file2
|
|
674 ./file1
|
|
675 ./dir1
|
|
676 </code>
|
|
677
|
|
678 <p><code>find</code> recursively searches for files in a directory tree. Note that in this case the <code>*</code> wildcard matching is not being done by Bash, it is being done by <code>find</code>. <code>find</code> has many options for searching for files and acting on them, see <code>man find</code>.</p>
|
|
679 <%
|
|
680 end
|
|
681 }
|
|
682 io = {
|
|
683 title = [[Input and Output]]
|
|
684 content = function()
|
|
685 %>
|
|
686 <code block>
|
|
687 ~/learn $ echo 'this is a test' >test.txt
|
|
688 ~/learn $ ls -F
|
|
689 dir1/ file1 file2 file3 test.txt
|
|
690 ~/learn $ cat test.txt
|
|
691 this is a test
|
|
692 ~/learn $ echo 'this is another test' >test.txt
|
|
693 ~/learn $ cat test.txt
|
|
694 this is another test
|
|
695 ~/learn $ echo 'another line' >>test.txt
|
|
696 ~/learn $ cat test.txt
|
|
697 this is another test
|
|
698 another line
|
49
|
699 ~/learn $ cat <test.txt
|
|
700 this is another test
|
|
701 another line
|
|
702 ~/learn $ cat <<End >test.txt
|
|
703 > I am typing this
|
|
704 > and this
|
|
705 > End
|
|
706 ~/learn $ cat test.txt
|
|
707 I am typing this
|
|
708 and this
|
|
709 ~/learn $ (echo one; echo two) >test.txt
|
|
710 ~/learn $ cat test.txt
|
|
711 one
|
|
712 two
|
|
713 </code>
|
|
714
|
|
715 <p>All programs have standard input, standard output, and standard error. Programs write normal output to standard output and error messages to standard error. By default, standard input comes from the terminal, and standard output and standard error go to the terminal, but this can be changed. <code>>file</code> sends standard output to <code>file</code>. <code>>>file</code> appends standard output to <code>file</code>. <code><file</code> reads standard input from <code>file</code>. <code><<whatever</code> reads standard input from the text that follows until a line with just <code>whatever</code>. Commands can be combined between <code>(</code> and <code>)</code>. Be sure to <code>man cat</code> to understand how <code>cat</code> works.</p>
|
|
716
|
|
717 <code block>
|
48
|
718 ~/learn $ ls >ls.txt
|
|
719 ~/learn $ cat ls.txt
|
|
720 dir1
|
|
721 file1
|
|
722 file2
|
|
723 file3
|
|
724 ls.txt
|
|
725 test.txt
|
|
726 ~/learn $ ls -d f* q* >ls.txt
|
|
727 ls: q*: No such file or directory
|
|
728 ~/learn $ cat ls.txt
|
|
729 file1
|
|
730 file2
|
|
731 file3
|
|
732 ~/learn $ ls -d f* q* 2>ls.txt
|
|
733 file1 file2 file3
|
|
734 ~/learn $ cat ls.txt
|
|
735 ls: q*: No such file or directory
|
|
736 ~/learn $ ls -d f* q* | tee ls.txt
|
|
737 ls: q*: No such file or directory
|
|
738 file1
|
|
739 file2
|
|
740 file3
|
|
741 ~/learn $ cat ls.txt
|
|
742 file1
|
|
743 file2
|
|
744 file3
|
|
745 ~/learn $ ls -d f* q* 2>&1 | tee ls.txt
|
|
746 ls: q*: No such file or directory
|
|
747 file1
|
|
748 file2
|
|
749 file3
|
|
750 ~/learn $ cat ls.txt
|
|
751 ls: q*: No such file or directory
|
|
752 file1
|
|
753 file2
|
|
754 file3
|
|
755 </code>
|
|
756
|
49
|
757 <p><code>2>file</code> sends standard error to <code>file</code>. <code>|</code> sends standard output of the previous command to standard input of the following command. <code>2>&1</code> sends standard error to standard output. <code>tee file</code> reads standard input and then writes it to both standard output and to <code>file</code>.</p>
|
48
|
758
|
|
759 <code block>
|
|
760 ~/learn $ find . -type f | wc -l
|
|
761 6
|
|
762 </code>
|
|
763
|
|
764 <p>There are 6 files in <code>learn</code>. Use <code>man</code> to figure out how this works.</p>
|
|
765 <%
|
|
766 end
|
|
767 }
|
49
|
768 ctrl = {
|
|
769 title = [[Control Keys]]
|
|
770 content = function()
|
|
771 %>
|
|
772 <code block>
|
|
773 ~/learn $ sleep 3
|
|
774 ~/learn $ sleep 30
|
|
775 ^C
|
|
776 ~/learn $
|
|
777 </code>
|
|
778
|
|
779 <p><code>sleep 3</code> sleeps for 3 seconds, meaning it does nothing for 3 seconds. I waited 3 seconds for this command to finish. Then I ran <code>sleep 30</code> which would sleep for 30 seconds, but I lost my patience and pressed control+c which interrupts the program and breaks out of it. You can try control+c if you ever get stuck waiting for a command to finish.</p>
|
|
780
|
|
781 <code block>
|
|
782 ~/learn $ wc
|
|
783 I am typing this
|
|
784 and this
|
|
785 now I will end my input with control+d
|
|
786 3 14 65
|
|
787 ~/learn $ wc
|
|
788 this time I will use control+c to break out
|
|
789 ^C
|
|
790 ~/learn $
|
|
791 </code>
|
|
792
|
|
793 <p>Control+d means end of input.</p>
|
|
794 <%
|
|
795 end
|
|
796 }
|
48
|
797 subst = {
|
|
798 title = [[Command Substitution]]
|
|
799 content = function()
|
|
800 %>
|
|
801 <code block>
|
|
802 ~/learn $ echo I am in $(pwd)
|
|
803 I am in /Users/fschmidt/learn
|
|
804 ~/learn $ echo this directory contains: $(ls)
|
|
805 this directory contains: dir1 file1 file2 file3 ls.txt test.txt
|
|
806 ~/learn $ echo this directory contains $(ls | wc -l) files
|
|
807 this directory contains 6 files
|
|
808 </code>
|
|
809
|
|
810 <p><code>cmd $(commands)</code> will use the output of <code>commands</code> as argument text for <code>cmd</code>.</p>
|
|
811
|
|
812 <code block>
|
|
813 ~/learn $ cat $(find . -type f) | wc -c
|
|
814 86
|
|
815 </code>
|
|
816
|
|
817 <p>The files in <code>learn</code> contain a total of 86 bytes. Use <code>man</code> to figure out how this works.</p>
|
|
818 <%
|
|
819 end
|
|
820 }
|
49
|
821 ampersand = {
|
|
822 title = [[Ampersand]]
|
|
823 content = function()
|
|
824 %>
|
|
825 <code block>
|
|
826 ~/learn $ (sleep 5; echo done) &
|
|
827 [1] 10080
|
|
828 ~/learn $ echo waiting
|
|
829 waiting
|
|
830 ~/learn $ done
|
|
831
|
|
832 [1]+ Done ( sleep 5; echo done )
|
|
833 ~/learn $
|
|
834 </code>
|
|
835
|
|
836 <p>Normally Bash waits for a command to complete before showing the command prompt and allowing input. But ending a command line with <code>&</code> tells bash not to wait, but instead to run the command in a separate process. Above in <code>~/learn $ echo waiting</code>, I typed in <code>echo waiting</code>. But in <code>~/learn $ done</code>, I did not type <code>done</code>. Instead this was produced by <code>echo done</code> after 5 seconds. <code>[1] 10080</code> tells me that a process was started and <code>[1]+ Done ( sleep 5; echo done )</code> tells me that the process finished.</p>
|
|
837
|
|
838 <p>This is useful where you do not want to wait for a command to finish. Consider this on Windows:</p>
|
|
839
|
|
840 <code block>
|
|
841 ~ $ notepad
|
|
842 </code>
|
|
843
|
|
844 <p>Here you will not get a command prompt again until you quit Notepad because Bash is waiting for this command to finish. So instead do:
|
|
845
|
|
846 <code block>
|
|
847 ~ $ notepad &
|
|
848 [1] 2010
|
|
849 ~ $
|
|
850 </code>
|
|
851
|
|
852 <p>Now Notepad will run and you can continue using Bash.</p>
|
|
853 <%
|
|
854 end
|
|
855 }
|
|
856 scripts = {
|
|
857 title = [[Shell Scripts]]
|
41
|
858 content = function()
|
|
859 %>
|
49
|
860 <p>Make a file called <code>test.sh</code> containing the following:</p>
|
|
861
|
|
862 <code block>
|
|
863 echo this is a shell script
|
|
864 </code>
|
|
865
|
|
866 <p>Now from Bash:</p>
|
|
867
|
|
868 <code block>
|
|
869 ~/learn $ cat test.sh
|
|
870 echo this is a shell script
|
|
871 ~/learn $ ./test.sh
|
|
872 -bash: ./test.sh: Permission denied
|
|
873 ~/learn $ ls -F test.sh
|
|
874 test.sh
|
|
875 ~/learn $ chmod +x test.sh
|
|
876 ~/learn $ ls -F test.sh
|
|
877 test.sh*
|
|
878 ~/learn $ ./test.sh
|
|
879 this is a shell script
|
|
880 ~/learn $
|
|
881 </code>
|
|
882
|
|
883 <p><code>chmod +x file</code> makes <code>file</code> into an executable that can be run. Now I will edit <code>test.sh</code></p>
|
|
884
|
|
885 <code block>
|
|
886 ~/learn $ # edit test.sh
|
|
887 ~/learn $ cat test.sh
|
|
888 nonsense
|
|
889 echo this is a shell script
|
|
890 ~/learn $ ./test.sh
|
|
891 ./test.sh: line 1: nonsense: command not found
|
|
892 this is a shell script
|
|
893 ~/learn $ # edit test.sh
|
|
894 ~/learn $ cat test.sh
|
|
895 set -e
|
|
896 nonsense
|
|
897 echo this is a shell script
|
|
898 ~/learn $ ./test.sh
|
|
899 ./test.sh: line 2: nonsense: command not found
|
|
900 ~/learn $
|
|
901 </code>
|
|
902
|
|
903 <p>By default, scripts continue running after an error. In longer scripts, we want the script to exit after an error. <code>set -e</code> does this, see <code>help set</code>.</p>
|
|
904
|
|
905 <code block>
|
|
906 ~/learn $ X=some
|
|
907 ~/learn $ echo $X
|
|
908 some
|
|
909 ~/learn $ echo $Xthing
|
|
910
|
|
911 ~/learn $ echo ${X}thing
|
|
912 something
|
|
913 ~/learn $ # edit test.sh
|
|
914 ~/learn $ cat test.sh
|
|
915 echo "\$* = $*"
|
|
916 echo "\$# = $#"
|
|
917 echo "\$0 = $0"
|
|
918 echo "\$1 = $1"
|
|
919 echo "\$2 = $2"
|
|
920 echo "\$3 = $3"
|
|
921 echo "\$4 = $4"
|
|
922 echo "\$14 = $14"
|
|
923 echo "\${14} = ${14}"
|
|
924 echo "\$@ = $@"
|
|
925 ./count.sh "$*"
|
|
926 ./count.sh "$@"
|
|
927 ~/learn $ ./test.sh a b "c d"
|
|
928 $* = a b c d
|
|
929 $# = 3
|
|
930 $0 = ./test.sh
|
|
931 $1 = a
|
|
932 $2 = b
|
|
933 $3 = c d
|
|
934 $4 =
|
|
935 $14 = a4
|
|
936 ${14} =
|
|
937 $@ = a b c d
|
|
938 1
|
|
939 3
|
|
940 ~/learn $ cat count.sh
|
|
941 echo $#
|
|
942 ~/learn $
|
|
943 </code>
|
|
944
|
|
945 <p>Bash scripts have special defined variables. The difference between <code>$*</code> and <code>$@</code> is subtle, and you will usually just use <code>$*</code>. <code>$*</code> returns all arguments as one string while <code>$@</code> returns the arguments separately, but this distinction rarely makes any difference.</p>
|
|
946 <%
|
|
947 end
|
|
948 }
|
|
949 vars_and_scripts = {
|
|
950 title = [[Variables and Scripts]]
|
|
951 content = function()
|
|
952 %>
|
|
953 <code block>
|
|
954 ~/learn $ X=value
|
|
955 ~/learn $ echo $X
|
|
956 value
|
|
957 ~/learn $ # edit test.sh
|
|
958 ~/learn $ cat test.sh
|
|
959 echo "\$X = $X"
|
|
960 ~/learn $ ./test.sh
|
|
961 $X =
|
|
962 ~/learn $ export X
|
|
963 ~/learn $ ./test.sh
|
|
964 $X = value
|
|
965 </code>
|
|
966
|
|
967 <p>Variables are defined in the current shell. Shell scripts are run in their own shell. So by default, they don't see variables defined in the terminal/parent shell. <code>export var</code> makes <code>var</code> available in descendant processes, meaning available in shell scripts. It is a good idea to do <code>export PATH</code> in <code>.bash_profile</code> so that your PATH is available to your scripts.</p>
|
|
968
|
|
969 <code block>
|
|
970 ~/learn $ X=terminal
|
|
971 ~/learn $ echo $X
|
|
972 terminal
|
|
973 ~/learn $ # edit test.sh
|
|
974 ~/learn $ cat test.sh
|
|
975 X=script
|
|
976 export X
|
|
977 ~/learn $ ./test.sh
|
|
978 ~/learn $ echo $X
|
|
979 terminal
|
|
980 ~/learn $ . test.sh
|
|
981 ~/learn $ echo $X
|
|
982 script
|
|
983 </code>
|
|
984
|
|
985 <p>You can export a variable from parent to children but not from children to parent. <code>. script</code> includes the text in the file <code>script</code> in the current shell. In this case, it is not run in a separate shell. This is the only way to have a script set variables in your terminal shell.</p>
|
|
986
|
|
987 <code block>
|
|
988 ~/learn $ pwd
|
|
989 /Users/fschmidt/learn
|
|
990 ~/learn $ # edit test.sh
|
|
991 ~/learn $ cat test.sh
|
|
992 cd ~
|
|
993 ~/learn $ ./test.sh
|
|
994 ~/learn $ pwd
|
|
995 /Users/fschmidt/learn
|
|
996 ~/learn $ . test.sh
|
|
997 ~ $ pwd
|
|
998 /Users/fschmidt
|
|
999 ~ $ cd learn
|
|
1000 ~/learn $
|
|
1001 </code>
|
|
1002
|
|
1003 <p>This illustrates the difference between <code>./script</code> and <code>. script</code>.</p>
|
|
1004
|
|
1005 <%
|
|
1006 end
|
|
1007 }
|
|
1008 your_scripts = {
|
|
1009 title = [[Your Scripts]]
|
|
1010 content = function()
|
|
1011 %>
|
|
1012 <code block>
|
|
1013 ~/learn $ echo $PATH
|
|
1014 /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/fschmidt/Dropbox/bin:/Users/fschmidt/hg/luan/scripts:/usr/local/opt/postgresql@9.5/bin:/Applications/Sublime Text.app/Contents/SharedSupport/bin
|
|
1015 ~/learn $ echo ~/Dropbox/bin
|
|
1016 /Users/fschmidt/Dropbox/bin
|
|
1017 ~/learn $ ls -F ~/Dropbox/bin/e
|
|
1018 /Users/fschmidt/Dropbox/bin/e*
|
|
1019 ~/learn $ cat ~/Dropbox/bin/e
|
|
1020 open -a 'Sublime Text' $*
|
|
1021 ~/learn $ e test.sh
|
|
1022 ~/learn $
|
|
1023 </code>
|
|
1024
|
53
|
1025 <p>When you write useful scripts, put them in a directory and add that directory to your PATH. I use <code>~/Dropbox/bin</code> and I have a script named <code>e</code> in that directory for editing files. So <code>e test.sh</code> lets me edit <code>test.sh</code> from the command line.</p>
|
49
|
1026 <%
|
|
1027 end
|
|
1028 }
|
|
1029 advanced = {
|
|
1030 title = [[Advanced Scripting]]
|
|
1031 content = function()
|
|
1032 %>
|
|
1033 <p>Here is a more advanced script called <code>undocx.sh</code> that unpacks a Word DOCX file.</p>
|
|
1034
|
|
1035 <code block>
|
|
1036 #!/bin/bash
|
|
1037
|
|
1038 set -e
|
|
1039
|
|
1040 if [ $# -ne 1 ]; then
|
|
1041 echo "usage: $0 filename"
|
|
1042 exit 1
|
|
1043 fi
|
|
1044
|
|
1045 FILE="$1"
|
|
1046 NEWDIR=$(basename $FILE .docx)
|
|
1047
|
|
1048 mkdir $NEWDIR
|
|
1049 unzip $FILE -d $NEWDIR
|
|
1050
|
|
1051 export XMLLINT_INDENT=$'\t'
|
|
1052 for file in $(find $NEWDIR -name "*.xml" -o -name "*.rels"); do
|
|
1053 mv "$file" temp.xml
|
|
1054 xmllint --format temp.xml >"$file"
|
|
1055 done
|
|
1056 rm temp.xml
|
|
1057 </code>
|
|
1058
|
|
1059 <p>Bash is a full programming language containing all the usual features. Some commands in my script are well explained by <code>man</code>, but some are not. In particular, the documentation for <code>if</code> and <code>for</code> are poor. In cases like this, I suggest asking ChatGPT like this:
|
|
1060
|
|
1061 <code block>
|
|
1062 Please explain the Bash "if" statement.
|
|
1063 </code>
|
|
1064
|
|
1065 <code block>
|
|
1066 Please explain the Bash "for" statement.
|
|
1067 </code>
|
|
1068
|
|
1069 <p>ChatGPT knows Bash well. I trust ChatGPT to explain details but not to explain core concepts. You can also try Google, but ChatGPT is better than modern programmers.</p>
|
|
1070 <%
|
|
1071 end
|
|
1072 }
|
|
1073 conclusion = {
|
|
1074 title = [[Conclusion]]
|
|
1075 content = function()
|
|
1076 %>
|
|
1077 <p>At least 90% of your usage of Bash will be simple commands that you enter in the terminal. Try to use Bash as much as possible instead of using the GUI so that you get practice using it. Unless you become system administrator, you won't use advanced scripting much. But with a solid understanding of the core basics, you should be able to figure out how to read or write advanced scripts when needed.</p>
|
41
|
1078 <%
|
|
1079 end
|
|
1080 }
|
40
|
1081 }
|
|
1082
|
|
1083
|
|
1084 local function show_toc(content)
|
|
1085 %>
|
|
1086 <ul>
|
|
1087 <%
|
|
1088 for id, info in pairs(content) do
|
|
1089 %>
|
|
1090 <li><a id="c_<%=id%>" href="#<%=id%>"><%=info.title%></a></li>
|
|
1091 <%
|
|
1092 end
|
|
1093 %>
|
|
1094 </ul>
|
|
1095 <%
|
|
1096 end
|
|
1097
|
|
1098 local function show_content(content,h)
|
|
1099 for id, info in pairs(content) do
|
|
1100 %>
|
|
1101 <div heading>
|
|
1102 <h<%=h%>><a id="<%=id%>" href="#<%=id%>"><%=info.title%></a></h<%=h%>>
|
|
1103 <a href="#c_<%=id%>">contents</a>
|
|
1104 </div>
|
|
1105 <%
|
|
1106 info.content()
|
|
1107 end
|
|
1108 end
|
|
1109
|
|
1110 return function()
|
|
1111 Io.stdout = Http.response.text_writer()
|
|
1112 %>
|
|
1113 <!doctype html>
|
|
1114 <html>
|
|
1115 <head>
|
|
1116 <% head() %>
|
45
|
1117 <title>Reactionary Bash Tutorial</title>
|
40
|
1118 </head>
|
|
1119 <body>
|
|
1120 <% header() %>
|
|
1121 <div content>
|
48
|
1122 <h1><a href="learn_bash.html">Reactionary Bash Tutorial</a></h1>
|
40
|
1123 <hr>
|
|
1124 <h2>Contents</h2>
|
|
1125 <div toc>
|
|
1126 <% show_toc(content) %>
|
|
1127 </div>
|
|
1128 <hr>
|
|
1129 <% show_content(content,2) %>
|
|
1130 </div>
|
|
1131 </body>
|
|
1132 </html>
|
|
1133 <%
|
|
1134 end
|