diff options
| author | Paul Buetow <paul@buetow.org> | 2023-04-01 15:55:00 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-04-01 15:55:00 +0300 |
| commit | c199ee45cf7bf4388305cb5c6e92f130b5737d05 (patch) | |
| tree | 2fdad7525b19b5b20466a8ade7606deb42f2e86a /gemfeed | |
| parent | d027937949a7bb02ee800271cc9a9b506f1a75d3 (diff) | |
Update content for gemtext
Diffstat (limited to 'gemfeed')
| -rw-r--r-- | gemfeed/atom.xml | 312 |
1 files changed, 156 insertions, 156 deletions
diff --git a/gemfeed/atom.xml b/gemfeed/atom.xml index 4babf082..6d7c384d 100644 --- a/gemfeed/atom.xml +++ b/gemfeed/atom.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> - <updated>2023-03-27T23:05:17+03:00</updated> + <updated>2023-04-01T15:50:57+03:00</updated> <title>foo.zone feed</title> <subtitle>To be in the .zone!</subtitle> <link href="gemini://foo.zone/gemfeed/atom.xml" rel="self" /> @@ -31,7 +31,7 @@ |o=======.| |:::::::[]| jgs `"""""""""` |o=======.| mod. by Paul Buetow `"""""""""` -</pre><br /> +</pre> <p>I proudly announce that I've released Gemtexter version <span class="inlinecode">2.0.0</span>. What is Gemtexter? It's my minimalist static site generator for Gemini Gemtext, HTML and Markdown written in GNU Bash.</p> <a class="textlink" href="https://codeberg.org/snonux/gemtexter">https://codeberg.org/snonux/gemtexter</a><br /> <p>This is a new major release, so it contains a breaking change (see "Meta cache made obsolete").</p> @@ -52,7 +52,7 @@ Welcome to this capsule! echo Multiline template line $i done >>> -</pre><br /> +</pre> <p>... results into the following <span class="inlinecode">index.gmi</span> after running <span class="inlinecode">./gemtexter --generate</span> (or <span class="inlinecode">./gemtexter --template</span>, which instructs to do only template processing and nothing else):</p> <pre> # Hello world @@ -71,7 +71,7 @@ Multiline template line 7 Multiline template line 8 Multiline template line 9 Multiline template line 10 -</pre><br /> +</pre> <p>Another thing you can do is insert an index with links to similar blog posts. E.g.:</p> <pre> See more entries about DTail and Golang: @@ -79,7 +79,7 @@ See more entries about DTail and Golang: << template::inline::index dtail golang Blablabla... -</pre><br /> +</pre> <p>... scans all other post entries with <span class="inlinecode">dtail</span> and <span class="inlinecode">golang</span> in the file name and generates a link list like this:</p> <pre> See more entries about DTail and Golang: @@ -90,14 +90,14 @@ See more entries about DTail and Golang: => ./2021-04-22-dtail-the-distributed-log-tail-program.gmi 2021-04-22 DTail - The distributed log tail program (You are currently reading this) Blablabla... -</pre><br /> +</pre> <h2>Added hooks</h2> <p>You can configure <span class="inlinecode">PRE_GENERATE_HOOK</span> and <span class="inlinecode">POST_PUBLISH_HOOK</span> to point to scripts to be executed before running <span class="inlinecode">--generate</span>, or after running <span class="inlinecode">--publish</span>. E.g. you could populate some of the content by an external script before letting Gemtexter do its thing or you could automatically deploy the site after running <span class="inlinecode">--publish</span>.</p> <p>The sample config file <span class="inlinecode">gemtexter.conf</span> includes this as an example now; these scripts will only be executed when they actually exist:</p> <pre> declare -xr PRE_GENERATE_HOOK=./pre_generate_hook.sh declare -xr POST_PUBLISH_HOOK=./post_publish_hook.sh -</pre><br /> +</pre> <h2>Use of safer Bash options</h2> <p>Gemtexter now does <span class="inlinecode">set -euf -o pipefile</span>, which helps to eliminate bugs and to catch scripting errors sooner. Previous versions only <span class="inlinecode">set -e</span>.</p> <h2>Meta cache made obsolete</h2> @@ -108,7 +108,7 @@ declare -xr POST_PUBLISH_HOOK=./post_publish_hook.sh # Title here The remaining content of the Gemtext file... -</pre><br /> +</pre> <p>Gemtexter will add a line starting with <span class="inlinecode">> Published at ...</span> now. Any subsequent Atom feed generation will then use that date.</p> <pre> % cat gemfeed/2023-02-26-title-here.gmi @@ -117,7 +117,7 @@ The remaining content of the Gemtext file... > Published at 2023-02-26T21:43:51+01:00 The remaining content of the Gemtext file... -</pre><br /> +</pre> <h2>XMLLint support</h2> <p>Optionally, when the <span class="inlinecode">xmllint</span> binary is installed, Gemtexter will perform a simple XML lint check against the Atom feed generated. This is a double-check of whether the Atom feed is a valid XML.</p> <h2>More</h2> @@ -654,7 +654,7 @@ nmap ,j :call OpenJournalPage()<CR> `|/-. \ / \ /,---`\ kat / `._____V_____V' ' ' -</pre><br /> +</pre> <p>This will be a quick blog post, as I am busy with my personal life now. I have relocated to a different country and am still busy arranging things. So bear with me :-)</p> <p> In this post, I want to give a quick overview (or how-to) about installing DTail on OpenBSD, as the official documentation only covers Red Hat and Fedora Linux! And this blog post will also be used as my reference!</p> <a class="textlink" href="https://dtail.dev">https://dtail.dev</a><br /> @@ -665,7 +665,7 @@ nmap ,j :call OpenJournalPage()<CR> <p>First of all, DTail needs to be downloaded and compiled. For that, <span class="inlinecode">git</span>, <span class="inlinecode">go</span>, and <span class="inlinecode">gmake</span> are required:</p> <pre> $ doas pkg_add git go gmake -</pre><br /> +</pre> <p>I am happy that the Go Programming Language is readily available in the OpenBSD packaging system. Once the dependencies got installed, clone DTail and compile it:</p> <pre> $ mkdir git @@ -673,18 +673,18 @@ $ cd git $ git clone https://github.com/mimecast/dtail $ cd dtail $ gmake -</pre><br /> +</pre> <p>You can verify the version by running the following command:</p> <pre> $ ./dtail --version DTail 4.1.0 Protocol 4.1 Have a lot of fun! $ file dtail dtail: ELF 64-bit LSB executable, x86-64, version 1 -</pre><br /> +</pre> <p>Now, there isn't any need anymore to keep <span class="inlinecode">git</span>, <span class="inlinecode">go</span> and <span class="inlinecode">gmake</span>, so they can be deinstalled now:</p> <pre> $ doas pkg_delete git go gmake -</pre><br /> +</pre> <p>One day I shall create an official OpenBSD port for DTail.</p> <h2>Install it</h2> <p>Installing the binaries is now just a matter of copying them to <span class="inlinecode">/usr/local/bin</span> as follows:</p> @@ -693,12 +693,12 @@ $ for bin in dserver dcat dgrep dmap dtail dtailhealth; do doas cp -p $bin /usr/local/bin/$bin doas chown root:wheel /usr/local/bin/$bin done -</pre><br /> +</pre> <p>Also, we will be creating the <span class="inlinecode">_dserver</span> service user:</p> <pre> $ doas adduser -class nologin -group _dserver -batch _dserver $ doas usermod -d /var/run/dserver/ _dserver -</pre><br /> +</pre> <p>The OpenBSD init script is created from scratch (not part of the official DTail project). Run the following to install the bespoke script:</p> <pre> $ cat <<'END' | doas tee /etc/rc.d/dserver @@ -720,7 +720,7 @@ rc_pre() { rc_cmd $1 & END $ doas chmod 755 /etc/rc.d/dserver -</pre><br /> +</pre> <h3>Rexification</h3> <p>This is the task for setting it up via Rex. Note the <span class="inlinecode">. . . .</span>, that's a placeholder which we will fill up more and more during this blog post:</p> <pre> @@ -744,14 +744,14 @@ task 'dtail', group => 'frontends', service 'dserver' => 'restart' if $restart; service 'dserver', ensure => 'started'; }; -</pre><br /> +</pre> <h2>Configure it</h2> <p>Now, DTail is fully installed but still needs to be configured. Grab the default config file from GitHub ...</p> <pre> $ doas mkdir /etc/dserver $ curl https://raw.githubusercontent.com/mimecast/dtail/master/samples/dtail.json.sample | doas tee /etc/dserver/dtail.json -</pre><br /> +</pre> <p>... and then edit it and adjust <span class="inlinecode">LogDir</span> in the <span class="inlinecode">Common</span> section to <span class="inlinecode">/var/log/dserver</span>. The result will look like this:</p> <pre> "Common": { @@ -762,7 +762,7 @@ $ curl https://raw.githubusercontent.com/mimecast/dtail/master/samples/dtail.jso "SSHPort": 2222, "LogLevel": "Info" } -</pre><br /> +</pre> <h3>Rexification</h3> <p>That's as simple as adding the following to the Rex task:</p> <pre> @@ -775,7 +775,7 @@ file '/etc/dserver/dtail.json', group => 'wheel', mode => '755', on_change => sub { $restart = TRUE }; -</pre><br /> +</pre> <h2>Update the key cache for it</h2> <p>DTail relies on SSH for secure authentication and communication. However, the system user <span class="inlinecode">_dserver</span> has no permission to read the SSH public keys from the user's home directories, so the DTail server also checks for available public keys in an alternative path <span class="inlinecode">/var/run/dserver/cache</span>. </p> <p>The following script, populating the DTail server key cache, can be run periodically via <span class="inlinecode">CRON</span>:</p> @@ -817,12 +817,12 @@ done echo 'All set...' END $ doas chmod 500 /usr/local/bin/dserver-update-key-cache.sh -</pre><br /> +</pre> <p>Note that the script above is a slight variation of the official DTail script. The official DTail one is a <span class="inlinecode">bash</span> script, but on OpenBSD, there's <span class="inlinecode">ksh</span>. I run it once daily by adding it to the <span class="inlinecode">daily.local</span>:</p> <pre> $ echo /usr/local/bin/dserver-update-key-cache.sh | doas tee -a /etc/daily.local /usr/local/bin/dserver-update-key-cache.sh -</pre><br /> +</pre> <h3>Rexification</h3> <p>That's done by adding ...</p> <pre> @@ -833,7 +833,7 @@ file '/usr/local/bin/dserver-update-key-cache.sh', mode => '500'; append_if_no_such_line '/etc/daily.local', '/usr/local/bin/dserver-update-key-cache.sh'; -</pre><br /> +</pre> <p>... to the Rex task!</p> <h2>Start it</h2> <p>Now, it's time to enable and start the DTail server:</p> @@ -857,7 +857,7 @@ INFO|1022-090739|86050|stats.go:53|2|11|7|||MAPREDUCE:STATS|currentConnections=0 . . Ctr+C -</pre><br /> +</pre> <p>As we don't want to wait until tomorrow, let's populate the key cache manually:</p> <pre> $ doas /usr/local/bin/dserver-update-key-cache.sh @@ -869,7 +869,7 @@ Caching /home/git/.ssh/authorized_keys -> /var/cache/dserver/git.authorized_k Caching /home/paul/.ssh/authorized_keys -> /var/cache/dserver/paul.authorized_keys Caching /home/rex/.ssh/authorized_keys -> /var/cache/dserver/rex.authorized_keys All set... -</pre><br /> +</pre> <h2>Use it</h2> <p>The DTail server is now ready to serve connections. You can use any DTail commands, such as <span class="inlinecode">dtail</span>, <span class="inlinecode">dgrep</span>, <span class="inlinecode">dmap</span>, <span class="inlinecode">dcat</span>, <span class="inlinecode">dtailhealth</span>, to do so. Checkout out all the usage examples on the official DTail page.</p> <p>I have installed DTail server this way on my personal OpenBSD frontends <span class="inlinecode">blowfish</span>, and <span class="inlinecode">fishfinger</span>, and the following command connects as user <span class="inlinecode">rex</span> to both machines and greps the file <span class="inlinecode">/etc/fstab</span> for the string <span class="inlinecode">local</span>:</p> @@ -883,13 +883,13 @@ CLIENT|earth|INFO|STATS:STATS|cgocalls=11|cpu=8|connected=2|servers=2|connected% CLIENT|earth|INFO|Added hosts to known hosts file|/home/paul/.ssh/known_hosts REMOTE|blowfish|100|7|fstab|31bfd9d9a6788844.h /usr/local ffs rw,wxallowed,nodev 1 2 REMOTE|fishfinger|100|7|fstab|093f510ec5c0f512.h /usr/local ffs rw,wxallowed,nodev 1 2 -</pre><br /> +</pre> <p>Running it the second time, and given that you trusted the keys the first time, it won't prompt you for the host keys anymore:</p> <pre> ❯ ./dgrep -user rex -servers blowfish.buetow.org,fishfinger.buetow.org --regex local /etc/fstab REMOTE|blowfish|100|7|fstab|31bfd9d9a6788844.h /usr/local ffs rw,wxallowed,nodev 1 2 REMOTE|fishfinger|100|7|fstab|093f510ec5c0f512.h /usr/local ffs rw,wxallowed,nodev 1 2 -</pre><br /> +</pre> <h2>Conclusions</h2> <p>It's a bit of manual work, but it's ok on this small scale! I shall invest time in creating an official OpenBSD port, though. That would render most of the manual steps obsolete, as outlined in this post!</p> <p>Check out the following for more information:</p> @@ -1004,7 +1004,7 @@ jgs (________\ \ |:::::::[]| |o=======.| jgs `"""""""""` -</pre><br /> +</pre> <p>I proudly announce that I've released Gemtexter version <span class="inlinecode">1.1.0</span>. What is Gemtexter? It's my minimalist static site generator for Gemini Gemtext, HTML and Markdown written in GNU Bash.</p> <a class="textlink" href="https://codeberg.org/snonux/gemtexter">https://codeberg.org/snonux/gemtexter</a><br /> <p>It has been around a year since I released the first version <span class="inlinecode">1.0.0</span>. Although, there aren't any groundbreaking changes, there have been a couple of smaller commits and adjustments. I was quite surprised that I received a bunch of feedback and requests about Gemtexter so it means that I am not the only person in the universe actually using it.</p> @@ -1029,7 +1029,7 @@ check_dependencies () { fi done } -</pre><br /> +</pre> <p>Especially macOS users didn't read the <span class="inlinecode">README</span> carefully enough to install GNU Grep, GNU Sed and GNU Date before using Gemtexter.</p> <h3>Backticks now produce <span class="inlinecode">inline code blocks</span> in the HTML output</h3> <p>The Gemtext format doesn't support inline code blocks, but Gemtexter now produces <span class="inlinecode">inline code blocks</span> (means, small code fragments can be placed in the middle of a paragraph) in the HTML output when the code block is enclosed with Backticks. There were no adjustments required for the Markdown output format, because Markdown supports it already out of the box.</p> @@ -1039,7 +1039,7 @@ check_dependencies () { <p>Once your capsule reaches a certain size, it can become annoying to re-generate everything if you only want to preview the HTML or Markdown output of one single content file. The following will add a filter to only generate the files matching a regular expression:</p> <pre> ./gemtexter --generate '.*hello.*' -</pre><br /> +</pre> <h3>Revamped <span class="inlinecode">git</span> support</h3> <p>The Git support has been completely rewritten. It's now more reliable and faster too. Have a look at the <span class="inlinecode">README</span> for more information.</p> <h3>Addition of <span class="inlinecode">htmlextras</span> and web font support</h3> @@ -2159,7 +2159,7 @@ learn () { ] ~ ~ | | | | | -</pre><br /> +</pre> <p>I have recently released DTail 4.0.0 and this blog post goes through all the new goodies. If you want to jump directly to DTail, do it here (there are nice animated gifs which demonstrates the usage pretty well):</p> <a class="textlink" href="https://dtail.dev">https://dtail.dev</a><br /> <h2>So, what's new in 4.0.0?</h2> @@ -2182,7 +2182,7 @@ const ( Trace level = iota All level = iota ) -</pre><br /> +</pre> <p>DTail also supports multiple log outputs (e.g. to file or to stdout). More are now easily pluggable with the new logging package. The output can also be "enriched" (default) or "plain" (read more about that further below).</p> <h3>Configurable terminal color codes</h3> <p>A complaint I received from the users of DTail 3 were the terminal colors used for the output. Under some circumstances (terminal configuration) it made the output difficult to read so that users defaulted to "--noColor" (disabling colored output completely). I toke it by heart and also rewrote the color handling. It's now possible to configure the foreground and background colors and an attribute (e.g. dim, bold, ...).</p> @@ -2280,7 +2280,7 @@ const ( }, ... } -</pre><br /> +</pre> <p>You notice the different sections - these are different contexts:</p> <ul> <li>Remote: Color configuration for all log lines sent remotely from the server to the client. </li> @@ -2292,40 +2292,40 @@ const ( <p>When you do so, make sure that you check your "dtail.json" against the JSON schema file. This is to ensure that you don't configure an invalid color accidentally (requires "jsonschema" to be installed on your computer). Furthermore, the schema file is also a good reference for all possible colors available:</p> <pre> jsonschema -i dtail.json schemas/dtail.schema.json -</pre><br /> +</pre> <h3>Serverless mode</h3> <p>All DTail commands can now operate on log files (and other text files) directly without any DTail server running. So there isn't a need anymore to install a DTail server when you are on the target server already anyway, like the following example shows:</p> <pre> % dtail --files /var/log/foo.log -</pre><br /> +</pre> <p>or</p> <pre> % dmap --files /var/log/foo.log --query 'from TABLE select .... outfile result.csv' -</pre><br /> +</pre> <p>The way it works in Go code is that a connection to a server is managed through an interface and in serverless mode DTail calls through that interface to the server code directly without any TCP/IP and SSH connection made in the background. This means, that the binaries are a bit larger (also ship with the code which normally would be executed by the server) but the increase of binary size is not much.</p> <h3>Shorthand flags</h3> <p>The "--files" from the previous example is now redundant. As a shorthand, It is now possible to do the following instead:</p> <pre> % dtail /var/log/foo.log -</pre><br /> +</pre> <p>Of course, this also works with all other DTail client commands (dgrep, dcat, ... etc).</p> <h3>Spartan (aka plain) mode</h3> <p>There's a plain mode, which makes DTail only print out the "plain" text of the files operated on (without any DTail specific enriched output). E.g.:</p> <pre> % dcat --plain /etc/passwd > /etc/test % diff /etc/test /etc/passwd # Same content, no diff -</pre><br /> +</pre> <p>This might be useful if you wanted to post-process the output. </p> <h3>Standard input pipe</h3> <p>In serverless mode, you might want to process your data in a pipeline. You can do that now too through an input pipe:</p> <pre> % dgrep --plain --regex 'somethingspecial' /var/log/foo.log | dmap --query 'from TABLE select .... outfile result.csv' -</pre><br /> +</pre> <p>Or, use any other "standard" tool:</p> <pre> % awk '.....' < /some/file | dtail .... -</pre><br /> +</pre> <h3>New command dtailhealth</h3> <p>Prior to DTail 4, there was a flag for the "dtail" command to check the health of a remote DTail server (for use with monitoring system such as Nagios). That has been moved out to a separate binary to reduce complexity of the "dtail" command. The following checks whether DTail is operational on the current machine (you could also check a remote instance of DTail server, just adjust the server address).</p> <pre> @@ -2333,7 +2333,7 @@ jsonschema -i dtail.json schemas/dtail.schema.json #!/bin/sh exec /usr/local/bin/dtailhealth --server localhost:2222 -</pre><br /> +</pre> <h3>Improved documentation</h3> <p>Some features, such as custom log formats and the map-reduce query language, are now documented. Also, the examples have been updated to reflect the new features added. This also includes the new animated example Gifs (plus documentation how they were created).</p> <p>I must admit that not all features are documented yet:</p> @@ -2349,7 +2349,7 @@ exec /usr/local/bin/dtailhealth --server localhost:2222 <p>How are the tests implemented? All integration tests are simply unit tests in the "./integrationtests" folder. They must be explicitly activated with:</p> <pre> % export DTAIL_INTEGRATION_TEST_RUN_MODE=yes -</pre><br /> +</pre> <p>Once done, first compile all commands, and then run the integration tests:</p> <pre> % make @@ -2358,7 +2358,7 @@ exec /usr/local/bin/dtailhealth --server localhost:2222 . % go clean -testcache % go test -race -v ./integrationtests -</pre><br /> +</pre> <h3>Improved code</h3> <p>Not that the code quality of DTail has been bad (I have been using Go vet and Go lint for previous releases and will keep using these), but this time I had new tools (such as SonarQube and BlackDuck) in my arsenal to:</p> <ul> @@ -2627,7 +2627,7 @@ GNU/kFreeBSD rhea.buetow.org 8.0-RELEASE-p5 FreeBSD 8.0-RELEASE-p5 #2: Sat Nov 2 / / . / / .' . | jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Art by Joan Stark, mod. by Paul Buetow -</pre><br /> +</pre> <p>This is the second blog post about my Bash Golf series. This series is random Bash tips, tricks and weirdnesses I came across. It's a collection of smaller articles I wrote in an older (in German language) blog, which I translated and refreshed with some new content.</p> <a class="textlink" href="https://foo.zone/gemfeed/2022-01-01-bash-golf-part-2.html">2022-01-01 Bash Golf Part 2 (You are currently reading this)</a><br /> <a class="textlink" href="https://foo.zone/gemfeed/2021-11-29-bash-golf-part-1.html">2021-11-29 Bash Golf Part 1</a><br /> @@ -2646,14 +2646,14 @@ lrwx------. 1 paul paul 64 Nov 23 09:46 0 -> /dev/pts/9 lrwx------. 1 paul paul 64 Nov 23 09:46 1 -> /dev/pts/9 lrwx------. 1 paul paul 64 Nov 23 09:46 2 -> /dev/pts/9 lr-x------. 1 paul paul 64 Nov 23 09:46 3 -> /proc/162912/fd -</pre><br /> +</pre> <p>The following examples demonstrate two different ways to accomplish the same thing. The difference is that the first command is directly printing out "Foo" to stdout and the second command is explicitly redirecting stdout to its own stdout file descriptor:</p> <pre> ❯ echo Foo Foo ❯ echo Foo > /proc/self/fd/0 Foo -</pre><br /> +</pre> <p>Other useful redirections are:</p> <ul> <li>Redirect stderr to stdin: "echo foo 2>&1"</li> @@ -2663,13 +2663,13 @@ Foo <pre> ❯ echo Foo 1>&2 2>/dev/null Foo -</pre><br /> +</pre> <p class="quote"><i>Update: A reader sent me an email and pointed out that the order of the redirections is important. </i></p> <p>As you can see, the following will not print out anything:</p> <pre> ❯ echo Foo 2>/dev/null 1>&2 ❯ -</pre><br /> +</pre> <p>A good description (also pointed out by the reader) can be found here:</p> <a class="textlink" href="https://wiki.bash-hackers.org/howto/redirection_tutorial#order_of_redirection_ie_file_2_1_vs_2_1_file">Order of redirection</a><br /> <p>Ok, back to the original blog post. You can also use grouping here (neither of these commands will print out anything to stdout):</p> @@ -2679,7 +2679,7 @@ Foo ❯ { { { echo Foo 1>&2; } 2>&1; } 1>&2; } 2>/dev/null ❯ ( ( ( echo Foo 1>&2; ) 2>&1; ) 1>&2; ) 2>/dev/null ❯ -</pre><br /> +</pre> <p>A handy way to list all open file descriptors is to use the "lsof" command (that's not a Bash built-in), whereas $$ is the process id (pid) of the current shell process:</p> <pre> ❯ lsof -a -p $$ -d0,1,2 @@ -2687,7 +2687,7 @@ COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME bash 62676 paul 0u CHR 136,9 0t0 12 /dev/pts/9 bash 62676 paul 1u CHR 136,9 0t0 12 /dev/pts/9 bash 62676 paul 2u CHR 136,9 0t0 12 /dev/pts/9 -</pre><br /> +</pre> <p>Let's create our own descriptor "3" for redirection to a file named "foo":</p> <pre> ❯ touch foo @@ -2702,7 +2702,7 @@ Bratwurst ❯ exec 3>&- # This closes fd 3. ❯ echo Steak >&3 -bash: 3: Bad file descriptor -</pre><br /> +</pre> <p>You can also override the default file descriptors, as the following example script demonstrates:</p> <pre> ❯ cat grandmaster.sh @@ -2729,14 +2729,14 @@ echo Second line: $LINE2 # Restore default stdin and delete fd 6 exec 0<&6 6<&- -</pre><br /> +</pre> <p>Let's execute it:</p> <pre> ❯ chmod 750 ./grandmaster.sh ❯ ./grandmaster.sh First line: Learn You a Haskell Second line: for Great Good -</pre><br /> +</pre> <h2>HERE</h2> <p>I have mentioned HERE-documents and HERE-strings already in this post. Let's do some more examples. The following "cat" receives a multi line string from stdin. In this case, the input multi line string is a HERE-document. As you can see, it also interpolates variables (in this case the output of "date" running in a subshell).</p> <pre> @@ -2746,7 +2746,7 @@ Second line: for Great Good > END Hello World It's Fri 26 Nov 08:46:52 GMT 2021 -</pre><br /> +</pre> <p>You can also write it the following way, but that's less readable (it's good for an obfuscation contest):</p> <pre> ❯ <<END cat @@ -2755,7 +2755,7 @@ It's Fri 26 Nov 08:46:52 GMT 2021 > END Hello Universe It's Fri 26 Nov 08:47:32 GMT 2021 -</pre><br /> +</pre> <p>Besides of an HERE-document, there is also a so-called HERE-string. Besides of...</p> <pre> ❯ declare VAR=foo @@ -2763,24 +2763,24 @@ It's Fri 26 Nov 08:47:32 GMT 2021 > echo '$VAR ontains foo' > fi $VAR ontains foo -</pre><br /> +</pre> <p>...you can use a HERE-string like that:</p> <pre> ❯ if grep -q foo <<< "$VAR"; then > echo '$VAR contains foo' > fi $VAR contains foo -</pre><br /> +</pre> <p>Or even shorter, you can do:</p> <pre> ❯ grep -q foo <<< "$VAR" && echo '$VAR contains foo' $VAR contains foo -</pre><br /> +</pre> <p>You can also use a Bash regex to accomplish the same thing, but the points of the examples so far were to demonstrate HERE-{documents,strings} and not Bash regular expressions:</p> <pre> ❯ if [[ "$VAR" =~ foo ]]; then echo yay; fi yay -</pre><br /> +</pre> <p>You can also use it with "read":</p> <pre> ❯ read a <<< ja @@ -2795,14 +2795,14 @@ NEIN!!! Learn ❯ echo ${words[3]} Golang -</pre><br /> +</pre> <p>The following is good for an obfuscation contest too:</p> <pre> ❯ echo 'I like Perl too' > perllove.txt ❯ cat - perllove.txt <<< "$dumdidumstring" Learn you a Golang for Great Good I like Perl too -</pre><br /> +</pre> <h2>RANDOM</h2> <p>Random is a special built-in variable containing a different pseudo random number each time it's used.</p> <pre> @@ -2812,7 +2812,7 @@ I like Perl too 14997 ❯ echo $RANDOM 9104 -</pre><br /> +</pre> <p>That's very useful if you want to randomly delay the execution of your scripts when you run it on many servers concurrently, just to spread the server load (which might be caused by the script run) better.</p> <p>Let's say you want to introduce a random delay of 1 minute. You can accomplish it with:</p> <pre> @@ -2841,7 +2841,7 @@ main ❯ ./calc_answer_to_ultimate_question_in_life.sh Delaying script execution for 42 seconds... Continuing script execution... -</pre><br /> +</pre> <h2>set -x and set -e and pipefile</h2> <p>In my opinion, -x and -e and pipefile are the most useful Bash options. Let's have a look at them one after another.</p> <h3>-x</h3> @@ -2856,11 +2856,11 @@ Continuing script execution... ++ echo 121 + echo 'Square of 11 is 121' Square of 11 is 121 -</pre><br /> +</pre> <p>You can also set it when calling an external script without modifying the script itself:</p> <pre> ❯ bash -x ./half_broken_script_to_be_debugged.sh -</pre><br /> +</pre> <p>Let's do that on one of the example scripts we covered earlier:</p> <pre> ❯ bash -x ./grandmaster.sh @@ -2878,21 +2878,21 @@ First line: Learn You a Haskell Second line: for Great Good + exec ❯ -</pre><br /> +</pre> <h3>-e</h3> <p>This is a very important option you want to use when you are paranoid. This means, you should always "set -e" in your scripts when you need to make absolutely sure that your script runs successfully (with that I mean that no command should exit with an unexpected status code).</p> <p>Ok, let's dig deeper:</p> <pre> ❯ help set | grep -- -e -e Exit immediately if a command exits with a non-zero status. -</pre><br /> +</pre> <p>As you can see in the following example, the Bash terminates after the execution of "grep" as "foo" is not matching "bar". Therefore, grep exits with 1 (unsuccessfully) and the shell aborts. And therefore, "bar" will not be printed out anymore:</p> <pre> ❯ bash -c 'set -e; echo hello; grep -q bar <<< foo; echo bar' hello ❯ echo $? 1 -</pre><br /> +</pre> <p>Whereas the outcome changes when the regex matches:</p> <pre> ❯ bash -c 'set -e; echo hello; grep -q bar <<< barman; echo bar' @@ -2900,7 +2900,7 @@ hello bar ❯ echo $? 0 -</pre><br /> +</pre> <p>So does it mean that grep will always make the shell terminate whenever its exit code isn't 0? This will render "set -e" quite unusable. Frankly, there are other commands where an exit status other than 0 should not terminate the whole script abruptly. Usually, what you want is to branch your code based on the outcome (exit code) of a command:</p> <pre> ❯ bash -c 'set -e @@ -2912,7 +2912,7 @@ bar > fi' ❯ echo $? 1 -</pre><br /> +</pre> <p>...but the example above won't reach any of the branches and won't print out anything, as the script terminates right after grep.</p> <p>The proper solution is to use grep as an expression in a conditional (e.g. in an if-else statement):</p> <pre> @@ -2934,7 +2934,7 @@ not matching matching ❯ echo $? 0 -</pre><br /> +</pre> <p>You can also temporally undo "set -e" if there is no other way:</p> <pre> ❯ cat ./e.sh @@ -2976,7 +2976,7 @@ foo Hello World Hello Universe Hello You! -</pre><br /> +</pre> <p>Why does calling "foo" with no arguments make the script terminate? Because as no argument was given, the "shift" won't have anything to do as the argument list $@ is empty, and therefore "shift" fails with a non-zero status.</p> <p>Why would you want to use "shift" after function-local variable assignments? Have a look at my personal Bash coding style guide for an explanation :-):</p> <a class="textlink" href="https://foo.zone/gemfeed/2021-05-16-personal-bash-coding-style-guide.html">./2021-05-16-personal-bash-coding-style-guide.html</a><br /> @@ -2987,14 +2987,14 @@ Hello You! pipefail the return value of a pipeline is the status of the last command to exit with a non-zero status, or zero if no command exited with a non-zero status -</pre><br /> +</pre> <p>The following greps for paul in passwd and converts all lowercase letters to uppercase letters. The exit code of the pipe is 0, as the last command of the pipe (converting from lowercase to uppercase) succeeded:</p> <pre> ❯ grep paul /etc/passwd | tr '[a-z]' '[A-Z]' PAUL:X:1000:1000:PAUL BUETOW:/HOME/PAUL:/BIN/BASH ❯ echo $? 0 -</pre><br /> +</pre> <p>Let's look at another example, where "TheRock" doesn't exist in the passwd file. However, the pipes exit status is still 0 (success). This is so because the last command ("tr" in this case) still succeeded. It is just that it didn't get any input on stdin to process:</p> <pre> ❯ grep TheRock /etc/passwd @@ -3003,14 +3003,14 @@ PAUL:X:1000:1000:PAUL BUETOW:/HOME/PAUL:/BIN/BASH ❯ grep TheRock /etc/passwd | tr '[a-z]' '[A-Z]' ❯ echo $? 0 -</pre><br /> +</pre> <p>To change this behaviour, pipefile can be used. Now, the pipes exit status is 1 (fail), because the pipe contains at least one command (in this case grep) which exited with status 1:</p> <pre> ❯ set -o pipefail ❯ grep TheRock /etc/passwd | tr '[a-z]' '[A-Z]' ❯ echo $? 1 -</pre><br /> +</pre> <p>Other related posts are:</p> <a class="textlink" href="https://foo.zone/gemfeed/2022-01-01-bash-golf-part-2.html">2022-01-01 Bash Golf Part 2 (You are currently reading this)</a><br /> <a class="textlink" href="https://foo.zone/gemfeed/2021-11-29-bash-golf-part-1.html">2021-11-29 Bash Golf Part 1</a><br /> @@ -3138,7 +3138,7 @@ PAUL:X:1000:1000:PAUL BUETOW:/HOME/PAUL:/BIN/BASH / / .' | jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Art by Joan Stark -</pre><br /> +</pre> <p>This is the first blog post about my Bash Golf series. This series is about random Bash tips, tricks and weirdnesses I came across. It's a collection of smaller articles I wrote in an older (in German language) blog, which I translated and refreshed with some new content.</p> <a class="textlink" href="https://foo.zone/gemfeed/2022-01-01-bash-golf-part-2.html">2022-01-01 Bash Golf Part 2</a><br /> <a class="textlink" href="https://foo.zone/gemfeed/2021-11-29-bash-golf-part-1.html">2021-11-29 Bash Golf Part 1 (You are currently reading this)</a><br /> @@ -3149,7 +3149,7 @@ jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ❯ cat < /dev/tcp/time.nist.gov/13 59536 21-11-18 08:09:16 00 0 0 153.6 UTC(NIST) * -</pre><br /> +</pre> <p>The Bash treats /dev/tcp/HOST/PORT in a special way so that it is actually establishing a TCP connection to HOST:PORT. The example above redirects the TCP output of the time-server to cat and cat is printing it on standard output (stdout).</p> <p>A more sophisticated example is firing up an HTTP request. Let's create a new read-write (rw) file descriptor (fd) 5, redirect the HTTP request string to it, and then read the response back:</p> <pre> @@ -3166,7 +3166,7 @@ Server: gws Content-Length: 218 X-XSS-Protection: 0 X-Frame-Options: SAMEORIGIN -</pre><br /> +</pre> <p>You would assume that this also works with the ZSH, but it doesn't. This is one of the few things which don't work with the ZSH but in the Bash. There might be plugins you could use for ZSH to do something similar, though.</p> <h2>Process substitution</h2> <p>The idea here is, that you can read the output (stdout) of a command from a file descriptor:</p> @@ -3187,7 +3187,7 @@ Access: 2021-11-20 10:59:31.482411961 +0000 Modify: 2021-11-20 10:59:31.482411961 +0000 Change: 2021-11-20 10:59:31.482411961 +0000 Birth: - -</pre><br /> +</pre> <p>This example doesn't make any sense practically speaking, but it clearly demonstrates how process substitution works. The standard output pipe of "uptime" is redirected to an anonymous file descriptor. That fd then is opened by the "cat" command as a regular file.</p> <p>A useful use case is displaying the differences of two sorted files:</p> <pre> @@ -3209,11 +3209,11 @@ Change: 2021-11-20 10:59:31.482411961 +0000 ❯ echo X >> /tmp/file-a.txt # Now, both files have the same content again. ❯ diff -u <(sort /tmp/file-a.txt) <(sort /tmp/file-b.txt) ❯ -</pre><br /> +</pre> <p>Another example is displaying the differences of two directories:</p> <pre> ❯ diff -u <(ls ./dir1/ | sort) <(ls ./dir2/ | sort) -</pre><br /> +</pre> <p>More (Bash golfing) examples:</p> <pre> ❯ wc -l <(ls /tmp/) /etc/passwd <(env) @@ -3228,12 +3228,12 @@ Change: 2021-11-20 10:59:31.482411961 +0000 > done < <(echo foo bar baz) foo bar baz ❯ -</pre><br /> +</pre> <p>So far, we only used process substitution for stdout redirection. But it also works for stdin. The following two commands result into the same outcome, but the second one is writing the tar data stream to an anonymous file descriptor which is substituted by the "bzip2" command reading the data stream from stdin and compressing it to its own stdout, which then gets redirected to a file:</p> <pre> ❯ tar cjf file.tar.bz2 foo ❯ tar cjf >(bzip2 -c > file.tar.bz2) foo -</pre><br /> +</pre> <p>Just think a while and see whether you understand fully what is happening here.</p> <h2>Grouping</h2> <p>Command grouping can be quite useful for combining the output of multiple commands:</p> @@ -3242,7 +3242,7 @@ foo bar baz 97 ❯ ( ls /tmp; cat /etc/passwd; env; ) | wc -l 97 -</pre><br /> +</pre> <p>But wait, what is the difference between curly braces and normal braces? I assumed that the normal braces create a subprocess whereas the curly ones don't, but I was wrong:</p> <pre> ❯ echo $$ @@ -3251,7 +3251,7 @@ foo bar baz 62676 ❯ ( echo $$; ) 62676 -</pre><br /> +</pre> <p>One difference is, that the curly braces require you to end the last statement with a semicolon, whereas with the normal braces you can omit the last semicolon:</p> <pre> ❯ ( env; ls ) | wc -l @@ -3259,7 +3259,7 @@ foo bar baz ❯ { env; ls } | wc -l > > ^C -</pre><br /> +</pre> <p>In case you know more (subtle) differences, please write me an E-Mail and let me know.</p> <p class="quote"><i>Update: A reader sent me an E-Mail and pointed me to the Bash manual page, which explains the difference between () and {} (I should have checked that by myself):</i></p> <pre> @@ -3275,19 +3275,19 @@ foo bar baz ters ( and ), { and } are reserved words and must occur where a reserved word is permitted to be recognized. Since they do not cause a word break, they must be separated from list by whitespace or another shell metacharacter. |
