mirror of
https://github.com/TrudeEH/web.git
synced 2025-12-06 00:13:36 +00:00
Continued work on bash and linux
This commit is contained in:
18
content/.obsidian/workspace.json
vendored
18
content/.obsidian/workspace.json
vendored
@@ -13,21 +13,21 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "markdown",
|
"type": "markdown",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "drafts/bash.md",
|
"file": "drafts/linux/index.md",
|
||||||
"mode": "source",
|
"mode": "source",
|
||||||
"source": false
|
"source": false
|
||||||
},
|
},
|
||||||
"icon": "lucide-file",
|
"icon": "lucide-file",
|
||||||
"title": "bash"
|
"title": "index"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "2fdb7f37153cc3f1",
|
"id": "1b262eef76a0e8e4",
|
||||||
"type": "leaf",
|
"type": "leaf",
|
||||||
"state": {
|
"state": {
|
||||||
"type": "markdown",
|
"type": "markdown",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "drafts/linux/index.md",
|
"file": "notes/index/index.md",
|
||||||
"mode": "source",
|
"mode": "source",
|
||||||
"source": false
|
"source": false
|
||||||
},
|
},
|
||||||
@@ -155,13 +155,13 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "outline",
|
"type": "outline",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "drafts/bash.md",
|
"file": "drafts/linux/index.md",
|
||||||
"followCursor": false,
|
"followCursor": false,
|
||||||
"showSearch": false,
|
"showSearch": false,
|
||||||
"searchQuery": ""
|
"searchQuery": ""
|
||||||
},
|
},
|
||||||
"icon": "lucide-list",
|
"icon": "lucide-list",
|
||||||
"title": "Outline of bash"
|
"title": "Outline of index"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -195,10 +195,10 @@
|
|||||||
},
|
},
|
||||||
"active": "c01600dff17cd347",
|
"active": "c01600dff17cd347",
|
||||||
"lastOpenFiles": [
|
"lastOpenFiles": [
|
||||||
"notes/c-language.md",
|
|
||||||
"drafts/bash.md",
|
|
||||||
"drafts/linux/index.md",
|
|
||||||
"notes/index/index.md",
|
"notes/index/index.md",
|
||||||
|
"drafts/linux/index.md",
|
||||||
|
"drafts/bash.md",
|
||||||
|
"notes/c-language.md",
|
||||||
"drafts/linux-encrypt/index.md",
|
"drafts/linux-encrypt/index.md",
|
||||||
"drafts/linux/image7.png",
|
"drafts/linux/image7.png",
|
||||||
"drafts/linux/image6.png",
|
"drafts/linux/image6.png",
|
||||||
|
|||||||
@@ -594,4 +594,84 @@ echo "Remaining arguments: $@"
|
|||||||
|
|
||||||
## Bash Startup Files
|
## Bash Startup Files
|
||||||
|
|
||||||
6.2 Bash Startup Files https://www.gnu.org/software/bash/manual/bash.html#What-is-Bash_003f
|
| Shell Type | Files |
|
||||||
|
| --------------- | ---------------------------------------------------------------- |
|
||||||
|
| Login | `/etc/profile`, `~/.bash_profile`, `~/.bash_login`, `~/.profile` |
|
||||||
|
| Interactive | `~/.bashrc` |
|
||||||
|
| Non-Interactive | `$BASH_ENV` |
|
||||||
|
| `sh` | `/etc/profile`, `~/.profile` |
|
||||||
|
|
||||||
|
## Detect if Running in Interactive Shell
|
||||||
|
|
||||||
|
```bash
|
||||||
|
case "$-" in
|
||||||
|
*i*) echo This shell is interactive ;;
|
||||||
|
*) echo This shell is not interactive ;;
|
||||||
|
esac
|
||||||
|
```
|
||||||
|
|
||||||
|
## Conditional Expressions
|
||||||
|
|
||||||
|
Conditional Expressions are used by the `[[]]`, `[]` and `test` commands.
|
||||||
|
|
||||||
|
- `-a file` True if file exists.
|
||||||
|
- `-b file` True if file exists and is a block special file.
|
||||||
|
- `-c file` True if file exists and is a character special file.
|
||||||
|
- `-d file` True if file exists and is a directory.
|
||||||
|
- `-e file` True if file exists. (Equivalent to `-a` in this context)
|
||||||
|
- `-f file` True if file exists and is a regular file.
|
||||||
|
- `-g file` True if file exists and its set-group-id bit is set.
|
||||||
|
- `-h file` True if file exists and is a symbolic link.
|
||||||
|
- `-k file` True if file exists and its "sticky" bit is set.
|
||||||
|
- `-p file` True if file exists and is a named pipe (FIFO).
|
||||||
|
- `-r file` True if file exists and is readable.
|
||||||
|
- `-s file` True if file exists and has a size greater than zero.
|
||||||
|
- `-t fd` True if file descriptor fd is open and refers to a terminal.
|
||||||
|
- `-u file` True if file exists and its set-user-id bit is set.
|
||||||
|
- `-w file` True if file exists and is writable.
|
||||||
|
- `-x file` True if file exists and is executable.
|
||||||
|
- `-G file` True if file exists and is owned by the effective group id.
|
||||||
|
- `-L file` True if file exists and is a symbolic link.
|
||||||
|
- `-N file` True if file exists and has been modified since it was last read.
|
||||||
|
- `-O file` True if file exists and is owned by the effective user id.
|
||||||
|
- `-S file` True if file exists and is a socket.
|
||||||
|
- `file1 -ef file2` True if file1 and file2 refer to the same device and inode numbers.
|
||||||
|
- `file1 -nt file2` True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.
|
||||||
|
- `file1 -ot file2` True if file1 is older than file2, or if file2 exists and file1 does not.
|
||||||
|
- `-o optname` True if the shell option optname is enabled.
|
||||||
|
- `-v varname` True if the shell variable varname is set (has been assigned a value).
|
||||||
|
- `-R varname` True if the shell variable varname is set and is a name reference.
|
||||||
|
- `-z string` True if the length of string is zero.
|
||||||
|
- `-n string` or `string` True if the length of string is non-zero.
|
||||||
|
- `string1 == string2` or `string1 = string2` True if the strings are equal. Use `=` for the `test` command.
|
||||||
|
- `string1 != string2` True if the strings are not equal.
|
||||||
|
- `string1 < string2` True if string1 sorts before string2 lexicographically.
|
||||||
|
- `string1 > string2` True if string1 sorts after string2 lexicographically.
|
||||||
|
- `arg1 OP arg2`
|
||||||
|
- `OP` is one of '-eq', '-ne', '-lt', '-le', '-gt', or '-ge'. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively.
|
||||||
|
|
||||||
|
## Shell Arithmetic
|
||||||
|
|
||||||
|
Arithmetic is performed using `(())`, `let` and `declare -i`.
|
||||||
|
|
||||||
|
- `id++` `id--` Variable post-increment and post-decrement
|
||||||
|
- `++id` `--id` Variable pre-increment and pre-decrement
|
||||||
|
- `-` `+` Unary minus and plus
|
||||||
|
- `!` `~` Logical and bitwise negation
|
||||||
|
- `**` Exponentiation
|
||||||
|
- `*` `/` `%` Multiplication, division, remainder
|
||||||
|
- `+` `-` Addition, subtraction
|
||||||
|
- `<<` `>>` Left and right bitwise shifts
|
||||||
|
- `<=` `>=` `<` `>` Comparison
|
||||||
|
- `==` `!=` Equality and inequality
|
||||||
|
- `&` Bitwise AND
|
||||||
|
- `^` Bitwise exclusive OR
|
||||||
|
- `|` Bitwise OR
|
||||||
|
- `&&` Logical AND
|
||||||
|
- `||` Logical OR
|
||||||
|
- `expr ? expr : expr` Conditional operator
|
||||||
|
- `=` `*=` `/=` `%=` `+=` `-=` `<<=` `>>=` `&=` `^=` `|=` Assignment
|
||||||
|
- `expr1 , expr2` Comma
|
||||||
|
|
||||||
|
6.7 Arrays
|
||||||
|
https://www.gnu.org/software/bash/manual/bash.html#Bash-Features
|
||||||
|
|||||||
@@ -289,7 +289,6 @@ git clone --depth 1 https://github.com/torvalds/linux # GitHub mirror
|
|||||||
cd linux
|
cd linux
|
||||||
make tinyconfig # OPTIONAL: Creates the most minimal configuration possible.
|
make tinyconfig # OPTIONAL: Creates the most minimal configuration possible.
|
||||||
make nconfig # Configure the kernel before compiling
|
make nconfig # Configure the kernel before compiling
|
||||||
make -j$(nproc) # Compile the kernel
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The kernel binary compiles to `arch/x86/boot/bzImage` on `x64` systems.
|
The kernel binary compiles to `arch/x86/boot/bzImage` on `x64` systems.
|
||||||
@@ -451,30 +450,164 @@ These APIs are code functions that forward the requests to a library such as `Me
|
|||||||
`Mesa` then calls the Kernel's DRM (Direct Rendering Engine), which then calls the GPU driver to provide GPU capabilities.
|
`Mesa` then calls the Kernel's DRM (Direct Rendering Engine), which then calls the GPU driver to provide GPU capabilities.
|
||||||
The GPU still has a device file, which is used by the Kernel's DRM and Mesa, but most commands are delivered to the GPU driver directly.
|
The GPU still has a device file, which is used by the Kernel's DRM and Mesa, but most commands are delivered to the GPU driver directly.
|
||||||
|
|
||||||
## Shell ([bash](bash.md))
|
## Shell (`bash`)
|
||||||
|
|
||||||
The kernel by itself isn't intractable, so a shell is needed for the user to be able to execute programs and run commands. Bash is not only a prompt, but also an interpreter for its own programming language, which can be used to write scripts and automate tasks.
|
The kernel by itself isn't intractable, so a shell is needed for the user to be able to execute programs and run commands. Bash is not only a prompt, but also an interpreter for [its own programming language](bash.md), which can be used to write scripts and automate tasks.
|
||||||
|
|
||||||
### Compiling
|
### Compiling
|
||||||
|
|
||||||
TODO
|
Dependencies:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sudo apt build-dep bash
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Compile Latest
|
||||||
|
|
||||||
|
```sh
|
||||||
|
git clone --depth 1 https://git.savannah.gnu.org/git/bash
|
||||||
|
mkdir bash-build && cd bash-build
|
||||||
|
../bash/configure
|
||||||
|
make -j$(nproc)
|
||||||
|
sudo make install # To specify the destination, use DESTDIR=...
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Recompile Debian Package
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt install devscripts debhelper
|
||||||
|
mkdir bash-build && cd bash-build
|
||||||
|
apt source bash # Download bash source
|
||||||
|
cd bash-*
|
||||||
|
debuild -j$(nproc) -uc -b
|
||||||
|
sudo apt install ../bash*.deb
|
||||||
|
```
|
||||||
|
|
||||||
## Programs
|
## Programs
|
||||||
|
|
||||||
### `coreutils`
|
*Bash* includes a set of *builtins*: Command-line utilities that come within bash itself. However, these are very limited, and to actually make the computer useful, more programs are needed.
|
||||||
|
|
||||||
|
> To learn more about a specific program, use: `man program_name`.
|
||||||
|
|
||||||
|
### Coreutils
|
||||||
|
|
||||||
|
*Coreutils* are a collection of small command-line utilities, which are essential for text, file and shell manipulation.
|
||||||
|
|
||||||
|
| | | | | |
|
||||||
|
| --------- | ---------- | ---------- | ----------- | ---------- |
|
||||||
|
| `arch` | `base64` | `basename` | `cat` | `chcon` |
|
||||||
|
| `chgrp` | `chmod` | `chown` | `chroot` | `cksum` |
|
||||||
|
| `comm` | `cp` | `csplit` | `cut` | `date` |
|
||||||
|
| `dd` | `df` | `dir` | `dircolors` | `dirname` |
|
||||||
|
| `du` | `echo` | `env` | `expand` | `expr` |
|
||||||
|
| `factor` | `false` | `fmt` | `fold` | `groups` |
|
||||||
|
| `head` | `hostid` | `hostname` | `id` | `install` |
|
||||||
|
| `join` | `kill` | `link` | `ln` | `logname` |
|
||||||
|
| `ls` | `md5sum` | `mkdir` | `mkfifo` | `mknod` |
|
||||||
|
| `mktemp` | `mv` | `nice` | `nl` | `nohup` |
|
||||||
|
| `nproc` | `numfmt` | `od` | `paste` | `pathchk` |
|
||||||
|
| `pinky` | `pr` | `printenv` | `printf` | `ptx` |
|
||||||
|
| `pwd` | `readlink` | `realpath` | `rm` | `rmdir` |
|
||||||
|
| `runcon` | `seq` | `shred` | `shuf` | `sleep` |
|
||||||
|
| `sort` | `split` | `stat` | `stdbuf` | `stty` |
|
||||||
|
| `sum` | `tac` | `tail` | `tee` | `test` |
|
||||||
|
| `timeout` | `touch` | `tr` | `true` | `truncate` |
|
||||||
|
| `tsort` | `tty` | `uname` | `unexpand` | `uniq` |
|
||||||
|
| `unlink` | `uptime` | `users` | `vdir` | `wc` |
|
||||||
|
| `who` | `whoami` | `yes` | | |
|
||||||
|
|
||||||
|
The most popular set of *coreutils* by far is [GNU's coreutils](https://www.gnu.org/software/coreutils/). For embedded systems, it's common to use [BusyBox](https://www.busybox.net/) instead. There is also an ongoing effort to port *GNU's coreutils* to Rust: [uutils coreutils]().
|
||||||
|
|
||||||
### `util-linux`
|
### `util-linux`
|
||||||
|
|
||||||
### `vi/nano`
|
*Util-Linux*, much like *coreutils*, is a collection of smaller programs. These are used mainly for maintenance, and interface with the kernel, filesystems, the clock, etc.
|
||||||
|
|
||||||
|
Unlike the *coreutils*, which are present in almost every system, distributions often pick which programs to ship from the `util-linux` package. For example, Debian doesn't ship `rename`, `hwclock`, and `cal` by default, among others.
|
||||||
|
|
||||||
|
| Path | Utility |
|
||||||
|
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
|
| `bin/` | `dmesg` `kill` `lsblk` `more` `mountpoint` `su` `wdctlfindmnt` `login` `lsfd` `mount` `pipesz` `umount` |
|
||||||
|
| `sbin/` | `agetty` `cfdisk` `fsck.cramfs` `mkfs` `pivot_root` `swapon` `blkdiscard` `chcpu` `fsck.minix` `mkfs.bfs` `runuser` `switch_root` `blkid` `ctrlaltdel` `fsfreeze` `mkfs.cramfs` `sfdisk` `wipefs` `blkpr` `fdisk` `fstrim` `mkfs.minix` `sulogin` `zramctl` `blkzone` `findfs` `hwclock` `mkswap` `swaplabel` `blockdev` `fsck` `losetup` `nologin` `swapoff` |
|
||||||
|
| `usr/bin/` | `bits` `column` `hardlink` `logger` `mcookie` `scriptreplay` `utmpdump` `cal` `coresched` `hexdump` `look` `mesg` `setarch` `uuidgen` `chfn` `eject` `ionice` `lsclocks` `namei` `setpgid` `uuidparse` `chmem` `enosys` `ipcmk` `lscpu` `nsenter` `setpriv` `waitpid` `choom` `exch` `ipcrm` `lsipc` `prlimit` `setsid` `wall` `chrt` `fadvise` `ipcs` `lsirq` `rename` `setterm` `whereis` `chsh` `fallocate` `irqtop` `lslocks` `renice` `taskset` `col` `fincore` `isosize` `lslogins` `rev` `uclampset` `colcrt` `flock` `last` `lsmem` `script` `ul` `colrm` `getopt` `lastlog2` `lsns` `scriptlive` `unshare` |
|
||||||
|
| `usr/sbin` | `addpart` `ldattach` `readprofile` `rfkill` `uuidd` `delpart` `partx` `resizepart` `rtcwake` |
|
||||||
|
|
||||||
|
### Text Editor
|
||||||
|
|
||||||
|
To read and write files on the system, we can make use of *coreutils*:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
touch file.txt # Create a file
|
||||||
|
echo "Data" > file.txt # Write to file
|
||||||
|
cat file.txt # Read from file
|
||||||
|
```
|
||||||
|
|
||||||
|
Although these commands do work, this workflow is not practical, especially for editing large, multi-line files, or even writing new software. There are many text editors, and which you decide to use is a matter of preference.
|
||||||
|
|
||||||
|
Most distributions ship a stripped-down version of `vim`, but other editors such as `nano` and regular `vim` are available in virtually all software repositories.
|
||||||
|
|
||||||
|
#### Vim
|
||||||
|
|
||||||
|
The original Vi editor was included with Unix as proprietary software. Vim was created as an open-source alternative, which improved Vi with many new features, including syntax highlighting, plugin support, and multiple undo/redo levels, among others.
|
||||||
|
|
||||||
|
Vim is still widely used due to its efficiency, as it uses modal editing to implement the following modes:
|
||||||
|
- **Normal Mode**: Navigate the file; delete; copy; paste; etc.
|
||||||
|
- **Insert Mode**: Type new text.
|
||||||
|
- **Visual Mode**: Select text.
|
||||||
|
|
||||||
|
To use these modes effectively, one must learn Vim's keyboard shortcuts. Vim provides a tutorial to help new users get started:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt install vim
|
||||||
|
vimtutor
|
||||||
|
```
|
||||||
|
|
||||||
|
Vim can be heavily customized and extended. Many of its own features can be toggled at compile time. To see which features your installation has enabled, run: `vim --version`.
|
||||||
|
|
||||||
|
#### Vi (`vim.tiny`)
|
||||||
|
|
||||||
|
The `/bin/vi` path on Debian is a symlink to `vim.tiny`, which is a version of Vim that was compiled with many optional features disabled to keep the binary very small. At the time of writing, `vim.tiny` is only ~1.9 MB in size for `x64` systems (`apt info vim-tiny`).
|
||||||
|
|
||||||
|
#### Nano
|
||||||
|
|
||||||
|
Nano is a more friendly and simpler alternative to Vim. It is not nearly as efficient, but it's more approachable for new users who might not be willing to learn Vim's complex keyboard shortcuts.
|
||||||
|
|
||||||
|
Debian also provides a tiny version of Nano, `nano-tiny`, at only 139 kB.
|
||||||
|
|
||||||
## Libraries
|
## Libraries
|
||||||
|
|
||||||
### `glibc`
|
Libraries are collections of reusable code that programs can access. On Linux systems, there are **static libraries**, which are compiled with the program and increase the binary's size, and **dynamic libraries**, which are linked at runtime and can be shared by multiple processes to save disk space and memory.
|
||||||
|
|
||||||
### `ncurses`
|
### Glibc
|
||||||
|
|
||||||
|
Glibc is the GNU's implementation of the C standard library, which is needed for almost all C programs. It provides wrappers for system calls, and bundles `ld-linux`, which is the dynamic linker/loader for ELF executables.
|
||||||
|
|
||||||
|
To see which dynamic libraries are linked to a binary, run: `ldd /path/to/executable`.
|
||||||
|
|
||||||
|
### Ncurses
|
||||||
|
|
||||||
|
Different terminals have their own control sequences to move the cursor and format text. `ncurses` acts as an abstraction layer and facilitates creating TUIs (Terminal User Interfaces).
|
||||||
|
|
||||||
## Init System
|
## Init System
|
||||||
|
|
||||||
|
The init system is started by the kernel as soon as it boots. It is resposible for managing services, starting the shell, and reboot/shutdown of the system.
|
||||||
|
|
||||||
|
The bare minimum init script to start the system could be as follows:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
#! /bin/bash
|
||||||
|
|
||||||
|
mount -t proc none /proc
|
||||||
|
mount -t sysfs none /sys
|
||||||
|
|
||||||
|
exec /bin/bash
|
||||||
|
```
|
||||||
|
|
||||||
|
Debian uses Systemd, and so, `systemctl` is used to manage services.
|
||||||
|
|
||||||
|
A service is a unit file that specifies how to start, stop, reload and manage the processes associated with the service. After starting a service, it runs in the background until stopped.
|
||||||
|
|
||||||
|
Unit files are usually located under `/lib/systemd/system/` and `/etc/systemd/system/`.
|
||||||
|
|
||||||
## GRUB
|
## GRUB
|
||||||
|
|
||||||
## Networking
|
## Networking
|
||||||
|
|||||||
Reference in New Issue
Block a user