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