view 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
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local pairs = Luan.pairs or error()
local Io = require "luan:Io.luan"
local Http = require "luan:http/Http.luan"
local Shared = require "site:/lib/Shared.luan"
local head = Shared.head or error()
local header = Shared.header or error()


local content = {
	intro = {
		title = [[Introduction]]
		content = function()
%>
<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>

<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>
<%
		end
	}
	access = {
		title = [[Running Bash]]
		content = function()
%>
<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>
<%
		end
	}
	start = {
		title = [[Getting Started]]
		content = function()
%>
<p>When I start Bash on my Mac I see:</p>

<code block>
Last login: Thu Jan  4 23:25:35 on ttys004

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
~ $ 

</code>

<p>On Windows - MSYS2 I just see:</p>

<code block>
~ $ 

</code>

<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>

<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>

<code block>
~ $ qqq
-bash: qqq: command not found
~ $ 
</code>

<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>

<code block>
~ $ echo hi
hi
~ $ echo how are you
how are you
~ $ echo bye
bye
</code>

<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>

<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>

<code block>
~ $ echo how  are         you
how are you
</code>

<p><code>echo</code> just returns the arguments separated by one space.</p>
<%
		end
	}
	man = {
		title = [[The "man" Command]]
		content = function()
%>
<p>Enter:</p>
<code block>
~ $ man echo
</code>

<p>You should get:</p>

<code block>

ECHO(1)                   BSD General Commands Manual                  ECHO(1)

NAME
     echo -- write arguments to the standard output

SYNOPSIS
     echo [-n] [string ...]

DESCRIPTION
     The echo utility writes any specified operands, separated by single blank
     (` ') characters and followed by a newline (`\n') character, to the stan-
     dard output.

     The following option is available:

     -n    Do not print the trailing newline character.  This may also be
           achieved by appending `\c' to the end of the string, as is done by
           iBCS2 compatible systems.  Note that this option as well as the
           effect of `\c' are implementation-defined in IEEE Std 1003.1-2001
           (``POSIX.1'') as amended by Cor. 1-2002.  Applications aiming for
           maximum portability are strongly encouraged to use printf(1) to
           suppress the newline character.
:
</code>

<p>But if you are on Windows, you may not have <code>man</code> installed.  In that case, do:</p>

<code block>
~ $ pacman -S man-db
</code>

<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>

<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>

<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>
<%
		end
	}
	later = {
		title = [[placeholder]]
		content = function()
%>
<p>later</p>
<%
		end
	}
}


local function show_toc(content)
%>
			<ul>
<%
	for id, info in pairs(content) do
%>
				<li><a id="c_<%=id%>" href="#<%=id%>"><%=info.title%></a></li>
<%
	end
%>
			</ul>
<%
end

local function show_content(content,h)
	for id, info in pairs(content) do
%>
			<div heading>
				<h<%=h%>><a id="<%=id%>" href="#<%=id%>"><%=info.title%></a></h<%=h%>>
				<a href="#c_<%=id%>">contents</a>
			</div>
<%
		info.content()
	end
end

return function()
	Io.stdout = Http.response.text_writer()
%>
<!doctype html>
<html>
	<head>
<%		head() %>
		<title>Reactionary Bash Tutorial</title>
	</head>
	<body>
<%		header() %>
		<div content>
			<h1><a href="learn.html">Reactionary Bash Tutorial</a></h1>
			<hr>
			<h2>Contents</h2>
			<div toc>
<%			show_toc(content) %>
			</div>
			<hr>
<%			show_content(content,2) %>
		</div>
	</body>
</html>
<%
end