From 9bd8cdf2ecc2b60f873b393122b19985cbc4587c Mon Sep 17 00:00:00 2001 From: Jasper Ras Date: Mon, 2 Jun 2025 12:15:47 +0200 Subject: vault backup: 2025-06-02 12:15:47 --- A nice way to select IPv4 addresses with JQ.md | 12 +++++++++ Bash process substition (tmp file).md | 13 ---------- Bash.md | 6 +++++ Building a command-line using arrays.md | 34 ++++++++++++++++++++++++++ Examples of proper JQ usage.md | 12 --------- Globbing patterns.md | 3 +++ Heredoc.md | 23 +++++++++++++++++ Nova compute evacuation.md | 10 ++++++++ Null substitution.md | 25 +++++++++++++++++++ Parameter subsitution.md | 8 ++++++ Process substition (tmp file).md | 15 ++++++++++++ Setting traps.md | 10 ++++++++ Shell.md | 5 ++++ daily/20-May-2025.md | 0 daily/27-May-2025.md | 4 +++ 15 files changed, 155 insertions(+), 25 deletions(-) create mode 100644 A nice way to select IPv4 addresses with JQ.md delete mode 100644 Bash process substition (tmp file).md create mode 100644 Bash.md create mode 100644 Building a command-line using arrays.md delete mode 100644 Examples of proper JQ usage.md create mode 100644 Heredoc.md create mode 100644 Nova compute evacuation.md create mode 100644 Null substitution.md create mode 100644 Parameter subsitution.md create mode 100644 Process substition (tmp file).md create mode 100644 Setting traps.md create mode 100644 Shell.md create mode 100644 daily/20-May-2025.md create mode 100644 daily/27-May-2025.md diff --git a/A nice way to select IPv4 addresses with JQ.md b/A nice way to select IPv4 addresses with JQ.md new file mode 100644 index 0000000..b8f751d --- /dev/null +++ b/A nice way to select IPv4 addresses with JQ.md @@ -0,0 +1,12 @@ +--- +tags: + - jq + - howto +--- +Double escapes are important! + +``` +jq -r '.fixed_ips[] | select(.ip_address | test("([0-9]+\\.)+")) | .ip_address' +``` + +An example of constructing an array from stdin and filtering empty lines is found in [[Using JQ to construct an array and filtering empty strings]]. \ No newline at end of file diff --git a/Bash process substition (tmp file).md b/Bash process substition (tmp file).md deleted file mode 100644 index 3540f70..0000000 --- a/Bash process substition (tmp file).md +++ /dev/null @@ -1,13 +0,0 @@ ---- -tags: - - bash - - howto ---- -Process substitution connects the output of a command to a file. This enables a command to accept the output of multiple commands. -Useful with diff for example: - -```bash -diff <(ls dirA) <(ls dirB) -``` - -> Important! Don't but the <() inside quotes.. it will report: cannot find file \ No newline at end of file diff --git a/Bash.md b/Bash.md new file mode 100644 index 0000000..239ac40 --- /dev/null +++ b/Bash.md @@ -0,0 +1,6 @@ +--- +tags: + - bash +--- +A scripting language in which a user can string together [[Shell]] commands in a reusable file called a script. The script can be executed (or "called") and it will execute the string of commands. +They are not full general purpose programming languages, even though they often have similar constructs available. \ No newline at end of file diff --git a/Building a command-line using arrays.md b/Building a command-line using arrays.md new file mode 100644 index 0000000..8e187c7 --- /dev/null +++ b/Building a command-line using arrays.md @@ -0,0 +1,34 @@ +--- +tags: + - bash +--- +[[Bash]] [[Globbing patterns]] + +--- +See https://www.shellcheck.net/wiki/SC2086 + +issue with building a command-line and prevent globbing by quoting parameter expansion, solution: use array. + +``` +#!/usr/bin/env bash +function test() { + echo "${@}" +} +function blaat() { + local flags + test hello "${flags[@]}" +} +function blaat1() { + local flags + flags=(a b c) + test hello "${flags[@]}" +} +blaat +blaat1 +``` + +``` +❯ bash /tmp/tmp.qI8lPlBQUe +hello +hello a b c +``` \ No newline at end of file diff --git a/Examples of proper JQ usage.md b/Examples of proper JQ usage.md deleted file mode 100644 index 2b68d01..0000000 --- a/Examples of proper JQ usage.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -tags: - - jq - - howto ---- -Double escapes are important! - -``` -jq -r '.fixed_ips[] | select(.ip_address | test("\\.")) | .ip_address' -``` - -An example of constructing an array from stdin and filtering empty lines is found in [[Using JQ to construct an array and filtering empty strings]]. \ No newline at end of file diff --git a/Globbing patterns.md b/Globbing patterns.md index 12b7582..bc7534c 100644 --- a/Globbing patterns.md +++ b/Globbing patterns.md @@ -1,6 +1,9 @@ --- tags: - linux +--- +[[Bash]] + --- https://linux.die.net/man/7/glob diff --git a/Heredoc.md b/Heredoc.md new file mode 100644 index 0000000..84c0fa5 --- /dev/null +++ b/Heredoc.md @@ -0,0 +1,23 @@ +--- +tags: + - bash +--- +[[Bash]] + +--- +``` +read -r -d '' VAR <`. I did it like this on the access node: +``` +nova --os-token gAAAAABoLXVPU1N-pYc8Xm7ypmWZX_hSuy7psMS_kudoYg6msHZdXYk_dordZB8Imm6-PT8916iPH5n9BvYLmHAQl7UGHuSNsfHZH8k_s1EPbfU8aE15s8Hu0ngNLW2amv_C-peJ8BZLHIl3K7cz2b05D0wRDnzbWl0h0w5NyY6aY_8YcMkVIzc --os-project-name hostnet --os-auth-url https://keystone.os1.openstack.group.one:5000/v3 host-evacuate n28.compute.env.vps1-cph8.one.com +``` + +[This](https://www.danplanet.com/blog/2016/03/03/evacuate-in-nova-one-command-to-confuse-us-all/) webpage has some useful information on evacuation in its different forms. \ No newline at end of file diff --git a/Null substitution.md b/Null substitution.md new file mode 100644 index 0000000..276a2e6 --- /dev/null +++ b/Null substitution.md @@ -0,0 +1,25 @@ +--- +tags: + - bash +--- +[[Bash]] [[Process substition (tmp file)]] [[Parameter subsitution]] + +--- +[This](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02) page contains a table with [[Parameter subsitution]] + +When we want to expand a variable into an argument to a command but the variable is empty it will expand to **an empty string**. This is often unwanted, we just want `null` which is removed from the command-line. + +*Example* +``` +openstack server delete server "${waitFlag}" +``` +if `waitFlag` is empty this will become: +``` +openstack server delete server '' +``` +which results in an error. + +This is where we can use `null substitution`: +``` +openstack server delete server "${waitFlag:+$waitFlag}" +``` \ No newline at end of file diff --git a/Parameter subsitution.md b/Parameter subsitution.md new file mode 100644 index 0000000..c50f231 --- /dev/null +++ b/Parameter subsitution.md @@ -0,0 +1,8 @@ +--- +tags: + - bash +--- +[[Bash]] [[Null substitution]] [[Process substition (tmp file)]] + +--- +[This](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02) page contains a useful table. \ No newline at end of file diff --git a/Process substition (tmp file).md b/Process substition (tmp file).md new file mode 100644 index 0000000..c9103c2 --- /dev/null +++ b/Process substition (tmp file).md @@ -0,0 +1,15 @@ +--- +tags: + - bash + - howto +--- +[[Bash]] [[Parameter subsitution]] [[Null substitution]] + +Process substitution connects the output of a command to a file. This enables another command to accept the output of multiple commands as files if it expects files. + +An example of this is `diff`, which outputs the difference between two files: +```bash +diff <(ls dirA) <(ls dirB) +``` + +> Important! Don't but the <() inside quotes.. it will report: cannot find file \ No newline at end of file diff --git a/Setting traps.md b/Setting traps.md new file mode 100644 index 0000000..cd36e80 --- /dev/null +++ b/Setting traps.md @@ -0,0 +1,10 @@ +--- +tags: + - bash +--- +With traps you can configure a function that is run upon a specific signal or when the script fails or exits normally. + +`trap fn ERR` > when an error occurrs +`trap fn EXIT` > any exit, also errors and interrupts +`trap fn SIGINT` > when interrupted +`trap fn ERR SIGINT` > either an interrupt or error occurred \ No newline at end of file diff --git a/Shell.md b/Shell.md new file mode 100644 index 0000000..ed90df2 --- /dev/null +++ b/Shell.md @@ -0,0 +1,5 @@ +--- +tags: + - linux +--- +A shell is a command-line interface; it interprets textual instructions (commands) to the operating system. \ No newline at end of file diff --git a/daily/20-May-2025.md b/daily/20-May-2025.md new file mode 100644 index 0000000..e69de29 diff --git a/daily/27-May-2025.md b/daily/27-May-2025.md new file mode 100644 index 0000000..908736b --- /dev/null +++ b/daily/27-May-2025.md @@ -0,0 +1,4 @@ +Decided that devenv is too invasive for use at work; I can't commit its .envrc nor .pre-commit-config.yaml. Therefore I'm setting up just regular shell.nix usage. I quickly ran into an issue where the nix shell is using bash and found a tool called Lorri which promises to be useful for developer environments with shell.nix. Let's hope that is less invasive than devenv. +Thus far it's not very nice, it install stuff on the background ( a systemd daemon that listens for change ) of which you have no idea when its done. + +TIL: gitlab ci job can specify `resource_group` to make it mutually exclusive with other jobs that have this resource group. \ No newline at end of file -- cgit v1.2.3