comparison src/learn_bash.html.luan @ 45:14518d772090

start learn_bash
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 05 Jan 2024 01:34:32 -0700
parents src/learn.html.luan@5b4d5cf453a8
children 89fdc29b296f
comparison
equal deleted inserted replaced
44:5b4d5cf453a8 45:14518d772090
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 = {
13 title = [[Introduction]]
14 content = function()
15 %>
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 damn thing.</p>
17
18 <p>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>
19 <%
20 end
21 }
22 access = {
23 title = [[Running Bash]]
24 content = function()
25 %>
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>
27 <%
28 end
29 }
30 start = {
31 title = [[Getting Started]]
32 content = function()
33 %>
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 ~ $
43
44 </code>
45
46 <p>On Windows - MSYS2 I just see:</p>
47
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>
56
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
74 <p>The <code>echo</code> command just echos 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>
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>
84 <%
85 end
86 }
87 man = {
88 title = [[The "man" Command]]
89 content = function()
90 %>
91 <p>Enter:</p>
92 <code block>
93 ~ $ man echo
94 </code>
95
96 <p>You should get:</p>
97
98 <code block>
99
100 ECHO(1) BSD General Commands Manual ECHO(1)
101
102 NAME
103 echo -- write arguments to the standard output
104
105 SYNOPSIS
106 echo [-n] [string ...]
107
108 DESCRIPTION
109 The echo utility writes any specified operands, separated by single blank
110 (` ') characters and followed by a newline (`\n') character, to the stan-
111 dard output.
112
113 The following option is available:
114
115 -n Do not print the trailing newline character. This may also be
116 achieved by appending `\c' to the end of the string, as is done by
117 iBCS2 compatible systems. Note that this option as well as the
118 effect of `\c' are implementation-defined in IEEE Std 1003.1-2001
119 (``POSIX.1'') as amended by Cor. 1-2002. Applications aiming for
120 maximum portability are strongly encouraged to use printf(1) to
121 suppress the newline character.
122 :
123 </code>
124
125 <p>But if you are on Windows, you may not have <code>man</code> installed. In that case, do:</p>
126
127 <code block>
128 ~ $ pacman -S man-db
129 </code>
130
131 <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>
132
133 <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>
134
135 <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>
136 <%
137 end
138 }
139 later = {
140 title = [[placeholder]]
141 content = function()
142 %>
143 <p>later</p>
144 <%
145 end
146 }
147 }
148
149
150 local function show_toc(content)
151 %>
152 <ul>
153 <%
154 for id, info in pairs(content) do
155 %>
156 <li><a id="c_<%=id%>" href="#<%=id%>"><%=info.title%></a></li>
157 <%
158 end
159 %>
160 </ul>
161 <%
162 end
163
164 local function show_content(content,h)
165 for id, info in pairs(content) do
166 %>
167 <div heading>
168 <h<%=h%>><a id="<%=id%>" href="#<%=id%>"><%=info.title%></a></h<%=h%>>
169 <a href="#c_<%=id%>">contents</a>
170 </div>
171 <%
172 info.content()
173 end
174 end
175
176 return function()
177 Io.stdout = Http.response.text_writer()
178 %>
179 <!doctype html>
180 <html>
181 <head>
182 <% head() %>
183 <title>Reactionary Bash Tutorial</title>
184 </head>
185 <body>
186 <% header() %>
187 <div content>
188 <h1><a href="learn.html">Reactionary Bash Tutorial</a></h1>
189 <hr>
190 <h2>Contents</h2>
191 <div toc>
192 <% show_toc(content) %>
193 </div>
194 <hr>
195 <% show_content(content,2) %>
196 </div>
197 </body>
198 </html>
199 <%
200 end