diff --git a/.vscode/settings.json b/.vscode/settings.json
index 450bd93..bf07811 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -50,12 +50,12 @@
"hadolint",
"helmfile",
"imager",
- "Istio",
+ "istio",
"istioctl",
"jdupes",
"journalctl",
"kbfs",
- "KEDA",
+ "keda",
"keepass",
"keybase",
"knockd",
@@ -84,6 +84,7 @@
"pkexec",
"polkit",
"portsnap",
+ "posix",
"poweroff",
"pvresize",
"radeon",
diff --git a/knowledge base/README.md b/knowledge base/README.md
index 36bfc67..83db10b 100644
--- a/knowledge base/README.md
+++ b/knowledge base/README.md
@@ -7,7 +7,8 @@ This is the collection of all notes, reminders and whatnot I gathered during the
- Prefer keeping an 80 characters width limit in code blocks.
This improves readability on most locations.
-- Always use an highlighting annotation when writing code blocks (default to `txt`).
+- Always use an highlighting annotation when writing code blocks
+ Default to `txt` if none is available.
- Use `sh` as highlighting annotation instead of `shell` when writing shell snippets in code blocks.
The local renderer just displays them better like this.
diff --git a/knowledge base/awk.md b/knowledge base/awk.md
index 0aba3e0..599c1c0 100644
--- a/knowledge base/awk.md
+++ b/knowledge base/awk.md
@@ -1,9 +1,41 @@
# `awk`
+## Table of contents
+
+1. [TL;DR](#tldr)
+1. [Further readings](#further-readings)
+
+## TL;DR
+
+```sh
+# Print only the 3rd column.
+awk '{print $3}' sales.txt
+cat sales.txt | awk '{print $3}'
+
+# Print the 2nd and 3rd columns, separated with a comma.
+awk '{print $2 ", " $3}' sales.txt
+cat sales.txt | awk '{print $2 ", " $3}'
+
+# Print the sum of the 2nd and 3rd columns.
+awk '{print $2 + $3}' sales.txt
+
+# Print only lines with a length of more than 20 characters.
+awk 'length($0) > 20' sales.txt
+
+# Print only lines where the value of the second column is greater than 100.
+awk '$2 > 100' sales.txt
+```
+
## Further readings
- [Print line only if number in third field is greater than X]
- [Printing the last column of a line in a file]
+- [The essential Bash cheat sheet]
+
+
+
+
[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
+[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
+[the essential bash cheat sheet]: https://betterprogramming.pub/the-essential-bash-cheat-sheet-e1c3df06560
diff --git a/knowledge base/bash.md b/knowledge base/bash.md
index 05965b2..d5495c4 100644
--- a/knowledge base/bash.md
+++ b/knowledge base/bash.md
@@ -17,10 +17,100 @@
## TL;DR
+Shortcuts:
+
+| Shortcut | Action |
+| ------------ | ------------------------------------------------------- |
+| `!!` | Insert the previous command in the current prompt. |
+| `Alt` + `.` | Insert the last argument in the current prompt. |
+| `Ctrl` + `L` | Clear the terminal. |
+| `Ctrl` + `R` | Search **backwards** in the history one step at a time. |
+| `Ctrl` + `Z` | Send the current foreground task to background. |
+
+Get help:
+
```sh
+# Get a brief summary about commands.
+help 'nano'
+
+# Get detailed information about commands.
+man 'parallel'
+```
+
+Session management:
+
+```sh
+# Clean the console.
+clear
+
+# Print the current directory.
+pwd
+
+# Change the current directory.
+cd
+cd /bin
+cd ..
+
+# Create local variables.
+VAR_NAME="value"
+
+# Convert local variables in environment ones.
+export VAR_NAME
+export VAR_NAME="value"
+
+# Deletes variables.
+unset MY_FRIENDS
+
+# Add directories to the current executables locations.
+export PATH="${PATH}:/home/user/bin"
+export PATH="/home/user/bin:${PATH}"
+
+# Show the path of executables in $PATH.
+which 'redis-cli'
+
+# Show the path, man pages, source code, etc of executables in $PATH.
+whereis nano
+
+# List existing aliases.
+alias
+
+# Create aliases.
+alias redo='$(history -p !!)'
+
+# Remove aliases.
+unalias redo
+
+# Print all environment variables.
+env
+
+# Print all local *and* environment variables.
+set
+( set -o posix ; set )
+
+# Print exported variables only.
+export -p
+
# Logout after 3 minutes of inactivity.
TMOUT=180
+```
+Piping:
+
+```sh
+# Use the output of a command as the input of another.
+tail 'file.txt' | grep 'search'
+
+# Save the output of command 'a' as 'file.txt'.
+# This *overwrites* already existing files with that name.
+a > 'file.txt'
+
+# Append the output of command 'a' to 'file.txt'.
+a >> 'file.txt'
+```
+
+Arrays:
+
+```sh
# Declare arrays.
ARRAY=(
"first_element"
@@ -42,7 +132,11 @@ echo ${ARRAY: -1}
# Get a slice of 4 elements from an array.
# Start from the element with index number 2.
echo ${ARRAY:2:4}
+```
+Functions:
+
+```sh
# Declare functions.
functionName () { … }
function functionName { … }
@@ -52,6 +146,43 @@ functionName () { command1 ; … ; command N ; }
# Get all the arguments in input.
echo $@
+```
+
+Error management:
+
+```sh
+# Run a command or function on exit, kill or error.
+trap "rm -f $tempfile" EXIT SIGTERM ERR
+trap function-name EXIT SIGTERM ERR
+
+# Disable CTRL-C.
+trap "" SIGINT
+
+# Re-enable CTRL-C.
+trap - SIGINT
+```
+
+Job control:
+
+```sh
+# Print a list of background tasks.
+jobs
+
+# Bring a background task in the foreground.
+fg
+fg 'task_number'
+```
+
+Other snippets:
+
+```sh
+# Copy and paste *on Linux*.
+echo "Hello my friend!" | xclip \
+&& xclip -o >> pasted_text.txt
+
+# Copy and paste *on Darwin*.
+echo "Hello my friend!" | pbcopy \
+&& pbpaste >> pasted_text.txt
# Of all the arguments in input, return only those which are existing directories.
DIRECTORIES=()
@@ -64,22 +195,6 @@ for (( I = $# ; I >= 0 ; I-- )); do
fi
done
-# Print all shell and environment variables.
-( set -o posix ; set )
-
-# Print exported variables only.
-export -p
-
-# Run a command or function on exit, kill or error.
-trap "rm -f $tempfile" EXIT SIGTERM ERR
-trap function-name EXIT SIGTERM ERR
-
-# Disable CTRL-C.
-trap "" SIGINT
-
-# Re-enable CTRL-C.
-trap - SIGINT
-
# Bash 3 and `sh` have no built-in means to convert case of a string, but the
# `awk`, `sed` or `tr` tools can be used instead.
echo $(echo "$name" | tr '[:upper:]' '[:lower:]' )
@@ -89,6 +204,11 @@ echo $(tr '[:upper:]' '[:lower:]' <<< "$name")
echo ${name,,}
echo ${name^^}
+# Leverage brace expansion to write less duplicated stuff.
+mv /tmp/readme.md{,.backup} # = mv /tmp/readme.md /tmp/readme.md.backup
+cp a{1,2,3}.txt backup-dir # = cp a1.txt a2.txt a3.txt backup-dir
+cp a{1..3}.txt backup-dir # = cp a1.txt a2.txt a3.txt backup-dir
+
# Add a clock to the top-right part of the terminal.
while sleep 1
do
@@ -99,7 +219,7 @@ do
done &
# Show a binary clock.
-watch -n 1 'echo "obase=2; `date +%s`" | bc'
+watch -n 1 'echo "obase=2; $(date +%s)" | bc'
# Fork bomb.
:(){ :|: & };:
@@ -249,14 +369,20 @@ echo $RECORDED
- [The Bash trap command]
- [Bash startup files loading order]
- [How to detect if a script is being sourced]
+- [The essential Bash cheat sheet]
+- [Speed up your command line navigation]
+- [6 Bash tricks you can use daily]
[trap]: trap.md
+[6 bash tricks you can use daily]: https://medium.com/for-linux-users/6-bash-tricks-you-can-use-daily-a32abdd8b13
[bash startup files loading order]: https://youngstone89.medium.com/unix-introduction-bash-startup-files-loading-order-562543ac12e9
[how to detect if a script is being sourced]: https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced#28776166
+[speed up your command line navigation]: https://blog.jread.com/speed-up-your-command-line-navigation-d4050207f02c
[the bash trap command]: https://www.linuxjournal.com/content/bash-trap-command
+[the essential bash cheat sheet]: https://betterprogramming.pub/the-essential-bash-cheat-sheet-e1c3df06560
[upper- or lower-casing strings]: https://scriptingosx.com/2019/12/upper-or-lower-casing-strings-in-bash-and-zsh/
diff --git a/knowledge base/sort.md b/knowledge base/sort.md
index a9bc001..58cdbf7 100644
--- a/knowledge base/sort.md
+++ b/knowledge base/sort.md
@@ -1,23 +1,35 @@
# Sort
+## Table of contents
+
+1. [TL;DR](#tldr)
+1. [Sort by the value in the last field](#sort-by-the-value-in-the-last-field)
+1. [Sources](#sources)
+
## TL;DR
```sh
# Sort given lines.
-sort path/to/file
+sort 'path/to/file'
# Sort lines in reverse.
-sort -r path/to/file
+sort -r 'path/to/file'
# Sort lines numerically.
-sort -n path/to/file
+sort -n 'path/to/file'
# Sort lines and remove duplicates.
-sort -u path/to/file
+sort -u 'path/to/file'
+
+# Sort lines in random order.
+sort -R 'path/to/file'
+
+# Sort lines numerically according to the value in the 3rd column.
+sort -t $'\t' -k 3n,3 'path/to/file'
# Sort by the value in the last field.
awk 'BEGIN {FS=","; OFS="|"} {print $NF,$0}' file.txt \
- | sort -n -t '|' | awk -F '|' '{print $NF}'
+| sort -n -t '|' | awk -F '|' '{print $NF}'
```
## Sort by the value in the last field
@@ -45,5 +57,11 @@ awk 'BEGIN {FS=","; OFS="|"} {print $NF,$0}' file.txt \
## Sources
- [Sort a file in Unix based on the last field]
+- [The essential Bash cheat sheet]
+
+
+
+
[sort a file in unix based on the last field]: http://www.unixcl.com/2010/11/sort-file-based-on-last-field-unix.html
+[the essential bash cheat sheet]: https://betterprogramming.pub/the-essential-bash-cheat-sheet-e1c3df06560
diff --git a/knowledge base/uniq.md b/knowledge base/uniq.md
index b642655..994f7c4 100644
--- a/knowledge base/uniq.md
+++ b/knowledge base/uniq.md
@@ -18,3 +18,13 @@ uniq -cu path/to/file
# Count duplicated lines only.
uniq -cd path/to/file
```
+
+## Sources
+
+- [The essential Bash cheat sheet]
+
+
+
+
+
+[the essential bash cheat sheet]: https://betterprogramming.pub/the-essential-bash-cheat-sheet-e1c3df06560