mirror of
https://github.com/TrudeEH/web.git
synced 2025-12-06 08:23:37 +00:00
Misc fixes; Update index; Add meson to compiling note
This commit is contained in:
31
content/.obsidian/workspace.json
vendored
31
content/.obsidian/workspace.json
vendored
@@ -13,13 +13,27 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "markdown",
|
"type": "markdown",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "notes/linux/index.md",
|
"file": "notes/index/index.md",
|
||||||
"mode": "source",
|
"mode": "source",
|
||||||
"source": false
|
"source": false
|
||||||
},
|
},
|
||||||
"icon": "lucide-file",
|
"icon": "lucide-file",
|
||||||
"title": "index"
|
"title": "index"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "2c7f99d6e12af978",
|
||||||
|
"type": "leaf",
|
||||||
|
"state": {
|
||||||
|
"type": "markdown",
|
||||||
|
"state": {
|
||||||
|
"file": "notes/compiling.md",
|
||||||
|
"mode": "source",
|
||||||
|
"source": false
|
||||||
|
},
|
||||||
|
"icon": "lucide-file",
|
||||||
|
"title": "compiling"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -141,7 +155,7 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "outline",
|
"type": "outline",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "notes/linux/index.md",
|
"file": "notes/index/index.md",
|
||||||
"followCursor": false,
|
"followCursor": false,
|
||||||
"showSearch": false,
|
"showSearch": false,
|
||||||
"searchQuery": ""
|
"searchQuery": ""
|
||||||
@@ -161,7 +175,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"currentTab": 4
|
"currentTab": 3
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"direction": "horizontal",
|
"direction": "horizontal",
|
||||||
@@ -181,9 +195,13 @@
|
|||||||
},
|
},
|
||||||
"active": "fc8813233e11f75b",
|
"active": "fc8813233e11f75b",
|
||||||
"lastOpenFiles": [
|
"lastOpenFiles": [
|
||||||
|
"notes/compiling.md",
|
||||||
"notes/index/index.md",
|
"notes/index/index.md",
|
||||||
"notes/firewall.md",
|
|
||||||
"notes/linux/index.md",
|
"notes/linux/index.md",
|
||||||
|
"drafts/rust.md",
|
||||||
|
"notes/c-language.md",
|
||||||
|
"drafts/bash.md",
|
||||||
|
"notes/firewall.md",
|
||||||
"notes/how_to_computer/index.md",
|
"notes/how_to_computer/index.md",
|
||||||
"posts/linux-starter-guide/index.md",
|
"posts/linux-starter-guide/index.md",
|
||||||
"drafts/linux/plasma6.jpg",
|
"drafts/linux/plasma6.jpg",
|
||||||
@@ -195,14 +213,11 @@
|
|||||||
"notes/git.md",
|
"notes/git.md",
|
||||||
"posts/dotfiles.md",
|
"posts/dotfiles.md",
|
||||||
"drafts/linux-encrypt/index.md",
|
"drafts/linux-encrypt/index.md",
|
||||||
"drafts/bash.md",
|
|
||||||
"notes/c-language.md",
|
|
||||||
"notes/linux/image7.png",
|
"notes/linux/image7.png",
|
||||||
"notes/linux/image6.png",
|
"notes/linux/image6.png",
|
||||||
"notes/linux/image5.png",
|
"notes/linux/image5.png",
|
||||||
"notes/linux/Pasted image 20250401143509.png",
|
"notes/linux/Pasted image 20250401143509.png",
|
||||||
"drafts/swift.md",
|
"drafts/swift.md",
|
||||||
"drafts/rust.md",
|
|
||||||
"drafts/nvim.md",
|
"drafts/nvim.md",
|
||||||
"drafts/macOS.md",
|
"drafts/macOS.md",
|
||||||
"drafts/lua.md",
|
"drafts/lua.md",
|
||||||
@@ -212,12 +227,10 @@
|
|||||||
"drafts/assembly.md",
|
"drafts/assembly.md",
|
||||||
"drafts",
|
"drafts",
|
||||||
"notes/drafts/bash.md",
|
"notes/drafts/bash.md",
|
||||||
"notes/bash.md",
|
|
||||||
"notes/drafts/linux/index.md",
|
"notes/drafts/linux/index.md",
|
||||||
"notes/drafts/rust.md",
|
"notes/drafts/rust.md",
|
||||||
"notes/drafts/swift.md",
|
"notes/drafts/swift.md",
|
||||||
"notes/drafts/linux-encrypt/index.md",
|
"notes/drafts/linux-encrypt/index.md",
|
||||||
"notes/compiling.md",
|
|
||||||
"notes/gdb.md",
|
"notes/gdb.md",
|
||||||
"debian-12.10.0-amd64-netinst.iso",
|
"debian-12.10.0-amd64-netinst.iso",
|
||||||
"notes/encryption.md",
|
"notes/encryption.md",
|
||||||
|
|||||||
@@ -8,21 +8,19 @@ author: TrudeEH
|
|||||||
showToc: true
|
showToc: true
|
||||||
---
|
---
|
||||||
|
|
||||||
## Bash Language
|
## Strings
|
||||||
|
|
||||||
### Strings
|
|
||||||
|
|
||||||
- `""` Defines a string which supports substitutions (`$` and `\`, for example).
|
- `""` Defines a string which supports substitutions (`$` and `\`, for example).
|
||||||
- `''` Defines a string, but preserves its actual value (substitutions are treated as regular characters).
|
- `''` Defines a string, but preserves its actual value (substitutions are treated as regular characters).
|
||||||
- [ANSI Escape Sequences](c-language.md#ANSI%20Escape%20Sequences) apply when using `""`.
|
- [ANSI Escape Sequences](c-language.md#ANSI%20Escape%20Sequences) apply when using `""`.
|
||||||
|
|
||||||
### Comments
|
## Comments
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# comment
|
# comment
|
||||||
```
|
```
|
||||||
|
|
||||||
### Commands
|
## Commands
|
||||||
|
|
||||||
A shell command consists of the command itself, followed by its arguments.
|
A shell command consists of the command itself, followed by its arguments.
|
||||||
|
|
||||||
@@ -32,7 +30,7 @@ command "arg1" "arg2"
|
|||||||
|
|
||||||
If the first word of a command is a reserved word, bash handles the command, otherwise, it searches for an executable on the system's `$PATH`, a list of directories where a binary could be located.
|
If the first word of a command is a reserved word, bash handles the command, otherwise, it searches for an executable on the system's `$PATH`, a list of directories where a binary could be located.
|
||||||
|
|
||||||
#### Reserved Words
|
### Reserved Words
|
||||||
|
|
||||||
| | | | | | |
|
| | | | | | |
|
||||||
|---|---|---|---|---|---|
|
|---|---|---|---|---|---|
|
||||||
@@ -41,16 +39,16 @@ If the first word of a command is a reserved word, bash handles the command, oth
|
|||||||
|`case`|`esac`|`coproc`|`select`|`function`|
|
|`case`|`esac`|`coproc`|`select`|`function`|
|
||||||
|`{`|`}`|`[[`|`]]`|`!`|
|
|`{`|`}`|`[[`|`]]`|`!`|
|
||||||
|
|
||||||
### List of Commands
|
## List of Commands
|
||||||
|
|
||||||
- `command1 ; command2` Execute command2 after command1, sequentially.
|
- `command1 ; command2` Execute command2 after command1, sequentially.
|
||||||
- `command1 &` Execute command1 asynchronously in a subshell.
|
- `command1 &` Execute command1 asynchronously in a subshell.
|
||||||
- `command1 && command2` *AND*: Only execute command2 if command1 returns 0 (success).
|
- `command1 && command2` *AND*: Only execute command2 if command1 returns 0 (success).
|
||||||
- `command1 || command2` *OR*: Only execute command2 if command1 returns a non-zero exit value (failure).
|
- `command1 || command2` *OR*: Only execute command2 if command1 returns a non-zero exit value (failure).
|
||||||
|
|
||||||
### Loops
|
## Loops
|
||||||
|
|
||||||
#### `until`
|
### `until`
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
until test-commands; do
|
until test-commands; do
|
||||||
@@ -60,7 +58,7 @@ done
|
|||||||
|
|
||||||
Execute the code in `...` for as long as `test-commands` return non-zero.
|
Execute the code in `...` for as long as `test-commands` return non-zero.
|
||||||
|
|
||||||
#### `while`
|
### `while`
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
while test-commands; do
|
while test-commands; do
|
||||||
@@ -70,11 +68,11 @@ done
|
|||||||
|
|
||||||
Execute `...` for as long as `test-commands` return 0.
|
Execute `...` for as long as `test-commands` return 0.
|
||||||
|
|
||||||
#### `for`
|
### `for`
|
||||||
|
|
||||||
Expand `words` and execute `...` for each member in the resultant list, with `name` bound to the current member.
|
Expand `words` and execute `...` for each member in the resultant list, with `name` bound to the current member.
|
||||||
|
|
||||||
##### Iterate through List
|
#### Iterate through List
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
for item in list; do
|
for item in list; do
|
||||||
@@ -82,7 +80,7 @@ for item in list; do
|
|||||||
done
|
done
|
||||||
```
|
```
|
||||||
|
|
||||||
##### C-like Loop
|
#### C-like Loop
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
for (( i=1; i<=10; i++ )); do
|
for (( i=1; i<=10; i++ )); do
|
||||||
@@ -90,7 +88,7 @@ for (( i=1; i<=10; i++ )); do
|
|||||||
done
|
done
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Infinite Loop
|
#### Infinite Loop
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
for (( ; ; )); do
|
for (( ; ; )); do
|
||||||
@@ -98,9 +96,9 @@ for (( ; ; )); do
|
|||||||
done
|
done
|
||||||
```
|
```
|
||||||
|
|
||||||
### Conditional Constructs
|
## Conditional Constructs
|
||||||
|
|
||||||
#### `if`
|
### `if`
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
if test-commands; then
|
if test-commands; then
|
||||||
@@ -115,7 +113,7 @@ fi
|
|||||||
Execute the first `...` if `test-commands` returns 0, and evaluate the next condition otherwise. This process repeats until `else` is found, or one of the `tests` evaluates to a 0.
|
Execute the first `...` if `test-commands` returns 0, and evaluate the next condition otherwise. This process repeats until `else` is found, or one of the `tests` evaluates to a 0.
|
||||||
Once any `...` executes, the remaining `if` construct is skipped.
|
Once any `...` executes, the remaining `if` construct is skipped.
|
||||||
|
|
||||||
#### `case`
|
### `case`
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
case word in
|
case word in
|
||||||
@@ -132,7 +130,7 @@ The `|` operator separates multiple patterns, and each clause can end with `;;`,
|
|||||||
|
|
||||||
Using `;&` instead of `;;` would cause the next `...` to be executed as well, and `;;&` would test the next clause, instead of immediately exiting.
|
Using `;&` instead of `;;` would cause the next `...` to be executed as well, and `;;&` would test the next clause, instead of immediately exiting.
|
||||||
|
|
||||||
#### `select`
|
### `select`
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
PS3="Enter a number: "
|
PS3="Enter a number: "
|
||||||
@@ -157,29 +155,29 @@ Output:
|
|||||||
Enter a number:
|
Enter a number:
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `((...))`
|
### `((...))`
|
||||||
|
|
||||||
The arithmetic expression is evaluated according to the rules described below (see [Shell Arithmetic]() TODO link to shell arithmetic).
|
The arithmetic expression is evaluated according to the rules described below (see [Shell Arithmetic]() TODO link to shell arithmetic).
|
||||||
|
|
||||||
#### `[[...]]`
|
### `[[...]]`
|
||||||
|
|
||||||
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below in [Bash Conditional Expressions](https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions).
|
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below in [Bash Conditional Expressions](https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions).
|
||||||
|
|
||||||
#### Combine Expressions
|
### Combine Expressions
|
||||||
|
|
||||||
- `( expression )` Returns the value of expression. (Can be used to override precedence).
|
- `( expression )` Returns the value of expression. (Can be used to override precedence).
|
||||||
- `! expression` *NOT* an expression. (`true` if expression is `false`).
|
- `! expression` *NOT* an expression. (`true` if expression is `false`).
|
||||||
- `exp1 && exp2` *AND* - `true` if both expressions are `true`.
|
- `exp1 && exp2` *AND* - `true` if both expressions are `true`.
|
||||||
- `exp1 || exp2` *OR* - `true` if either expressions are `true`.
|
- `exp1 || exp2` *OR* - `true` if either expressions are `true`.
|
||||||
|
|
||||||
#### Grouping Commands
|
### Grouping Commands
|
||||||
|
|
||||||
Bash allows for commands to be grouped as a single unit. That way, if the group is redirected, the output of every command in the list is passed to a single stream.
|
Bash allows for commands to be grouped as a single unit. That way, if the group is redirected, the output of every command in the list is passed to a single stream.
|
||||||
|
|
||||||
- `( list )` Create a subshell (variables created inside it can't be accessed outside).
|
- `( list )` Create a subshell (variables created inside it can't be accessed outside).
|
||||||
- `{ list; }` No subshell is created.
|
- `{ list; }` No subshell is created.
|
||||||
|
|
||||||
### Functions
|
## Functions
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
fname() {
|
fname() {
|
||||||
@@ -199,7 +197,7 @@ fname
|
|||||||
|
|
||||||
Any variables defined inside the function
|
Any variables defined inside the function
|
||||||
|
|
||||||
#### Arguments
|
### Arguments
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
fname() {
|
fname() {
|
||||||
@@ -209,7 +207,7 @@ fname() {
|
|||||||
fname "a" "b"
|
fname "a" "b"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Scope
|
### Scope
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
var1='A'
|
var1='A'
|
||||||
@@ -228,7 +226,7 @@ echo "var1: $var1, var2: $var2" # A, D
|
|||||||
|
|
||||||
Defining a variable inside the function overwrites the global scope. To prevent this, use the `local` keyword.
|
Defining a variable inside the function overwrites the global scope. To prevent this, use the `local` keyword.
|
||||||
|
|
||||||
#### `return`
|
### `return`
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
fname() {
|
fname() {
|
||||||
@@ -241,7 +239,7 @@ echo $? # 1
|
|||||||
|
|
||||||
Use the `return` command to exit the function and return a value.
|
Use the `return` command to exit the function and return a value.
|
||||||
|
|
||||||
### Variables (Parameters)
|
## Variables (Parameters)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
name="Trude"
|
name="Trude"
|
||||||
@@ -254,7 +252,7 @@ echo ${name}
|
|||||||
|
|
||||||
Variables can be of any type, and grow to any needed size.
|
Variables can be of any type, and grow to any needed size.
|
||||||
|
|
||||||
#### Special Variables
|
### Special Variables
|
||||||
|
|
||||||
- `$*` Expands to every positional parameter: `$1$2$3`.
|
- `$*` Expands to every positional parameter: `$1$2$3`.
|
||||||
- `$@` Expands to every positional parameter, separated by spaces: `"$1" "$2" "$3"`.
|
- `$@` Expands to every positional parameter, separated by spaces: `"$1" "$2" "$3"`.
|
||||||
@@ -265,21 +263,21 @@ Variables can be of any type, and grow to any needed size.
|
|||||||
- `$!` Process ID of the latest job placed into the background.
|
- `$!` Process ID of the latest job placed into the background.
|
||||||
- `$0` Name of the shell or script.
|
- `$0` Name of the shell or script.
|
||||||
|
|
||||||
### Shell Expansions
|
## Shell Expansions
|
||||||
|
|
||||||
#### Brace Expansion
|
### Brace Expansion
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
echo a{d,c,b}e # ade ace abe
|
echo a{d,c,b}e # ade ace abe
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Tilde Expansion
|
### Tilde Expansion
|
||||||
|
|
||||||
- `~` = `$HOME`
|
- `~` = `$HOME`
|
||||||
- `~+` = `$PWD`
|
- `~+` = `$PWD`
|
||||||
- `~-` = `$OLDPWD`
|
- `~-` = `$OLDPWD`
|
||||||
|
|
||||||
#### Shell Parameter Expansion
|
### Shell Parameter Expansion
|
||||||
|
|
||||||
- `${var}` Braces are required if the variable is positional and over one digit, or if it is followed by a character that is not part of its name.
|
- `${var}` Braces are required if the variable is positional and over one digit, or if it is followed by a character that is not part of its name.
|
||||||
- `${!var}` Access the value of `var`, and checks if it is the name of another variable. If so, expands that variable. (Pointer)
|
- `${!var}` Access the value of `var`, and checks if it is the name of another variable. If so, expands that variable. (Pointer)
|
||||||
@@ -316,7 +314,7 @@ echo a{d,c,b}e # ade ace abe
|
|||||||
- `a` Output the flags corresponding to `var`'s attributes.
|
- `a` Output the flags corresponding to `var`'s attributes.
|
||||||
- `k` Same as `K`, but separates keys and values using spaces, making it easier to loop through them.
|
- `k` Same as `K`, but separates keys and values using spaces, making it easier to loop through them.
|
||||||
|
|
||||||
#### Command Substitution
|
### Command Substitution
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
echo "$(command)"
|
echo "$(command)"
|
||||||
@@ -325,7 +323,7 @@ echo "`command`"
|
|||||||
|
|
||||||
Execute a command and substitute itself with the command's result.
|
Execute a command and substitute itself with the command's result.
|
||||||
|
|
||||||
#### Arithmetic Expansion
|
### Arithmetic Expansion
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
echo "$(( expression ))"
|
echo "$(( expression ))"
|
||||||
@@ -333,7 +331,7 @@ echo "$(( expression ))"
|
|||||||
|
|
||||||
Performs an arithmetic expression and substitutes itself with the result.
|
Performs an arithmetic expression and substitutes itself with the result.
|
||||||
|
|
||||||
#### Process Substitution
|
### Process Substitution
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cat <(command) >(command)
|
cat <(command) >(command)
|
||||||
@@ -347,7 +345,7 @@ The `>()` substitution is also executed asynchronously, and creates a temporary
|
|||||||
|
|
||||||
> Neither `cat` nor `gzip` are `bash` commands ('builtins'), but external programs.
|
> Neither `cat` nor `gzip` are `bash` commands ('builtins'), but external programs.
|
||||||
|
|
||||||
#### Pattern Matching
|
### Pattern Matching
|
||||||
|
|
||||||
- `*` Matches any string.
|
- `*` Matches any string.
|
||||||
- `?` Matches any single character.
|
- `?` Matches any single character.
|
||||||
@@ -358,7 +356,7 @@ The `>()` substitution is also executed asynchronously, and creates a temporary
|
|||||||
- `@(pattern-list)` Matches one of the given patterns.
|
- `@(pattern-list)` Matches one of the given patterns.
|
||||||
- `!(pattern-list)` Matches anything except the given patterns.
|
- `!(pattern-list)` Matches anything except the given patterns.
|
||||||
|
|
||||||
### Redirections
|
## Redirections
|
||||||
|
|
||||||
- `command > dest` Redirect the output of a command to a destination: A file, device, command, list, etc.
|
- `command > dest` Redirect the output of a command to a destination: A file, device, command, list, etc.
|
||||||
- `command >> dest` Append to the destination instead of overwriting.
|
- `command >> dest` Append to the destination instead of overwriting.
|
||||||
@@ -379,12 +377,12 @@ string
|
|||||||
EOF
|
EOF
|
||||||
```
|
```
|
||||||
|
|
||||||
### Shell Builtin Commands
|
## Shell Builtin Commands
|
||||||
|
|
||||||
This section is an introduction to every command available in `bash`.
|
This section is an introduction to every command available in `bash`.
|
||||||
To learn more about some command, run `help command`.
|
To learn more about some command, run `help command`.
|
||||||
|
|
||||||
#### Bourne Shell Commands
|
### Bourne Shell Commands
|
||||||
|
|
||||||
- `: arguments` Do nothing beyond expanding arguments and performing redirections.
|
- `: arguments` Do nothing beyond expanding arguments and performing redirections.
|
||||||
- `. file` Read and execute commands from `file`.
|
- `. file` Read and execute commands from `file`.
|
||||||
@@ -419,7 +417,7 @@ To learn more about some command, run `help command`.
|
|||||||
- `umask` Defines which permissions should be removed from newly created files.
|
- `umask` Defines which permissions should be removed from newly created files.
|
||||||
- `unset` Remove a variable or function name. (Use `-f` to remove the actual function definition)
|
- `unset` Remove a variable or function name. (Use `-f` to remove the actual function definition)
|
||||||
|
|
||||||
#### Bash Commands
|
### Bash Commands
|
||||||
|
|
||||||
- `alias` Prints the list of aliases or defines new ones (with `alias name=value`).
|
- `alias` Prints the list of aliases or defines new ones (with `alias name=value`).
|
||||||
- `bind` Displays or sets key and function bindings for Readline.
|
- `bind` Displays or sets key and function bindings for Readline.
|
||||||
@@ -444,9 +442,9 @@ To learn more about some command, run `help command`.
|
|||||||
- `ulimit` Controls resource limits for processes created by the shell.
|
- `ulimit` Controls resource limits for processes created by the shell.
|
||||||
- `unalias` Removes defined aliases, with an option to remove all.
|
- `unalias` Removes defined aliases, with an option to remove all.
|
||||||
|
|
||||||
### Shell Variables
|
## Shell Variables
|
||||||
|
|
||||||
#### Bourne Shell Variables
|
### Bourne Shell Variables
|
||||||
|
|
||||||
- `CDPATH` Search path directories for the `cd` command.
|
- `CDPATH` Search path directories for the `cd` command.
|
||||||
- `HOME` Current user's home directory, default for `cd`.
|
- `HOME` Current user's home directory, default for `cd`.
|
||||||
@@ -459,7 +457,7 @@ To learn more about some command, run `help command`.
|
|||||||
- `PS1` Primary prompt string displayed interactively.
|
- `PS1` Primary prompt string displayed interactively.
|
||||||
- `PS2` Secondary prompt string for continued commands.
|
- `PS2` Secondary prompt string for continued commands.
|
||||||
|
|
||||||
#### Bash Variables
|
### Bash Variables
|
||||||
|
|
||||||
- `_` Pathname of invoked shell/script, or last argument of previous command.
|
- `_` Pathname of invoked shell/script, or last argument of previous command.
|
||||||
- `BASH` Full pathname used to execute the current Bash instance.
|
- `BASH` Full pathname used to execute the current Bash instance.
|
||||||
@@ -559,7 +557,7 @@ To learn more about some command, run `help command`.
|
|||||||
- `TMPDIR` Directory used for creating temporary files.
|
- `TMPDIR` Directory used for creating temporary files.
|
||||||
- `UID` Numeric real user ID of the current user (readonly).
|
- `UID` Numeric real user ID of the current user (readonly).
|
||||||
|
|
||||||
### Parse Arguments
|
## Parse Arguments
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
while getopts "ab:c" opt; do
|
while getopts "ab:c" opt; do
|
||||||
@@ -673,5 +671,21 @@ Arithmetic is performed using `(())`, `let` and `declare -i`.
|
|||||||
- `=` `*=` `/=` `%=` `+=` `-=` `<<=` `>>=` `&=` `^=` `|=` Assignment
|
- `=` `*=` `/=` `%=` `+=` `-=` `<<=` `>>=` `&=` `^=` `|=` Assignment
|
||||||
- `expr1 , expr2` Comma
|
- `expr1 , expr2` Comma
|
||||||
|
|
||||||
|
## Arrays
|
||||||
|
|
||||||
|
### Indexed arrays
|
||||||
|
|
||||||
|
```bash
|
||||||
|
declare -a name
|
||||||
|
name[]=value
|
||||||
|
```
|
||||||
|
|
||||||
|
### Associative arrays
|
||||||
|
|
||||||
|
```bash
|
||||||
|
declare -A name
|
||||||
|
name=(value1 value2 ...)
|
||||||
|
```
|
||||||
|
|
||||||
6.7 Arrays
|
6.7 Arrays
|
||||||
https://www.gnu.org/software/bash/manual/bash.html#Bash-Features
|
https://www.gnu.org/software/bash/manual/bash.html#Bash-Features
|
||||||
@@ -1,41 +1,85 @@
|
|||||||
---
|
---
|
||||||
title: "rust"
|
title: Rust
|
||||||
description:
|
description:
|
||||||
draft: true
|
draft: true
|
||||||
tags:
|
tags:
|
||||||
author: TrudeEH
|
author: TrudeEH
|
||||||
showToc: true
|
showToc: true
|
||||||
|
summary:
|
||||||
---
|
---
|
||||||
|
|
||||||
## Vocabulary
|
|
||||||
|
|
||||||
|Command / Word|Action / Meaning|Example|
|
|
||||||
|---|---|---|
|
|
||||||
|Statement|Performs an action, but does not return a value.|Function definitions, code that ends with `;`.|
|
|
||||||
|Expression|Evaluate to a resultant value.|Tests, math.|
|
|
||||||
|
|
||||||
## Tools
|
## Tools
|
||||||
|
|
||||||
- Install Rust: `curl --proto '=https' --tlsv1.2 -sSf <https://sh.rustup.rs> | sh`
|
- Install Rust: `curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh`
|
||||||
- `rustup`
|
- `rustup`
|
||||||
- `rustc`
|
- `rustc`
|
||||||
- `cargo`
|
- `cargo`
|
||||||
|
|
||||||
## Hello World!
|
## Hello World
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello world!");
|
println!("Hello world!"); // Macro to print text
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Variables
|
## Comments
|
||||||
|
|
||||||
|
- *Regular comments* which are ignored by the compiler:
|
||||||
|
- `// Line comments which go to the end of the line.`
|
||||||
|
- `/* Block comments which go to the closing delimiter. */`
|
||||||
|
- *Doc comments* which are parsed into HTML library documentation:
|
||||||
|
- `/// Generate library docs for the following item.`
|
||||||
|
- `//! Generate library docs for the enclosing item.`
|
||||||
|
|
||||||
|
## Formatted Print
|
||||||
|
|
||||||
|
## Primitives (Variables)
|
||||||
|
|
||||||
|
### Scalar Types
|
||||||
|
|
||||||
|
- Signed integers (default to `i32`): `i8`, `i16`, `i32`, `i64`, `i128` and `isize` (pointer size)
|
||||||
|
- Unsigned integers: `u8`, `u16`, `u32`, `u64`, `u128` and `usize` (pointer size)
|
||||||
|
- Floating point (default to `f64`): `f32`, `f64`
|
||||||
|
- `char` Unicode scalar values like `'a'`, `'α'` and `'∞'` (4 bytes each)
|
||||||
|
- `bool` either `true` or `false`
|
||||||
|
- The unit type `()`, whose only possible value is an empty tuple: `()`
|
||||||
|
|
||||||
|
### Compound Types
|
||||||
|
|
||||||
|
- Arrays: `[1, 2, 3]`
|
||||||
|
- Tuples: `(1, true)`
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
|
||||||
|
Variables can either be *type annotated*, or infered by context. By default, a variable is always constant, and can be made mutable with the `mut` keyword. After creating a variable, its data type cannot be changed.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
let x: i32; // A variable can only be used if it has been initialized (contains a value)
|
fn main() {
|
||||||
|
let x: i32; // Declare a variable
|
||||||
|
|
||||||
let y: i8 = 5; // All variables are constant by default.
|
let a_float: f64 = 1.0; // Regular annotation
|
||||||
let mut z = 1; // The mut keyword makes the variable mutable. (Explicit type annotation is not necessary, but recommended).
|
let an_integer = 5i32; // Suffix annotation
|
||||||
|
|
||||||
|
let default_float = 3.0; // `f64`
|
||||||
|
let default_integer = 7; // `i32`
|
||||||
|
|
||||||
|
// A type can also be inferred from context.
|
||||||
|
let mut inferred_type = 12; // Type i64 is inferred from another line.
|
||||||
|
inferred_type = 4294967296i64;
|
||||||
|
|
||||||
|
let mut mutable = 12; // Mutable `i32`
|
||||||
|
mutable = 21;
|
||||||
|
|
||||||
|
// Variables can be overwritten with shadowing.
|
||||||
|
let mutable = true;
|
||||||
|
|
||||||
|
// Array signature consists of Type T and length as [T; length].
|
||||||
|
let my_array: [i32; 5] = [1, 2, 3, 4, 5];
|
||||||
|
|
||||||
|
// Tuple is a collection of values of different types
|
||||||
|
// and is constructed using parentheses ().
|
||||||
|
let my_tuple = (5u32, 1u8, true, -5.04f32);
|
||||||
|
|
||||||
let (k, f); //Same as "let k; let f;"
|
let (k, f); //Same as "let k; let f;"
|
||||||
|
|
||||||
@@ -43,8 +87,47 @@ let t = { // Initialize a variable as the result of an expression.
|
|||||||
let squared = y * y;
|
let squared = y * y;
|
||||||
squared
|
squared
|
||||||
};
|
};
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> A variable can only be used after it has been initialized (contains a value).
|
||||||
|
|
||||||
|
### Literals and Operators
|
||||||
|
|
||||||
|
Integers can also be expressed using hexadecimal `0x`, octal `0o` or binary `0b`.
|
||||||
|
To improve readability, `_` can be added to numbers: `1_000` is the same as `1000`.
|
||||||
|
Scientific e-notation such as `1e6`, `7.4e-4` is also supported, and defaults to `f64`.
|
||||||
|
|
||||||
|
The operators available and their precedence are similar to `C`:
|
||||||
|
|
||||||
|
```Rust
|
||||||
|
fn main() {
|
||||||
|
println!("1 + 2 = {}", 1u32 + 2); // Integer addition
|
||||||
|
println!("1 - 2 = {}", 1i32 - 2); // Integer subtraction
|
||||||
|
// Changing `1i32` to `1u32` would causa an integer underflow.
|
||||||
|
|
||||||
|
// Scientific notation
|
||||||
|
println!("1e4 is {}, -2.5e-3 is {}", 1e4, -2.5e-3);
|
||||||
|
|
||||||
|
// Short-circuiting boolean logic
|
||||||
|
println!("true AND false is {}", true && false);
|
||||||
|
println!("true OR false is {}", true || false);
|
||||||
|
println!("NOT true is {}", !true);
|
||||||
|
|
||||||
|
// Bitwise operations
|
||||||
|
println!("0011 AND 0101 is {:04b}", 0b0011u32 & 0b0101);
|
||||||
|
println!("0011 OR 0101 is {:04b}", 0b0011u32 | 0b0101);
|
||||||
|
println!("0011 XOR 0101 is {:04b}", 0b0011u32 ^ 0b0101);
|
||||||
|
println!("1 << 5 is {}", 1u32 << 5);
|
||||||
|
println!("0x80 >> 2 is 0x{:x}", 0x80u32 >> 2);
|
||||||
|
|
||||||
|
// Use underscores to improve readability!
|
||||||
|
println!("One million is written as {}", 1_000_000u32);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Data Types
|
## Data Types
|
||||||
|
|
||||||
### Integer Types
|
### Integer Types
|
||||||
|
|||||||
@@ -64,16 +64,50 @@ gcc -o hello hello.c -lcs50
|
|||||||
## Make
|
## Make
|
||||||
|
|
||||||
`Make` Is a build automation tool that automates the process of compiling, linking and building executables.
|
`Make` Is a build automation tool that automates the process of compiling, linking and building executables.
|
||||||
An example `Makefile` could look like the following:
|
An example `Makefile` (for an app using `GTK4`) could be as follows:
|
||||||
|
|
||||||
```Makefile
|
```Makefile
|
||||||
hello:
|
CC = gcc
|
||||||
gcc -o hello hello.c -lcs50
|
CFLAGS = $(shell pkg-config --cflags gtk4)
|
||||||
|
LIBS = $(shell pkg-config --libs gtk4)
|
||||||
|
|
||||||
|
hello: hello.c
|
||||||
|
$(CC) $(CFLAGS) -o hello hello.c $(LIBS) -O2
|
||||||
|
|
||||||
|
debug: hello.c
|
||||||
|
$(CC) $(CFLAGS) -o hello hello.c $(LDFLAGS) -Wall -Wextra -Og -g
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f hello
|
rm -f hello
|
||||||
```
|
```
|
||||||
|
|
||||||
```Shell
|
```Shell
|
||||||
make # Compiles hello.c
|
make # Compiles hello.c
|
||||||
|
make debug # Compiles hello.c using debug flags
|
||||||
make clean # Removes the executable (hello) generated by the make command.
|
make clean # Removes the executable (hello) generated by the make command.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Meson
|
||||||
|
|
||||||
|
While `make` automates compiling, on larger projects, makefiles can grow hard to read quickly, and dependency resolution has to be done using external tools (such as `pkg-config`).
|
||||||
|
|
||||||
|
Meson is a more modern alternative, which is faster, and uses declarative syntax (describe *what* to build, instead of *how* to build it). Meson can automatically manage dependencies, header files, and compiler flags.
|
||||||
|
|
||||||
|
When `meson` is executed, it generates a `ninja.build` file, which is then parsed by `ninja`, a lower-level build tool.
|
||||||
|
|
||||||
|
```meson.build
|
||||||
|
project('hello', 'c')
|
||||||
|
|
||||||
|
gtk4_dep = dependency('gtk4')
|
||||||
|
|
||||||
|
executable('hello', 'hello.c',
|
||||||
|
dependencies: gtk4_dep)
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mkdir build
|
||||||
|
meson setup build # Generate files for Ninja in the build dir
|
||||||
|
meson compile -C build # Compile the project (same as `ninja -C build`)
|
||||||
|
./build/hello
|
||||||
|
ninja clean -C build # Clean executable files in the build directory
|
||||||
|
```
|
||||||
|
|||||||
@@ -10,29 +10,30 @@ weight: "1"
|
|||||||
---
|
---
|
||||||
|
|
||||||
- [Building a Computer From Scratch](../how_to_computer/)
|
- [Building a Computer From Scratch](../how_to_computer/)
|
||||||
- [Operating Systems](../linux/) `Linux`
|
- [Operating System](../linux/) `Linux`
|
||||||
- Shell Scripting `BASH` - `Coming Soon!`
|
- [C Language](../c-language/)
|
||||||
- [C](../c-language/)
|
- [Algorithms & Data Structures](../algorithms_and_data/)
|
||||||
- [Compiling](../compiling/) `Make` `GCC` `Clang`
|
- [Compiling](../compiling/) `GCC` `Make` `Meson`
|
||||||
- [Debugging](../gdb/) `GDB`
|
- [Debugging](../gdb/) `GDB`
|
||||||
- [Snippets](../c-snippets/)
|
- [Snippets](../c-snippets/)
|
||||||
- [Algorithms & Data Structures](../algorithms_and_data/)
|
- GUI Apps
|
||||||
- Rust Lang - `Coming Soon!`
|
- Object-Oriented C `GObject` - `Coming Soon!`
|
||||||
- GUI Apps `GTK4` - `Coming Soon!`
|
- GUI Toolkit `GTK4` - `Coming Soon!`
|
||||||
- [Python](../python/)
|
- Shell Scripting `BASH` - `Coming Soon!`
|
||||||
- [Flask](../flask/)
|
|
||||||
- [Databases](../databases/) `SQL`
|
|
||||||
- Networking
|
- Networking
|
||||||
- [HTTP](../http/) `CURL`
|
- [HTTP](../http/) `CURL`
|
||||||
- [Encryption](../encryption/) `GPG` `Cryptsetup`
|
- [Encryption](../encryption/) `GPG` `Cryptsetup`
|
||||||
- [HTTPS and SSL Certificates](../https-ssl-certs/) `Certbot`
|
- [HTTPS and SSL Certificates](../https-ssl-certs/) `Certbot`
|
||||||
- [SSH](../ssh/)
|
- [SSH](../ssh/)
|
||||||
- [Firewall](../firewall/) `UFW`
|
- [Firewall](../firewall/) `UFW`
|
||||||
- [IRC](../irc/)
|
- [Instant Messaging](../irc/) `IRC`
|
||||||
- Web Development
|
- Web Development
|
||||||
- [HTML](../html/)
|
- [HTML](../html/)
|
||||||
- CSS - `Coming Soon!`
|
- CSS - `Coming Soon!`
|
||||||
- JS - `Coming Soon!`
|
- JS - `Coming Soon!`
|
||||||
|
- [Python](../python/)
|
||||||
|
- [Flask](../flask/)
|
||||||
|
- [Databases](../databases/) `SQL`
|
||||||
- Tools
|
- Tools
|
||||||
- [Version Control](../git/) `GIT`
|
- [Version Control](../git/) `GIT`
|
||||||
- [Password Manager](../pass/) `PASS`
|
- [Password Manager](../pass/) `PASS`
|
||||||
|
|||||||
@@ -204,7 +204,7 @@ The GPU still has a device file, which is used by the Kernel's DRM and Mesa, but
|
|||||||
|
|
||||||
## Shell (`bash`)
|
## 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](../bash), 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, which can be used to write scripts and automate tasks.
|
||||||
|
|
||||||
### Compiling
|
### Compiling
|
||||||
|
|
||||||
@@ -910,7 +910,7 @@ Then, after the installation finishes, install your preferred desktop environmen
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
sudo apt update
|
sudo apt update
|
||||||
sudo apt install gnome-core # change to kde-plasma-desktop
|
sudo apt install gnome-core # or kde-plasma-desktop
|
||||||
sudo systemctl reboot
|
sudo systemctl reboot
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user