Imported knowledge from a private repository

This commit is contained in:
Michele Cereda
2023-02-23 20:11:42 +01:00
parent e9a22db73b
commit fb83703ef9
12 changed files with 499 additions and 12 deletions

9
knowledge base/awk.md Normal file
View File

@@ -0,0 +1,9 @@
# `awk`
## Further readings
- [Print line only if number in third field is greater than X]
- [Printing the last column of a line in a file]
[print line only if number in third field is greater than x]: https://unix.stackexchange.com/questions/395588/print-line-only-if-number-in-third-field-is-greater-than-x#395593
[Printing the last column of a line in a file]: https://stackoverflow.com/questions/13046167/printing-the-last-column-of-a-line-in-a-file#13046224

View File

@@ -1,8 +1,45 @@
# Bourne Again SHell
1. [TL;DR](#tldr)
2. [Startup files loading order](#startup-files-loading-order)
3. [Functions](#functions)
4. [Substitutions](#substitutions)
1. [!! (command substitution)](#-command-substitution)
2. [^^ (caret substitution)](#-caret-substitution)
5. [Here documents](#here-documents)
6. [Keys combinations](#keys-combinations)
7. [Check if a script is sourced by another](#check-if-a-script-is-sourced-by-another)
8. [Gotchas](#gotchas)
1. [Exist statuses of killed commands](#exist-statuses-of-killed-commands)
2. [Go incognito](#go-incognito)
9. [Further readings](#further-readings)
10. [Sources](#sources)
## TL;DR
```sh
# Declare arrays.
ARRAY=(
"first_element"
"second_element" "nth_element"
)
# Get the length of arrays.
# A.K.A. number of elements.
ARRAY_LEN=${#ARRAY[@]}
# Access all elements in arrays with referencing.
echo ${ARRAY[@]}
echo ${ARRAY[*]}
# Access the last value of arrays.
echo ${ARRAY[-1]}
echo ${ARRAY: -1}
# Get a slice of 4 elements from an array.
# Start from the element with index number 2.
echo ${ARRAY:2:4}
# Declare functions.
functionName () {}
function functionName {}
@@ -10,6 +47,23 @@ function functionName { … }
# Declare functions on a single line.
functionName () { command1 ;; command N ; }
# Get all the arguments in input.
echo $@
# Of all the arguments in input, return only those which are existing directories.
DIRECTORIES=()
for (( I = $# ; I >= 0 ; I-- )); do
if [[ -d ${@[$I]} ]]; then
DIRECTORIES+=${@[$I]}
else
local COMMAND="${@:1: 1-$I}"
break
fi
done
# Print all shell and environment variables.
( set -o posix ; set )
# Print exported variables only.
export -p
@@ -68,10 +122,118 @@ Upon exit:
A function automatically returns the exit code of the last command in it.
## Substitutions
### !! (command substitution)
Substitutes `!!` with the last command in your history
```sh
$ echo 'hallo!'
hallo!
$ !!
echo 'hallo!'
hallo!
$ sudo !!
sudo echo 'hallo!'
[sudo] password for user:
hallo!
```
### ^^ (caret substitution)
Re-runs a command replacing a string.
```sh
$ sudo apt search tmux
$ ^search^show
sudo apt show tmux
```
## Here documents
A _Here document_ (_heredoc_) is a type of redirection that allows you to pass multiple lines of input to a command.
```sh
[COMMAND] <<[-] 'DELIMITER'
HERE-DOCUMENT
DELIMITER
```
- the first line must start with an **optional command** followed by the special redirection operator `<<` and the **delimiting identifier**
- one can use **any string** as a delimiting identifier, the most commonly used being `EOF` or `END`
- if the delimiting identifier is **unquoted**, the shell will substitute all variables, commands and special characters before passing the here-document lines to the command
- appending a **minus sign** to the redirection operator (`<<-`), will cause all leading tab characters to be **ignored**
this allows one to use indentation when writing here-documents in shell scripts
leading whitespace characters are not allowed, only tabs are
- the here-document block can contain strings, variables, commands and any other type of input
- the last line must end with the delimiting identifier
white space in front of the delimiter is not allowed
```sh
$ cat << EOF
The current working directory is: $PWD
You are logged in as: $(whoami)
EOF
The current working directory is: /home/user
You are logged in as: user
```
```sh
$ cat <<-'EOF' | sed 's/l/e/g' > file.txt
Hello
World
EOF
$ cat file.txt
Heeeo
Wored
```
## Keys combinations
- `Ctrl+L`: clear the screen (instead of typing `clear`)
- `Ctrl+R`: reverse search your Bash history for a command that you have already run and wish to run again
## Check if a script is sourced by another
```sh
(return 0 2>/dev/null) && echo "this script is not sourced" || echo "this script is sourced"
(return 0 2>/dev/null) \
&& echo "this script is not sourced" \
|| echo "this script is sourced"
```
## Gotchas
### Exist statuses of killed commands
The exit status of a killed command is **128 + _n_** if the command was killed by signal _n_:
```sh
$ pgrep tryme.sh
880
$ kill -9 880
$ echo $?
137
```
### Go incognito
See [How do I open an incognito bash session] on [unix.stackexchange.com]
```sh
HISTFILE=
```
You can also avoid recording a single command simply preceding it with space
```shell
echo $RECORDED
echo $NOT_RECORDED
```
## Further readings
@@ -93,3 +255,29 @@ A function automatically returns the exit code of the last command in it.
[how to detect if a script is being sourced]: https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced#28776166
[the bash trap command]: https://www.linuxjournal.com/content/bash-trap-command
[upper- or lower-casing strings]: https://scriptingosx.com/2019/12/upper-or-lower-casing-strings-in-bash-and-zsh/
<!-- FIXME -->
[add directory to $path if it's not already there]: https://superuser.com/questions/39751/add-directory-to-path-if-its-not-already-there
[append elements to an array]: https://linuxhint.com/bash_append_array/
[bash-heredoc]: https://linuxize.com/post/bash-heredoc
[bashrc ps1 generator]: http://bashrcgenerator.com/
[check if a string represents a valid path]: https://unix.stackexchange.com/questions/214886/check-if-string-represents-valid-path-in-bash
[command line arguments in a shell script]: https://tecadmin.net/tutorial/bash-scripting/bash-command-arguments/
[find out the exit codes of all piped commands]: https://www.cyberciti.biz/faq/unix-linux-bash-find-out-the-exit-codes-of-all-piped-commands
[histsize vs histfilesize]: https://stackoverflow.com/questions/19454837/bash-histsize-vs-histfilesize#19454838
[how do i open an incognito bash session]: https://unix.stackexchange.com/questions/158933/how-do-i-open-an-incognito-bash-session/158937#158937
[how to find a bash shell array length]: https://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/
[how to slice an array]: https://stackoverflow.com/questions/1335815/how-to-slice-an-array-in-bash#1336245
[is there a way of reading the last element of an array?]: https://unix.stackexchange.com/questions/198787/is-there-a-way-of-reading-the-last-element-of-an-array-with-bash#198789
[linux-terminal-trick]: https://opensource.com/article/20/1/linux-terminal-trick
[printing array elements in reverse]: https://www.unix.com/shell-programming-and-scripting/267967-printing-array-elements-reverse.html
[regular expressions in a case statement]: https://stackoverflow.com/questions/9631335/regular-expressions-in-a-bash-case-statement#9631449
[reverse an array]: https://unix.stackexchange.com/questions/412868/bash-reverse-an-array
[set]: https://ss64.com/bash/set.html
[slice of positional parameters]: https://unix.stackexchange.com/questions/82060/bash-slice-of-positional-parameters#82061
[how to list all variables names and their current values?]: https://askubuntu.com/questions/275965/how-to-list-all-variables-names-and-their-current-values#275972
[linuxize.com]: https://linuxize.com
[opensource.com]: https://opensource.com
[unix.stackexchange.com]: https://unix.stackexchange.com

View File

@@ -3,9 +3,15 @@
1. [TL;DR](#tldr)
2. [Client management](#client-management)
1. [Remote management](#remote-management)
3. [Use the GPU for computation](#use-the-gpu-for-computation)
3. [Use the GPU for computations](#use-the-gpu-for-computations)
1. [On OpenSUSE](#on-opensuse)
4. [Further readings](#further-readings)
2. [Check the GPU is OpenCL-enabled](#check-the-gpu-is-opencl-enabled)
4. [Use VirtualBox for computations](#use-virtualbox-for-computations)
5. [Gotchas](#gotchas)
6. [Further readings](#further-readings)
Files are located in `/var/lib/boinc` by default.
Some distribution (debian and derivate) use `/etc/boinc-client` for configuration files instead, and create links to the ones in the default location.
## TL;DR
@@ -26,8 +32,14 @@ Name | Type | Description
[boinccmd] | Command line |
[boinctui] | Text |
Local control RPCs are authenticated using the GUI RPC password. This password is located in the `gui_rpc_auth.cfg` configuration file, as the single first line, with a max length of 255 characters.
A password is **required** from version FIXME, and is automatically generated if the file is not found or it is empty.
### Remote management
All remote RPCs (both status and control) are authenticated using the GUI RPC password.
Quick, dirty solution: use the `--allow_remote_gui_rpc` option when starting the client.<br/>
This will make the BOINC client accept connections from **any** host (subject to password authentication) even if the client's configuration files are set otherwise.
@@ -42,12 +54,21 @@ Better solution:
1. check the `gui_rpc_auth.cfg` file in the BOINC data directory to get the password for authentication
## Use the GPU for computation
## Use the GPU for computations
Also see [AMD Linux drivers] and [Radeon™ Software for Linux® Installation].
To use the GPU for projects you may need the proprietary nVidia or AMD drivers. See [AMD Linux drivers] and [Radeon™ Software for Linux® Installation].
```sh
amdgpu-install --usecase=workstation --opencl=rocr
```
The BOINC client seems to need to be added to the `video` group to be able to use the drivers correctly - this is something I still need to check.
```sh
gpasswd -a 'boinc' 'video'
usermod --append --groups 'video' 'boinc'
```
### On OpenSUSE
Install the `amdgpu-install` package from [AMD's Linux drivers][amd linux drivers] page, then execute it.
@@ -65,11 +86,41 @@ At the next restart of the boinc-client, something similar to this line should a
Oct 09 23:09:40 hostnameHere boinc[1709]: 09-Oct-2022 23:09:40 [---] OpenCL: AMD/ATI GPU 0: gfx90c:xnack- (driver version 3452.0 (HSA1.1,LC), device ve>
```
### Check the GPU is OpenCL-enabled
Just install and run `clinfo`:
```sh
$ clinfo
Number of platforms 1
Platform Name NVIDIA CUDA
Platform Vendor NVIDIA Corporation
Platform Version OpenCL 1.2 CUDA 10.0.132
```
## Use VirtualBox for computations
Install VirtualBox, then add the `boinc` user to the `vboxusers` group:
```sh
usermod --append --groups 'vboxusers' 'boinc'
```
## Gotchas
- it seems to work much better on debian-based distribution
- In order to suspend computing when the computer is in use, the `boinc` user should have access to your X session so that mouse and keyboard input can be communicated to the client:
```sh
xhost +SI:localuser:boinc
```
## Further readings
- [BOINC Manager]
- [boinccmd]
- [boinctui]
- [boinccmd] for the bare CLI utility
- [boinctui] for a TUI manager
- [GUI RPC bind to port 31416 failed: 98]
<!-- internal references -->
@@ -83,3 +134,11 @@ Oct 09 23:09:40 hostnameHere boinc[1709]: 09-Oct-2022 23:09:40 [---] OpenCL: AMD
[radeon™ software for linux® installation]: https://amdgpu-install.readthedocs.io/en/latest/
[gui rpc bind to port 31416 failed: 98]: https://boinc.mundayweb.com/wiki/index.php?title=GUI_RPC_bind_to_port_31416_failed:_98
<!-- FIXME -->
[boinc on arch wiki]: https://wiki.archlinux.org/title/BOINC
[client configuration]: https://boinc.berkeley.edu/wiki/Client_configuration
[controlling boinc remotely]: https://boinc.berkeley.edu/wiki/Controlling_BOINC_remotely
[installing or uninstalling the amdgpu stack]: https://amdgpu-install.readthedocs.io/en/latest/install-installing.html
[linux suspend when computer is in use bug]: https://boinc.berkeley.edu/dev/forum_thread.php?id=14019&postid=101146#101146

View File

@@ -1,8 +1,8 @@
# Google cloud platform CLI <!-- omit in toc -->
- [TL;DR](#tldr)
- [Further readings](#further-readings)
- [Sources](#sources)
1. [TL;DR](#tldr)
2. [Further readings](#further-readings)
3. [Sources](#sources)
## TL;DR

10
knowledge base/history.md Normal file
View File

@@ -0,0 +1,10 @@
# History
1. [TL;DR](#tldr)
## TL;DR
```sh
# Delete everything.
history -c
```

View File

@@ -0,0 +1,9 @@
# HTTP response status codes
## Further readings
- [MDN]
- [http.cat]
[mdn]: https://developer.mozilla.org/nl/docs/Web/HTTP/Status
[http.cat]: https://http.cat/

View File

@@ -3,8 +3,8 @@
## TL;DR
```sh
# filter elements
# only works on arrays, not on maps
# Filter elements.
# Only works on arrays, not on maps.
kubectl get serviceaccounts \
-o jsonpath="{.items[?(@.metadata.name!='default')].metadata.name}"
```
@@ -12,5 +12,7 @@ kubectl get serviceaccounts \
## Further readings
- [JSONPath Syntax]
- [Live editor]
[jsonpath syntax]: https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html
[live editor]: https://json8.github.io/patch/demos/apply/

10
knowledge base/nilfs.md Normal file
View File

@@ -0,0 +1,10 @@
# NilFS
* -O _feature_: set feature
* -m _percentage_: set percentage of segments reserved to garbage collection (default: 5)
* -n: dry run
* -v: verbose
```sh
sudo mkfs -t 'nilfs2' -L 'label' -O 'block_count' -v '/dev/sdb1'
```

View File

@@ -0,0 +1,65 @@
# Optimize battery on a linux system
1. [TL;DR](#tldr)
2. [Disable unused services](#disable-unused-services)
3. [Improve battery performance](#improve-battery-performance)
4. [Further readings](#further-readings)
## TL;DR
```sh
# Summarize performance of the last boot.
sudo systemd-analyze
# Show last boot performance.
# Also shows the process tree.
sudo systemd-analyze critical-chain
# Check power stats.
sudo 'powertop'
```
## Disable unused services
```sh
$ sudo systemd-analyze
Startup finished in 13.129s (firmware) + 5.413s (loader) + 1.746s (kernel) + 7.903s (userspace) = 28.192s
graphical.target reached after 1.239s in userspace
$ sudo systemd-analyze critical-chain
The time when unit became active or started is printed after the "@" character.
The time the unit took to start is printed after the "+" character.
graphical.target @1.239s
└─multi-user.target @1.239s
└─ModemManager.service @1.154s +84ms
└─polkit.service @937ms +215ms
└─basic.target @928ms
└─sockets.target @928ms
└─dbus.socket @928ms
└─sysinit.target @924ms
└─systemd-backlight@backlight:acpi_video0.service @2.273s +8ms
└─system-systemd\x2dbacklight.slice @2.272s
└─system.slice @197ms
└─-.slice @197ms
```
## Improve battery performance
```sh
# Enable automatic power management.
# See `tlpui` on GitHub for UI.
sudo systemctl enable --now 'tlp.service'
sudo vim '/etc/tlp.conf'
# Check power stats.
sudo 'powertop'
```
## Further readings
- [laptop-mode-tools]
- [laptop-mode-tools in the Arch Wiki]
[laptop-mode-tools]: https://www.unixmen.com/laptop-mode-tools-extend-laptop-battery-life/
[laptop-mode-tools in the arch wiki]: https://wiki.archlinux.org/title/Laptop_Mode_Tools