diff options
21 files changed, 2581 insertions, 34 deletions
diff --git a/gemfeed/2021-04-22-dtail-the-distributed-log-tail-program.gmi b/gemfeed/2021-04-22-dtail-the-distributed-log-tail-program.gmi index 7fad7f1b..250e7ffe 100644 --- a/gemfeed/2021-04-22-dtail-the-distributed-log-tail-program.gmi +++ b/gemfeed/2021-04-22-dtail-the-distributed-log-tail-program.gmi @@ -103,6 +103,12 @@ Mimecast highly encourages you to have a look at DTail and submit an issue for a => https://dtail.dev +More related posts are: + +=> ./2022-10-30-installing-dtail-on-openbsd.gmi 2022-10-30 Installing DTail on OpenBSD +=> ./2022-03-06-the-release-of-dtail-4.0.0.gmi 2022-03-06 The release of DTail 4.0.0 +=> ./2021-04-22-dtail-the-distributed-log-tail-program.gmi 2021-04-22 DTail - The distributed log tail program (You are currently reading this) + E-Mail your comments to hi@paul.cyou :-) => ../ Back to the main site diff --git a/gemfeed/2021-04-22-dtail-the-distributed-log-tail-program.gmi.tpl b/gemfeed/2021-04-22-dtail-the-distributed-log-tail-program.gmi.tpl new file mode 100644 index 00000000..861ac636 --- /dev/null +++ b/gemfeed/2021-04-22-dtail-the-distributed-log-tail-program.gmi.tpl @@ -0,0 +1,112 @@ +# DTail - The distributed log tail program + +> Published at 2021-04-22T19:28:41+01:00; Updated at 2021-04-26 + +=> ./2021-04-22-dtail-the-distributed-log-tail-program/title.png DTail logo image + +This article first appeared at the Mimecast Engineering Blog but I made it available here in my personal internet site too. + +=> https://medium.com/mimecast-engineering/dtail-the-distributed-log-tail-program-79b8087904bb Original Mimecast Engineering Blog post at Medium + +Running a large cloud-based service requires monitoring the state of huge numbers of machines, a task for which many standard UNIX tools were not really designed. In this post, I will describe a simple program, DTail, that Mimecast has built and released as Open-Source, which enables us to monitor log files of many servers at once without the costly overhead of a full-blown log management system. + +At Mimecast, we run over 10 thousand server boxes. Most of them host multiple microservices and each of them produces log files. Even with the use of time series databases and monitoring systems, raw application logs are still an important source of information when it comes to analysing, debugging, and troubleshooting services. + +Every engineer familiar with UNIX or a UNIX-like platform (e.g., Linux) is well aware of tail, a command-line program for displaying a text file content on the terminal which is also especially useful for following application or system log files with tail -f logfile. + +Think of DTail as a distributed version of the tail program which is very useful when you have a distributed application running on many servers. DTail is an Open-Source, cross-platform, fairly easy to use, support and maintain log file analysis & statistics gathering tool designed for Engineers and Systems Administrators. It is programmed in Google Go. + +## A Mimecast Pet Project + +DTail got its inspiration from public domain tools available already in this area but it is a blue sky from-scratch development which was first presented at Mimecast’s annual internal Pet Project competition (awarded with a Bronze prize). It has gained popularity since and is one of the most widely deployed DevOps tools at Mimecast (reaching nearly 10k server installations) and many engineers use it on a regular basis. The Open-Source version of DTail is available at: + +=> https://dtail.dev + +Try it out — We would love any feedback. But first, read on… + +## Differentiating from log management systems + +Why not just use a full-blown log management system? There are various Open-Source and commercial log management solutions available on the market you could choose from (e.g. the ELK stack). Most of them store the logs in a centralized location and are fairly complex to set up and operate. Possibly they are also pretty expensive to operate if you have to buy dedicated hardware (or pay fees to your cloud provider) and have to hire support staff for it. + +DTail does not aim to replace any of the log management tools already available but is rather an additional tool crafted especially for ad-hoc debugging and troubleshooting purposes. DTail is cheap to operate as it does not require any dedicated hardware for log storage as it operates directly on the source of the logs. It means that there is a DTail server installed on all server boxes producing logs. This decentralized comes with the direct advantages that there is no introduced delay because the logs are not shipped to a central log storage device. The reduced complexity also makes it more robust against outages. You won’t be able to troubleshoot your distributed application very well if the log management infrastructure isn’t working either. + +=> ./2021-04-22-dtail-the-distributed-log-tail-program/dtail.gif DTail sample session animated gif + +As a downside, you won’t be able to access any logs with DTail when the server is down. Furthermore, a server can store logs only up to a certain capacity as disks will fill up. For the purpose of ad-hoc debugging, these are not typically issues. Usually, it’s the application you want to debug and not the server. And disk space is rarely an issue for bare metal and VM-based systems these days, with sufficient space for several weeks’ worth of log storage being available. DTail also supports reading compressed logs. The currently supported compression algorithms are gzip and zstd. + +## Combining simplicity, security and efficiency + +DTail also has a client component that connects to multiple servers concurrently for log files (or any other text files). + +The DTail client interacts with a DTail server on port TCP/2222 via SSH protocol and does not interact in any way with the system’s SSH server (e.g., OpenSSH Server) which might be running at port TCP/22 already. As a matter of fact, you don’t need a regular SSH server running for DTail at all. There is no support for interactive login shells at TCP/2222 either, as by design that port can only be used for text data streaming. The SSH protocol is used for the public/private key infrastructure and transport encryption only and DTail implements its own protocol on top of SSH for the features provided. There is no need to set up or buy any additional TLS certificates. The port 2222 can be easily reconfigured if you preferred to use a different one. + +The DTail server, which is a single static binary, will not fork an external process. This means that all features are implemented in native Go code (exception: Linux ACL support is implemented in C, but it must be enabled explicitly on compile time) and therefore helping to make it robust, secure, efficient, and easy to deploy. A single client, running on a standard Laptop, can connect to thousands of servers concurrently while still maintaining a small resource footprint. + +Recent log files are very likely still in the file system caches on the servers. Therefore, there tends to be a minimal I/O overhead involved. + +## The DTail family of commands + +Following the UNIX philosophy, DTail includes multiple command-line commands each of them for a different purpose: + +* dserver: The DTail server, the only binary required to be installed on the servers involved. +* dtail: The distributed log tail client for following log files. +* dcat: The distributed cat client for concatenating and displaying text files. +* dgrep: The distributed grep client for searching text files for a regular expression pattern. +* dmap: The distributed map-reduce client for aggregating stats from log files. + +=> ./2021-04-22-dtail-the-distributed-log-tail-program/dgrep.gif DGrep sample session animated gif + +## Usage example + +The use of these commands is almost self-explanatory for a person already used to the standard command line in Unix systems. One of the main goals is to make DTail easy to use. A tool that is too complicated to use under high-pressure scenarios (e.g., during an incident) can be quite detrimental. + +The basic idea is to start one of the clients from the command line and provide a list of servers to connect to with –servers. You also must provide a path of remote (log) files via –files. If you want to process multiple files per server, you could either provide a comma-separated list of file paths or make use of file system globbing (or a combination of both). + +The following example would connect to all DTail servers listed in the serverlist.txt, follow all files with the ending .log and filter for lines containing the string error. You can specify any Go compatible regular expression. In this example we add the case-insensitive flag to the regex: + +``` +dtail –servers serverlist.txt –files ‘/var/log/*.log’ –regex ‘(?i:error)’ +``` + +You usually want to specify a regular expression as a client argument. This will mean that responses are pre-filtered for all matching lines on the server-side and thus sending back only the relevant lines to the client. If your logs are growing very rapidly and the regex is not specific enough there might be the chance that your client is not fast enough to keep up processing all of the responses. This could be due to a network bottleneck or just as simple as a slow terminal emulator displaying the log lines on the client-side. + +A green 100 in the client output before each log line received from the server always indicates that there were no such problems and 100% of all log lines could be displayed on your terminal (have a look at the animated Gifs in this post). If the percentage falls below 100 it means that some of the channels used by the servers to send data to the client are congested and lines were dropped. In this case, the color will change from green to red. The user then could decide to run the same query but with a more specific regex. + +You could also provide a comma-separated list of servers as opposed to a text file. There are many more options you could use. The ones listed here are just the very basic ones. There are more instructions and usage examples on the GitHub page. Also, you can study even more of the available options via the –help switch (some real treasures might be hidden there). + +## Fitting it in + +DTail integrates nicely into the user management of existing infrastructure. It follows normal system permissions and does not open new “holes” on the server which helps to keep security departments happy. The user would not have more or less file read permissions than he would have via a regular SSH login shell. There is a full SSH key, traditional UNIX permissions, and Linux ACL support. There is also a very low resource footprint involved. On average for tailing and searching log files less than 100MB RAM and less than a quarter of a CPU core per participating server are required. Complex map-reduce queries on big data sets will require more resources accordingly. + +## Advanced features + +The features listed here are out of the scope of this blog post but are worthwhile to mention: + +* Distributed map-reduce queries on stats provided in log files with dmap. dmap comes with its own SQL-like aggregation query language. +* Stats streaming with continuous map-reduce queries. The difference to normal queries is that the stats are aggregated over a specified interval only on the newly written log lines. Thus, giving a de-facto live stat view for each interval. +* Server-side scheduled queries on log files. The queries are configured in the DTail server configuration file and scheduled at certain time intervals. Results are written to CSV files. This is useful for generating daily stats from the log files without the need for an interactive client. +* Server-side stats streaming with continuous map-reduce queries. This for example can be used to periodically generate stats from the logs at a configured interval, e.g., log error counts by the minute. These then can be sent to a time-series database (e.g., Graphite) and then plotted in a Grafana dashboard. +* Support for custom extensions. E.g., for different server discovery methods (so you don’t have to rely on plain server lists) and log file formats (so that map-reduce queries can parse more stats from the logs). + +## For the future + +There are various features we want to see in the future. + +* A spartan mode, not printing out any extra information but the raw remote log files would be a nice feature to have. This will make it easier to post-process the data produced by the DTail client with common UNIX tools. (To some degree this is possible already, just disable the ANSI terminal color output of the client with -noColors and pipe the output to another program). +* Tempting would be implementing the dgoawk command, a distributed version of the AWK programming language purely implemented in Go, for advanced text data stream processing capabilities. There are 3rd party libraries available implementing AWK in pure Go which could be used. +* A more complex change would be the support of federated queries. You can connect to thousands of servers from a single client running on a laptop. But does it scale to 100k of servers? Some of the servers could be used as middleware for connecting to even more servers. +* Another aspect is to extend the documentation. Especially the advanced features such as map-reduce query language and how to configure the server-side queries currently do require more documentation. For now, you can read the code, sample config files or just ask the author for that! But this will be certainly addressed in the future. + +## Open Source + +Mimecast highly encourages you to have a look at DTail and submit an issue for any features you would like to see. Have you found a bug? Maybe you just have a question or comment? If you want to go a step further: We would also love to see pull requests for any features or improvements. Either way, if in doubt just contact us via the DTail GitHub page. + +=> https://dtail.dev + +More related posts are: + +<< template::inline::index dtail + +E-Mail your comments to hi@paul.cyou :-) + +=> ../ Back to the main site diff --git a/gemfeed/2021-04-24-welcome-to-the-geminispace.gmi b/gemfeed/2021-04-24-welcome-to-the-geminispace.gmi index 8b1864a6..8cf3ea2e 100644 --- a/gemfeed/2021-04-24-welcome-to-the-geminispace.gmi +++ b/gemfeed/2021-04-24-welcome-to-the-geminispace.gmi @@ -78,6 +78,12 @@ Check out one of the following links for more information about Gemini. For exam => gemini://gemini.circumlunar.space => https://gemini.circumlunar.space +More related posts are: + +=> ./2022-08-27-gemtexter-1.1.0-lets-gemtext-again.gmi 2022-08-27 Gemtexter 1.1.0 - Let's Gemtext again +=> ./2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi 2021-06-05 Gemtexter - One Bash script to rule it all +=> ./2021-04-24-welcome-to-the-geminispace.gmi 2021-04-24 Welcome to the Geminispace (You are currently reading this) + E-Mail your comments to hi@paul.cyou :-) => ../ Back to the main site diff --git a/gemfeed/2021-04-24-welcome-to-the-geminispace.gmi.tpl b/gemfeed/2021-04-24-welcome-to-the-geminispace.gmi.tpl new file mode 100644 index 00000000..385ccf4e --- /dev/null +++ b/gemfeed/2021-04-24-welcome-to-the-geminispace.gmi.tpl @@ -0,0 +1,87 @@ +# Welcome to the Geminispace + +> Published at 2021-04-24T19:28:41+01:00; Updated at 2021-06-18 + +ASCII Art by Andy Hood! + +Have you reached this article already via Gemini? It requires a Gemini client; web browsers such as Firefox, Chrome, Safari, etc., don't support the Gemini protocol. The Gemini address of this site (or the address of this capsule as people say in Geminispace) is: + +=> gemini://foo.zone + +However, if you still use HTTP, you are just surfing the fallback HTML version of this capsule. In that case, I suggest reading on what this is all about :-). + +``` + + /\ + / \ + | | + |NASA| + | | + | | + | | + ' ` + |Gemini| + | | + |______| + '-`'-` . + / . \'\ . .' + ''( .'\.' ' .;' +'.;.;' ;'.;' ..;;' AsH + +``` + +## Motivation + +### My urge to revamp my personal website + +For some time, I had to urge to revamp my personal website. Not to update the technology and its design but to update all the content (+ keep it current) and start a small tech blog again. So unconsciously, I began to search for an excellent platform to do all of that in a KISS (keep it simple & stupid) way. + +### My still great Laptop running hot + +Earlier this year (2021), I noticed that my almost seven-year-old but still great Laptop started to become hot and slowed down while surfing the web. Also, the Laptop's fan became quite noisy. This was all due to the additional bloat such as JavaScript, excessive use of CSS, tracking cookies+pixels, ads, and so on there was on the website. + +All I wanted was to read an interesting article, but after a big advertising pop-up banner appeared and made everything worse, I gave up and closed the browser tab. + +## Discovering the Gemini internet protocol + +Around the same time, I discovered a relatively new, more lightweight protocol named Gemini, which does not support all these CPU-intensive features like HTML, JavaScript, and CSS. Also, tracking and ads are unsupported by the Gemini protocol. + +The "downside" is that due to the limited capabilities of the Gemini protocol, all sites look very old and spartan. But that is not a downside; that is, in fact, a design choice people made. It is up to the client software how your capsule looks. For example, you could use a graphical client, such as Lagrange, with nice font renderings and colours to improve the appearance. Or you could use a very minimalistic command line black-and-white Gemini client. It's your (the user's) choice. + +=> ./2021-04-24-welcome-to-the-geminispace/amfora-screenshot.png Screenshot Amfora Gemini terminal client surfing this site +=> ./2021-04-24-welcome-to-the-geminispace/lagrange-screenshot.png Screenshot graphical Lagrange Gemini client surfing this site + +Why is there a need for a new protocol? As the modern web is a superset of Gemini, can't we use simple HTML 1.0 instead? That's a good and valid question. It is not a technical problem but a human problem. We tend to abuse the features once they are available. You can ensure that things stay efficient and straightforward as long as you are using the Gemini protocol. On the other hand, you can't force every website on the modern web to only create plain and straightforward-looking HTML pages. + +## My own Gemini capsule + +As it is effortless to set up and maintain your own Gemini capsule (Gemini server + content composed via the Gemtext markup language), I decided to create my own. What I like about Gemini is that I can use my favourite text editor and get typing. I don't need to worry about the style and design of the presence, and I also don't have to test anything in ten different web browsers. I can only focus on the content! As a matter of fact, I am using the Vim editor + its spellchecker + auto word completion functionality to write this. + +This site was generated with Gemtexter. You can read more about it here: + +=> ./2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi Gemtexter - One Bash script to rule it all + +## Gemini advantages summarised + +* Supports an alternative to the modern bloated web +* Easy to operate and easy to write content +* No need to worry about various web browser compatibilities +* It's the client's responsibility how the content is designed+presented +* Lightweight (although not as lightweight as the Gopher protocol) +* Supports privacy (no cookies, no request header fingerprinting, TLS encryption) +* Fun to play with (it's a bit geeky, yes, but a lot of fun!) + +## Dive into deep Gemini space + +Check out one of the following links for more information about Gemini. For example, you will find a FAQ that explains why the protocol is named Gemini. Many Gemini capsules are dual-hosted via Gemini and HTTP(S) so that people new to Gemini can sneak peek at the content with a regular web browser. Some people go as far as tri-hosting all their content via HTTP(S), Gemini and Gopher. + +=> gemini://gemini.circumlunar.space +=> https://gemini.circumlunar.space + +More related posts are: + +<< template::inline::index gemtext gemini + +E-Mail your comments to hi@paul.cyou :-) + +=> ../ Back to the main site diff --git a/gemfeed/2021-05-16-personal-bash-coding-style-guide.gmi b/gemfeed/2021-05-16-personal-bash-coding-style-guide.gmi index 2db178a3..15fdcdeb 100644 --- a/gemfeed/2021-05-16-personal-bash-coding-style-guide.gmi +++ b/gemfeed/2021-05-16-personal-bash-coding-style-guide.gmi @@ -380,6 +380,13 @@ I also highly recommend having a read through the "Advanced Bash-Scripting Guide => https://tldp.org/LDP/abs/html/ Advanced Bash-Scripting Guide +More related posts are: + +=> ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 +=> ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 +=> ./2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi 2021-06-05 Gemtexter - One Bash script to rule it all +=> ./2021-05-16-personal-bash-coding-style-guide.gmi 2021-05-16 Personal Bash coding style guide (You are currently reading this) + E-Mail your comments to hi@paul.cyou :-) => ../ Back to the main site diff --git a/gemfeed/2021-05-16-personal-bash-coding-style-guide.gmi.tpl b/gemfeed/2021-05-16-personal-bash-coding-style-guide.gmi.tpl new file mode 100644 index 00000000..060313eb --- /dev/null +++ b/gemfeed/2021-05-16-personal-bash-coding-style-guide.gmi.tpl @@ -0,0 +1,389 @@ +# Personal Bash coding style guide + +> Published at 2021-05-16T14:51:57+01:00 + +``` + .---------------------------. + /,--..---..---..---..---..--. `. + //___||___||___||___||___||___\_| + [j__ ######################## [_| + \============================| + .==| |"""||"""||"""||"""| |"""|| +/======"---""---""---""---"=| =|| +|____ []* ____ | ==|| +// \\ // \\ |===|| hjw +"\__/"---------------"\__/"-+---+' +``` + +Lately, I have been polishing and writing a lot of Bash code. Not that I never wrote a lot of Bash, but now as I also looked through the Google Shell Style Guide, I thought it is time also to write my thoughts on that. I agree with that guide in most, but not in all points. + +=> https://google.github.io/styleguide/shellguide.html Google Shell Style Guide + +## My modifications + +These are my modifications to the Google Guide. + +### Shebang + +Google recommends using always... + +``` +#!/bin/bash +``` + +... as the shebang line, but that does not work on all Unix and Unix-like operating systems (e.g., the *BSDs don't have Bash installed to /bin/bash). Better is: + +``` +#!/usr/bin/env bash +``` + +### Two space soft-tabs indentation + +I know there have been many tab- and soft-tab wars on this planet. Google recommends using two space soft-tabs for Bash scripts. + +I don't care if I use two or four space indentations. I agree, however, that we should not use tabs. I tend to use four-space soft-tabs as that's how I currently configured Vim for any programming language. What matters most, though, is consistency within the same script/project. + +Google also recommends limiting the line length to 80 characters. For some people, that seems to be an old habit from the '80s, where all computer terminals couldn't display longer lines. But I think that the 80 character mark is still a good practice, at least for shell scripts. For example, I am often writing code on a Microsoft Go Tablet PC (running Linux, of course), and it comes in convenient if the lines are not too long due to the relatively small display on the device. + +I hit the 80 character line length quicker with the four spaces than with two spaces, but that makes me refactor the Bash code more aggressively, which is a good thing. + +### Breaking long pipes + +Google recommends breaking up long pipes like this: + +``` +# All fits on one line +command1 | command2 + +# Long commands +command1 \ + | command2 \ + | command3 \ + | command4 +``` + +I think there is a better way like the following, which is less noisy. The pipe | already indicates the Bash that another command is expected, thus making the explicit line breaks with \ obsolete: + +``` +# Long commands +command1 | + command2 | + command3 | + command4 +``` + +### Quoting your variables + +Google recommends always quote your variables. Generally, it would be best if you did that only for variables where you are unsure about the content/values of the variables (e.g., content is from an external input source and may contain whitespace or other special characters). In my opinion, the code will become quite noisy when you always quote your variables like this: + +``` +greet () { + local -r greeting="${1}" + local -r name="${2}" + echo "${greeting} ${name}!" +} +``` + +In this particular example, I agree that you should quote them as you don't know the input (are there, for example, whitespace characters?). But if you are sure that you are only using simple bare words, then I think that the code looks much cleaner when you do this instead: + +``` +say_hello_to_paul () { + local -r greeting=Hello + local -r name=Paul + echo "$greeting $name!" +} +``` + +You see, I also omitted the curly braces { } around the variables. I only use the curly braces around variables when it makes the code either easier/clearer to read or if it is necessary to use them: + +``` +declare FOO=bar +# Curly braces around FOO are necessary +echo "foo${FOO}baz" +``` + +A few more words on always quoting the variables: For the sake of consistency (and for making ShellCheck happy), I am not against quoting everything I encounter. I also think that the larger the Bash script becomes, the more critical it becomes always to quote variables. That's because it will be more likely that you might not remember that some of the functions don't work on values with spaces in them, for example. It's just that I won't quote everything in every small script I write. + +### Prefer built-in commands over external commands + +Google recommends using the built-in commands over available external commands where possible: + +``` +# Prefer this: +addition=$(( X + Y )) +substitution="${string/#foo/bar}" + +# Instead of this: +addition="$(expr "${X}" + "${Y}")" +substitution="$(echo "${string}" | sed -e 's/^foo/bar/')" +``` + +I can't entirely agree here. The external commands (especially sed) are much more sophisticated and powerful than the built-in Bash versions. Sed can do much more than the Bash can ever do by itself when it comes to text manipulation (the name "sed" stands for streaming editor, after all). + +I prefer to do light text processing with the Bash built-ins and more complicated text processing with external programs such as sed, grep, awk, cut, and tr. However, there is also medium-light text processing where I would want to use external programs. That is so because I remember using them better than the Bash built-ins. The Bash can get relatively obscure here (even Perl will be more readable then - Side note: I love Perl). + +Also, you would like to use an external command for floating-point calculation (e.g., bc) instead of using the Bash built-ins (worth noticing that ZSH supports built-in floating-points). + +I even didn't get started with what you can do with awk (especially GNU Awk), a fully-fledged programming language. Tiny Awk snippets tend to be used quite often in Shell scripts without honouring the real power of Awk. But if you did everything in Perl or Awk or another scripting language, then it wouldn't be a Bash script anymore, wouldn't it? ;-) + +## My additions + +### Use of 'yes' and 'no' + +Bash does not support a boolean type. I tend just to use the strings 'yes' and 'no' here. I used 0 for false and 1 for true for some time, but I think that the yes/no strings are easier to read. Yes, the Bash script would need to perform string comparisons on every check, but if performance is crucial to you, you wouldn't want to use a Bash script anyway, correct? + +``` +declare -r SUGAR_FREE=yes +declare -r I_NEED_THE_BUZZ=no + +buy_soda () { + local -r sugar_free=$1 + + if [[ $sugar_free == yes ]]; then + echo 'Diet Dr. Pepper' + else + echo 'Pepsi Coke' + fi +} + +buy_soda $I_NEED_THE_BUZZ +``` + +### Non-evil alternative to variable assignments via eval + +Google is in the opinion that eval should be avoided. I think so too. They list these examples in their guide: + +``` +# What does this set? +# Did it succeed? In part or whole? +eval $(set_my_variables) + +# What happens if one of the returned values has a space in it? +variable="$(eval some_function)" + +``` + +However, if I want to read variables from another file, I don't have to use eval here. I only have to source the file: + +``` +% cat vars.source.sh +declare foo=bar +declare bar=baz +declare bay=foo + +% bash -c 'source vars.source.sh; echo $foo $bar $baz' +bar baz foo +``` + +And suppose I want to assign variables dynamically. In that case, I could just run an external script and source its output (This is how you could do metaprogramming in Bash without the use of eval - write code which produces code for immediate execution): + +``` +% cat vars.sh +#!/usr/bin/env bash +cat <<END +declare date="$(date)" +declare user=$USER +END + +% bash -c 'source <(./vars.sh); echo "Hello $user, it is $date"' +Hello paul, it is Sat 15 May 19:21:12 BST 2021 +``` + +The downside is that ShellCheck won't be able to follow the dynamic sourcing anymore. + +### Prefer pipes over arrays for list processing + +When I do list processing in Bash, I prefer to use pipes. You can chain them through Bash functions as well, which is pretty neat. Usually, my list processing scripts are of a structure like this: + +``` +filter_lines () { + echo 'Start filtering lines in a fancy way!' >&2 + grep ... | sed .... +} + +process_lines () { + echo 'Start processing line by line!' >&2 + while read -r line; do + ... do something and produce a result... + echo "$result" + done +} + +# Do some post-processing of the data +postprocess_lines () { + echo 'Start removing duplicates!' >&2 + sort -u +} + +genreate_report () { + echo 'My boss wants to have a report!' >&2 + tee outfile.txt + wc -l outfile.txt +} + +main () { + filter_lines | + process_lines | + postprocess_lines | + generate_report +} + +main +``` + +The stdout is always passed as a pipe to the next following stage. The stderr is used for info logging. + +### Assign-then-shift + +I often refactor existing Bash code. That leads me to add and removing function arguments quite often. It's pretty repetitive work changing the $1, $2.... function argument numbers every time you change the order or add/remove possible arguments. + +The solution is to use of the "assign-then-shift"-method, which goes like this: "local -r var1=$1; shift; local -r var2=$1; shift". The idea is that you only use "$1" to assign function arguments to named (better readable) local function variables. You will never have to bother about "$2" or above. That is very useful when you constantly refactor your code and remove or add function arguments. It's something that I picked up from a colleague (a pure Bash wizard) some time ago: + +``` +some_function () { + local -r param_foo="$1"; shift + local -r param_baz="$1"; shift + local -r param_bay="$1"; shift + ... +} +``` + +Want to add a param_baz? Just do this: + +``` +some_function () { + local -r param_foo="$1"; shift + local -r param_bar="$1"; shift + local -r param_baz="$1"; shift + local -r param_bay="$1"; shift + ... +} +``` + +Want to remove param_foo? Nothing easier than that: + +``` +some_function () { + local -r param_bar="$1"; shift + local -r param_baz="$1"; shift + local -r param_bay="$1"; shift + ... +} +``` + +As you can see, I didn't need to change any other assignments within the function. Of course, you would also need to change the function argument lists at every occasion where the function is invoked - you would do that within the same refactoring session. + +### Paranoid mode + +I call this the paranoid mode. The Bash will stop executing when a command exits with a status not equal to 0: + +``` +set -e +grep -q foo <<< bar +echo Jo +``` + +Here 'Jo' will never be printed out as the grep didn't find any match. It's unrealistic for most scripts to run in paranoid mode purely, so there must be a way to add exceptions. Critical Bash scripts of mine tend to look like this: + +``` +#!/usr/bin/env bash + +set -e + +some_function () { + .. some critical code + ... + + set +e + # Grep might fail, but that's OK now + grep .... + local -i ec=$? + set -e + + .. critical code continues ... + if [[ $ec -ne 0 ]]; then + ... + fi + ... +} +``` + +## Learned + +There are also a couple of things I've learned from Google's guide. + +### Unintended lexicographical comparison. + +The following looks like a valid Bash code: + +``` +if [[ "${my_var}" > 3 ]]; then + # True for 4, false for 22. + do_something +fi +``` + +... but it is probably an unintended lexicographical comparison. A correct way would be: + +``` +if (( my_var > 3 )); then + do_something +fi +``` + +or + +``` +if [[ "${my_var}" -gt 3 ]]; then + do_something +fi +``` + +### PIPESTATUS + +I have never used the PIPESTATUS variable before. I knew that it's there, but I never bothered to understand how it works until now thoroughly. + +The PIPESTATUS variable in Bash allows checking of the return code from all parts of a pipe. If it's only necessary to check the success or failure of the whole pipe, then the following is acceptable: + +``` +tar -cf - ./* | ( cd "${dir}" && tar -xf - ) +if (( PIPESTATUS[0] != 0 || PIPESTATUS[1] != 0 )); then + echo "Unable to tar files to ${dir}" >&2 +fi +``` + +However, as PIPESTATUS will be overwritten as soon as you do any other command, if you need to act differently on errors based on where it happened in the pipe, you'll need to assign PIPESTATUS to another variable immediately after running the command (don't forget that [ is a command and will wipe out PIPESTATUS). + +``` +tar -cf - ./* | ( cd "${DIR}" && tar -xf - ) +return_codes=( "${PIPESTATUS[@]}" ) +if (( return_codes[0] != 0 )); then + do_something +fi +if (( return_codes[1] != 0 )); then + do_something_else +fi +``` + +## Use common sense and BE CONSISTENT. + +The following two paragraphs are thoroughly quoted from the Google guidelines. But they hit the hammer on the head: + +> If you are editing code, take a few minutes to look at the code around you and determine its style. If they use spaces around their if clauses, you should, too. If their comments have little boxes of stars around them, make your comments have little boxes of stars around them too. + +> The point of having style guidelines is to have a common vocabulary of coding so people can concentrate on what you are saying rather than on how you are saying it. We present global style rules here, so people know the vocabulary. But local style is also important. If the code you add to a file looks drastically different from the existing code around it, the discontinuity throws readers out of their rhythm when they go to read it. Try to avoid this. + + +## Advanced Bash learning pro tip + +I also highly recommend having a read through the "Advanced Bash-Scripting Guide" (not from Google). I use it as the universal Bash reference and learn something new every time I look at it. + +=> https://tldp.org/LDP/abs/html/ Advanced Bash-Scripting Guide + +More related posts are: + +<< template::inline::index bash + +E-Mail your comments to hi@paul.cyou :-) + +=> ../ Back to the main site diff --git a/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi b/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi index f98f651d..05027f79 100644 --- a/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi +++ b/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi @@ -43,9 +43,7 @@ `+a:f:......jrei''' ``` -You might have read my previous blog post about entering the Geminispace, where I pointed out the benefits of having and maintaining an internet presence there. This whole site (the blog and all other pages) is composed in the Gemtext markup language. - -=> ./2021-04-24-welcome-to-the-geminispace.gmi Welcome to the Geminispace +You might have read my previous blog posts about entering the Geminispace, where I pointed out the benefits of having and maintaining an internet presence there. This whole site (the blog and all other pages) is composed in the Gemtext markup language. This comes with the benefit that I can write content in my favourite text editor (Vim). @@ -166,6 +164,15 @@ It was quite a lot of fun writing Gemtexter. It's a relatively small project, bu I finally revamped my personal internet site and started to blog again. I wanted the result to be exactly how it is now: A slightly retro-inspired internet site built for fun with unconventional tools. +More related posts are: + +=> ./2022-08-27-gemtexter-1.1.0-lets-gemtext-again.gmi 2022-08-27 Gemtexter 1.1.0 - Let's Gemtext again +=> ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 +=> ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 +=> ./2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi 2021-06-05 Gemtexter - One Bash script to rule it all (You are currently reading this) +=> ./2021-05-16-personal-bash-coding-style-guide.gmi 2021-05-16 Personal Bash coding style guide +=> ./2021-04-24-welcome-to-the-geminispace.gmi 2021-04-24 Welcome to the Geminispace + E-Mail your comments to hi@paul.cyou :-) => ../ Back to the main site diff --git a/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi.tpl b/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi.tpl new file mode 100644 index 00000000..19426d54 --- /dev/null +++ b/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi.tpl @@ -0,0 +1,173 @@ +# Gemtexter - One Bash script to rule it all + +> Published at 2021-06-05T19:03:32+01:00 + +``` + o .,<>., o + |\/\/\/\/| + '========' + (_ SSSSSSs + )a'`SSSSSs + /_ SSSSSS + .=## SSSSS + .#### SSSSs + ###::::SSSSS + .;:::""""SSS + .:;:' . . \\ + .::/ ' .'| + .::( . | + :::) \ + /\( / + /) ( | + .' \ . ./ / + _-' |\ . | + _..--.. . /"---\ | ` | . | + -=====================,' _ \=(*#(7.#####() | `/_.. , ( + _.-''``';'-''-) ,. \ ' '+/// | .'/ \ ``-.) \ + ,' _.- (( `-' `._\ `` \_/_.' ) /`-._ ) | + ,'\ ,' _.'.`:-. \.-' / <_L )" | + _/ `._,' ,')`; `-'`' | L / / + / `. ,' ,|_/ / \ ( <_-' \ + \ / `./ ' / /,' \ /|` `. | + )\ /`._ ,'`._.-\ |) \' + / `.' )-'.-,' )__) |\ `| + : /`. `.._(--.`':`':/ \ ) \ \ + |::::\ ,'/::;-)) / ( )`. | + ||::::: . .::': :`-( |/ . | + ||::::| . :| |==[]=: . - \ + |||:::| : || : | | /\ ` | + ___ ___ '|;:::| | |' \=[]=| / \ \ +| /_ ||``|||::::: | ; | | | \_.'\_ `-. +: \_``[]--[]|::::'\_;' )-'..`._ .-'\``:: ` . \ + \___.>`''-.||:.__,' SSt |_______`> <_____:::. . . \ _/ + `+a:f:......jrei''' +``` + +You might have read my previous blog posts about entering the Geminispace, where I pointed out the benefits of having and maintaining an internet presence there. This whole site (the blog and all other pages) is composed in the Gemtext markup language. + +This comes with the benefit that I can write content in my favourite text editor (Vim). + +## Motivation + +Another benefit of using Gemini is that the Gemtext markup language is easy to parse. As my site is dual-hosted (Gemini+HTTP), I could, in theory, just write a shell script to deal with the conversion from Gemtext to HTML; there is no need for a full-featured programming language here. I have done a lot of Bash in the past, but I am also often revisiting old tools and techniques for refreshing and keeping the knowledge up to date here. + +=> ./2021-06-05-gemtexter-one-bash-script-to-rule-it-all/blog-engine.jpg Motivational comic strip + +I have exactly done that - I wrote a Bash script, named Gemtexter, for that: + +=> https://codeberg.org/snonux/gemtexter + +In short, Gemtexter is a static site generator and blogging engine that uses Gemtext as its input format. + +## Output formats + +Gemtexter takes the Gemntext Markup files as the input and generates the following outputs from it (you find examples for each of these output formats on the Gemtexter GitHub page): + +* HTML files for my website +* Markdown files for a GitHub page +* A Gemtext Atom feed for my blog posts +* A Gemfeed for my blog posts (a particular feed format commonly used in Geminispace. The Gemfeed can be used as an alternative to the Atom feed). +* An HTML Atom feed of my blog posts + +I could have done all of that with a more robust language than Bash (such as Perl, Ruby, Go...), but I didn't. The purpose of this exercise was to challenge what I can do with a "simple" Bash script and learn new things. + +## Taking it as far as I should, but no farther + +The Bash is suitable very well for small scripts and ad-hoc automation on the command line. But it is for sure not a robust programming language. Writing this blog post, Gemtexter is nearing 1000 lines of code, which is actually a pretty large Bash script. + +### Modularization + +I modularized the code so that each core functionality has its own file in ./lib. All the modules are included from the main Gemtexter script. For example, there is one module for HTML generation, one for Markdown generation, and so on. + +``` +paul in uranus in gemtexter on 🌱 main +❯ wc -l gemtexter lib/* + 117 gemtexter + 59 lib/assert.source.sh + 128 lib/atomfeed.source.sh + 64 lib/gemfeed.source.sh + 161 lib/generate.source.sh + 50 lib/git.source.sh + 162 lib/html.source.sh + 30 lib/log.source.sh + 63 lib/md.source.sh + 834 total +``` + +This way, the script could grow far beyond 1000 lines of code and still be maintainable. With more features, execution speed may slowly become a problem, though. I already notice that Gemtexter doesn't produce results instantly but requires few seconds of runtime already. That's not a problem yet, though. + +### Bash best practises and ShellCheck + +While working on Gemtexter, I also had a look at the Google Shell Style Guide and wrote a blog post on that: + +=> ./2021-05-16-personal-bash-coding-style-guide.gmi Personal bash coding style guide + +I followed all these best practices, and in my opinion, the result is a pretty maintainable Bash script (given that you are fluent with all the sed and grep commands I used). + +ShellCheck, a shell script analysis tool written in Haskell, is run on Gemtexter ensuring that all code is acceptable. I am pretty impressed with what ShellCheck found. + +It, for example, detected "some_command | while read var; do ...; done" loops and hinted that these create a new subprocess for the while part. The result is that all variable modifications taking place in the while-subprocess won't reflect the primary Bash process. ShellSheck then recommended rewriting the loop so that no subprocess is spawned as "while read -r var; do ...; done < <(some_command)". ShellCheck also pointed out to add a "-r" to "read"; otherwise, there could be an issue with backspaces in the loop data. + +Furthermore, ShellCheck recommended many more improvements. Declaration of unused variables and missing variable and string quotations were the most common ones. ShellSheck immensely helped to improve the robustness of the script. + +=> https://shellcheck.net + +### Unit testing + +There is a basic unit test module in ./lib/assert.source.sh, which is used for unit testing. I found this to be very beneficial for cross-platform development. For example, I noticed that some unit tests failed on macOS while everything still worked fine on my Fedora Linux laptop. + +After digging a bit, I noticed that I had to install the GNU versions of the sed and grep commands on macOS and a newer version of the Bash to make all unit tests pass and Gemtexter work. + +It has been proven quite helpful to have unit tests in place for the HTML part already when working on the Markdown generator part. To test the Markdown part, I copied the HTML unit tests and changed the expected outcome in the assertions. This way, I could implement the Markdown generator in a test-driven way (writing the test first and afterwards the implementation). + +### HTML unit test example + +``` +gemtext='=> http://example.org Description of the link' +assert::equals "$(generate::make_link html "$gemtext")" \ + '<a class="textlink" href="http://example.org">Description of the link</a><br />' + +``` + +### Markdown unit test example + +``` +gemtext='=> http://example.org Description of the link' +assert::equals "$(generate::make_link md "$gemtext")" \ + '[Description of the link](http://example.org) ' +``` + +## Handcrafted HTML styles + +I had a look at some ready off the shelf CSS styles, but they all seemed too bloated. There is a whole industry selling CSS styles on the interweb. I preferred an effortless and minimalist style for the HTML site. So I handcrafted the Cascading Style Sheets manually with love and included them in the HTML header template. + +For now, I have to re-generate all HTML files whenever the CSS changes. That should not be an issue now, but I might move the CSS into a separate file one day. + +It's worth mentioning that all generated HTML files and Atom feeds pass the W3C validation tests. + +## Configurability + +In case someone else than me wants to use Gemtexter for his own site, it is pretty much configurable. It is possible to specify your own configuration file and your own HTML templates. Have a look at the GitHub page for examples. + +## Future features + +I could think of the following features added to a future version of Gemtexter: + +* Templating of Gemtext files so that the .gmi files are generated from .gmi.tpl files. The template engine could do such things as an automatic table of contents and sitemap generation. It could also include the output of inlined shell code, e.g. a fortune quote. +* Add support for more output formats, such as Groff, PDF, plain text, Gopher, etc. +* External CSS file for HTML. +* Improve speed by introducing parallelism and/or concurrency and/or better caching. + +## Conclusion + +It was quite a lot of fun writing Gemtexter. It's a relatively small project, but given that I worked on that in my spare time once in a while, it kept me busy for several weeks. + +I finally revamped my personal internet site and started to blog again. I wanted the result to be exactly how it is now: A slightly retro-inspired internet site built for fun with unconventional tools. + +More related posts are: + +<< template::inline::index gemtext gemini bash + +E-Mail your comments to hi@paul.cyou :-) + +=> ../ Back to the main site diff --git a/gemfeed/2021-11-29-bash-golf-part-1.gmi b/gemfeed/2021-11-29-bash-golf-part-1.gmi index 3b1155c6..120999ba 100644 --- a/gemfeed/2021-11-29-bash-golf-part-1.gmi +++ b/gemfeed/2021-11-29-bash-golf-part-1.gmi @@ -16,8 +16,8 @@ jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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. -=> ./2021-11-29-bash-golf-part-1.gmi Bash Golf Part 1 (you are reading this atm.) -=> ./2022-01-01-bash-golf-part-2.gmi Bash Golf Part 2 +=> ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 +=> ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 (You are currently reading this) ## TCP/IP networking @@ -463,6 +463,13 @@ In the Bash you will have to fall back to an external command like "bc" (the arb See you later for the next post of this series. +More related posts are: + +=> ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 +=> ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 (You are currently reading this) +=> ./2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi 2021-06-05 Gemtexter - One Bash script to rule it all +=> ./2021-05-16-personal-bash-coding-style-guide.gmi 2021-05-16 Personal Bash coding style guide + E-Mail your comments to hi@paul.cyou :-) => ../ Back to the main site diff --git a/gemfeed/2021-11-29-bash-golf-part-1.gmi.tpl b/gemfeed/2021-11-29-bash-golf-part-1.gmi.tpl new file mode 100644 index 00000000..55ab158d --- /dev/null +++ b/gemfeed/2021-11-29-bash-golf-part-1.gmi.tpl @@ -0,0 +1,471 @@ +# Bash Golf Part 1 + +> Published at 2021-11-29T14:06:14+00:00; Updated at 2022-01-05 + +``` + + '\ . . |>18>> + \ . ' . | + O>> . 'o | + \ . | + /\ . | + / / .' | +jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + Art by Joan Stark +``` + +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. + +<< template::inline::index bash-golf + +## TCP/IP networking + +You probably know the Netcat tool, which is a swiss army knife for TCP/IP networking on the command line. But did you know that the Bash natively supports TCP/IP networking? + +Have a look here how that works: + +``` +❯ cat < /dev/tcp/time.nist.gov/13 + +59536 21-11-18 08:09:16 00 0 0 153.6 UTC(NIST) * +``` + +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). + +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: + +``` +❯ exec 5<>/dev/tcp/google.de/80 +❯ echo -e "GET / HTTP/1.1\nhost: google.de\n\n" >&5 +❯ cat <&5 | head +HTTP/1.1 301 Moved Permanently +Location: http://www.google.de/ +Content-Type: text/html; charset=UTF-8 +Date: Thu, 18 Nov 2021 08:27:18 GMT +Expires: Sat, 18 Dec 2021 08:27:18 GMT +Cache-Control: public, max-age=2592000 +Server: gws +Content-Length: 218 +X-XSS-Protection: 0 +X-Frame-Options: SAMEORIGIN +``` + +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. + +## Process substitution + +The idea here is, that you can read the output (stdout) of a command from a file descriptor: + +``` +❯ uptime # Without process substitution + 10:58:03 up 4 days, 22:08, 1 user, load average: 0.16, 0.34, 0.41 + +❯ cat <(uptime) # With process substitution + 10:58:16 up 4 days, 22:08, 1 user, load average: 0.14, 0.33, 0.41 + +❯ stat <(uptime) + File: /dev/fd/63 -> pipe:[468130] + Size: 64 Blocks: 0 IO Block: 1024 symbolic link +Device: 16h/22d Inode: 468137 Links: 1 +Access: (0500/lr-x------) Uid: ( 1001/ paul) Gid: ( 1001/ paul) +Context: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 +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: - +``` + +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. + +A useful use case is displaying the differences of two sorted files: + +``` +❯ echo a > /tmp/file-a.txt +❯ echo b >> /tmp/file-a.txt +❯ echo c >> /tmp/file-a.txt +❯ echo b > /tmp/file-b.txt +❯ echo a >> /tmp/file-b.txt +❯ echo c >> /tmp/file-b.txt +❯ echo X >> /tmp/file-b.txt +❯ diff -u <(sort /tmp/file-a.txt) <(sort /tmp/file-b.txt) +--- /dev/fd/63 2021-11-20 11:05:03.667713554 +0000 ++++ /dev/fd/62 2021-11-20 11:05:03.667713554 +0000 +@@ -1,3 +1,4 @@ + a + b + c ++X +❯ 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) +❯ +``` + +Another example is displaying the differences of two directories: + +``` +❯ diff -u <(ls ./dir1/ | sort) <(ls ./dir2/ | sort) +``` + +More (Bash golfing) examples: + +``` +❯ wc -l <(ls /tmp/) /etc/passwd <(env) + 24 /dev/fd/63 + 49 /etc/passwd + 24 /dev/fd/62 + 97 total +❯ + +❯ while read foo; do +> echo $foo +> done < <(echo foo bar baz) +foo bar baz +❯ +``` + +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: + +``` +❯ tar cjf file.tar.bz2 foo +❯ tar cjf >(bzip2 -c > file.tar.bz2) foo +``` + +Just think a while and see whether you understand fully what is happening here. + +## Grouping + +Command grouping can be quite useful for combining the output of multiple commands: + +``` +❯ { ls /tmp; cat /etc/passwd; env; } | wc -l +97 +❯ ( ls /tmp; cat /etc/passwd; env; ) | wc -l +97 +``` + +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: + +``` +❯ echo $$ +62676 +❯ { echo $$; } +62676 +❯ ( echo $$; ) +62676 +``` + +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: + +``` +❯ ( env; ls ) | wc -l +27 +❯ { env; ls } | wc -l +> +> ^C +``` + +In case you know more (subtle) differences, please write me an E-Mail and let me know. + +> 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): + +``` +(list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT + below). Variable assignments and builtin commands that affect the shell's + environment do not remain in effect after the command completes. The return + status is the exit status of list. + +{ list; } + list is simply executed in the current shell environment. list must be ter‐ + minated with a newline or semicolon. This is known as a group command. The + return status is the exit status of list. Note that unlike the metacharac‐ + 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. +``` + +So I was right that () is executed in a subprocess. But why does $$ not show a different PID? Also here (as pointed out by the reader) is the answer in the manual page: + +``` +$ Expands to the process ID of the shell. In a () subshell, it expands to the + process ID of the current shell, not the subshell. +``` + +If we want print the subprocess PID, we can use the BASHPID variable: + +``` +❯ echo $BASHPID; { echo $BASHPID; }; ( echo $BASHPID; ) +1028465 +1028465 +1028739 +``` + +## Expansions + +Let's start with simple examples: + +``` +❯ echo {0..5} +0 1 2 3 4 5 +❯ for i in {0..5}; do echo $i; done +0 +1 +2 +3 +4 +5 +``` + +You can also add leading 0 or expand to any number range: + +``` +❯ echo {00..05} +00 01 02 03 04 05 +❯ echo {000..005} +000 001 002 003 004 005 +❯ echo {201..205} +201 202 203 204 205 +``` + +It also works with letters: + +``` +❯ echo {a..e} +a b c d e +``` + +Now it gets interesting. The following takes a list of words and expands it so that all words are quoted: + +``` +❯ echo \"{These,words,are,quoted}\" +"These" "words" "are" "quoted" +``` + +Let's also expand to the cross product of two given lists: + +``` +❯ echo {one,two}\:{A,B,C} +one:A one:B one:C two:A two:B two:C +❯ echo \"{one,two}\:{A,B,C}\" +"one:A" "one:B" "one:C" "two:A" "two:B" "two:C" +``` + +Just because we can: + +``` +❯ echo Linux-{one,two,three}\:{A,B,C}-FreeBSD +Linux-one:A-FreeBSD Linux-one:B-FreeBSD Linux-one:C-FreeBSD Linux-two:A-FreeBSD Linux-two:B-FreeBSD Linux-two:C-FreeBSD Linux-three:A-FreeBSD Linux-three:B-FreeBSD Linux-three:C-FreeBSD +``` + +## - aka stdin and stdout placeholder + +Some commands and Bash builtins use "-" as a placeholder for stdin and stdout: + +``` +❯ echo Hello world +Hello world +❯ echo Hello world | cat - +Hello world +❯ cat - <<ONECHEESEBURGERPLEASE +Hello world +ONECHEESEBURGERPLEASE +Hello world +❯ cat - <<< 'Hello world' +Hello world +``` + +Let's walk through all three examples from the above snippet: + +* The first example is obvious (the Bash builtin "echo" prints its arguments to stdout). +* The second pipes "Hello world" via stdout to stdin of the "cat" command. As cat's argument is "-" it reads its data from stdin and not from a regular file named "-". So "-" has a special meaning for cat. +* The third and fourth examples are interesting as we don't use a pipe as of "|" but a so-called HERE-document and a HERE-string. But the end results are the same. + +The "tar" command understands "-" too. The following example tars up some local directory and sends the data to stdout (this is what "-f -" commands it to do). stdout then is piped via an SSH session to a remote tar process (running on buetow.org) and reads the data from stdin and extracts all the data coming from stdin (as we told tar with "-f -") on the remote machine: + +``` +❯ tar -czf - /some/dir | ssh hercules@buetow.org tar -xzvf - +``` + +This is yet another example of using "-", but this time using the "file" command: + +``` +$ head -n 1 grandmaster.sh +#!/usr/bin/env bash +$ file - < <(head -n 1 grandmaster.sh) +/dev/stdin: a /usr/bin/env bash script, ASCII text executable +``` + +Some more golfing: + +``` +$ cat - +hello +hello +^C +$ file - +#!/usr/bin/perl +/dev/stdin: Perl script text executable +``` + +## Alternative argument passing + +This is a quite unusual way of passing arguments to a Bash script: + +``` +❯ cat foo.sh +#/usr/bin/env bash +declare -r USER=${USER:?Missing the username} +declare -r PASS=${PASS:?Missing the secret password for $USER} +echo $USER:$PASS +``` + +So what we are doing here is to pass the arguments via environment variables to the script. The script will abort with an error when there's an undefined argument. + +``` +❯ chmod +x foo.sh +❯ ./foo.sh +./foo.sh: line 3: USER: Missing the username +❯ USER=paul ./foo.sh +./foo.sh: line 4: PASS: Missing the secret password for paul +❯ echo $? +1 +❯ USER=paul PASS=secret ./foo.sh +paul:secret +``` + +You have probably noticed this *strange* syntax: + +``` +❯ VARIABLE1=value1 VARIABLE2=value2 ./script.sh +``` + +That's just another way to pass environment variables to a script. You can write it as well as like this: + +``` +❯ export VARIABLE1=value1 +❯ export VARIABLE2=value2 +❯ ./script.sh +``` + +But the downside of it is that the variables will also be defined in your current shell environment and not just in the scripts sub-process. + +## : aka the null command + +First, let's use the "help" Bash built-in to see what it says about the null command: + +``` +❯ help : +:: : + Null command. + + No effect; the command does nothing. + + Exit Status: + Always succeeds. +``` + +PS: IMHO, people should use the Bash help more often. It is a very useful Bash reference. Too many fallbacks to a Google search and then land on Stack Overflow. Sadly, there's no help built-in for the ZSH shell though (so even when I am using the ZSH I make use of the Bash help as most of the built-ins are compatible). + +OK, back to the null command. What happens when you try to run it? As you can see, absolutely nothing. And its exit status is 0 (success): + +``` +❯ : +❯ echo $? +0 +``` + +Why would that be useful? You can use it as a placeholder in an endless while-loop: + +``` +❯ while : ; do date; sleep 1; done +Sun 21 Nov 12:08:31 GMT 2021 +Sun 21 Nov 12:08:32 GMT 2021 +Sun 21 Nov 12:08:33 GMT 2021 +^C +❯ +``` + +You can also use it as a placeholder for a function body not yet fully implemented, as an empty function ill result in a syntax error: + +``` +❯ foo () { } +-bash: syntax error near unexpected token `}' +❯ foo () { :; } +❯ foo +❯ +``` + +Or use it as a placeholder for not yet implemented conditional branches: + +``` +❯ if foo; then :; else echo bar; fi +``` + +Or (not recommended) as a fancy way to comment your Bash code: + +``` +❯ : I am a comment and have no other effect +❯ : I am a comment and result in a syntax error () +-bash: syntax error near unexpected token `(' +❯ : "I am a comment and don't result in a syntax error ()" +❯ +``` + +As you can see in the previous example, the Bash still tries to interpret some syntax of all text following after ":". This can be exploited (also not recommended) like this: + +``` +❯ declare i=0 +❯ $[ i = i + 1 ] +bash: 1: command not found... +❯ : $[ i = i + 1 ] +❯ : $[ i = i + 1 ] +❯ : $[ i = i + 1 ] +❯ echo $i +4 +``` + +For these kinds of expressions it's always better to use "let" though. And you should also use $((...expression...)) instead of the old (deprecated) way $[ ...expression... ] like this example demonstrates: + +``` +❯ declare j=0 +❯ let j=$((j + 1)) +❯ let j=$((j + 1)) +❯ let j=$((j + 1)) +❯ let j=$((j + 1)) +❯ echo $j +4 +``` + +## (No) floating point support + +I have to give a plus-point to the ZSH here. As the ZSH supports floating point calculation, whereas the Bash doesn't: + +``` +❯ bash -c 'echo $(( 1/10 ))' +0 +❯ zsh -c 'echo $(( 1/10 ))' +0 +❯ bash -c 'echo $(( 1/10.0 ))' +bash: line 1: 1/10.0 : syntax error: invalid arithmetic operator (error token is ".0 ") +❯ zsh -c 'echo $(( 1/10.0 ))' +0.10000000000000001 +❯ +``` + +It would be nice to have native floating point support for the Bash too, but you don't want to use the shell for complicated calculations anyway. So it's fine that Bash doesn't have that, I guess. + +In the Bash you will have to fall back to an external command like "bc" (the arbitrary precision calculator language): + +``` +❯ bc <<< 'scale=2; 1/10' +.10 +``` + +See you later for the next post of this series. + +More related posts are: + +<< template::inline::index bash + +E-Mail your comments to hi@paul.cyou :-) + +=> ../ Back to the main site diff --git a/gemfeed/2022-01-01-bash-golf-part-2.gmi b/gemfeed/2022-01-01-bash-golf-part-2.gmi index 471c1a9c..e1746dce 100644 --- a/gemfeed/2022-01-01-bash-golf-part-2.gmi +++ b/gemfeed/2022-01-01-bash-golf-part-2.gmi @@ -16,8 +16,8 @@ jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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. -=> ./2021-11-29-bash-golf-part-1.gmi Bash Golf Part 1 -=> ./2022-01-01-bash-golf-part-2.gmi Bash Golf Part 2 (you are reading this atm.) +=> ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 (You are currently reading this) +=> ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 ## Redirection @@ -480,6 +480,13 @@ To change this behaviour, pipefile can be used. Now, the pipes exit status is 1 1 ``` +More related posts are: + +=> ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 (You are currently reading this) +=> ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 +=> ./2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi 2021-06-05 Gemtexter - One Bash script to rule it all +=> ./2021-05-16-personal-bash-coding-style-guide.gmi 2021-05-16 Personal Bash coding style guide + E-Mail your comments to hi@paul.cyou :-) => ../ Back to the main site diff --git a/gemfeed/2022-01-01-bash-golf-part-2.gmi.tpl b/gemfeed/2022-01-01-bash-golf-part-2.gmi.tpl new file mode 100644 index 00000000..fe09490d --- /dev/null +++ b/gemfeed/2022-01-01-bash-golf-part-2.gmi.tpl @@ -0,0 +1,488 @@ +# Bash Golf Part 2 + +> Published at 2022-01-01T23:36:15+00:00; Updated at 2022-01-05 + +``` + + '\ '\ . . |>18>> + \ \ . ' . | + O>> O>> . 'o | + \ .\. .. . | + /\ . /\ . . | + / / . / / .' . | +jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + Art by Joan Stark, mod. by Paul Buetow +``` + +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. + +<< template::inline::index bash-golf + +## Redirection + +Let's have a closer look at Bash redirection. As you might already know that there are 3 standard file descriptors: + +* 0 aka stdin (standard input) +* 1 aka stdout (standard output) +* 2 aka stderr (standard error output) + +These are most certainly the ones you are using on regular basis. "/proc/self/fd" lists all file descriptors which are open by the current process (in this case: the current Bash shell itself): + +``` +❯ ls -l /proc/self/fd/ +total 0 +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 +``` + +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: + +``` +❯ echo Foo +Foo +❯ echo Foo > /proc/self/fd/0 +Foo +``` + +Other useful redirections are: + +* Redirect stderr to stdin: "echo foo 2>&1" +* Redirect stdin to stderr: "echo foo >&2" + +It is, however, not possible to redirect multiple times within the same command. E.g. the following won't work. You would expect stdin to be redirected to stderr and then stderr to be redirected to /dev/null. But as the example shows, Foo is still printed out: + +``` +❯ echo Foo 1>&2 2>/dev/null +Foo +``` + +> Update: A reader sent me an email and pointed out that the order of the redirections is important. + +As you can see, the following will not print out anything: + +``` +❯ echo Foo 2>/dev/null 1>&2 +❯ +``` + +A good description (also pointed out by the reader) can be found here: + +=> https://wiki.bash-hackers.org/howto/redirection_tutorial#order_of_redirection_ie_file_2_1_vs_2_1_file Order of redirection + +Ok, back to the original blog post. You can also use grouping here (neither of these commands will print out anything to stdout): + +``` +❯ { echo Foo 1>&2; } 2>/dev/null +❯ ( echo Foo 1>&2; ) 2>/dev/null +❯ { { { echo Foo 1>&2; } 2>&1; } 1>&2; } 2>/dev/null +❯ ( ( ( echo Foo 1>&2; ) 2>&1; ) 1>&2; ) 2>/dev/null +❯ +``` + +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: + +``` +❯ lsof -a -p $$ -d0,1,2 +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 +``` + +Let's create our own descriptor "3" for redirection to a file named "foo": + +``` +❯ touch foo +❯ exec 3>foo # This opens fd 3 and binds it to file foo. +❯ ls -l /proc/self/fd/3 +l-wx------. 1 paul paul 64 Nov 23 10:10 \ + /proc/self/fd/3 -> /home/paul/foo +❯ cat foo +❯ echo Bratwurst >&3 +❯ cat foo +Bratwurst +❯ exec 3>&- # This closes fd 3. +❯ echo Steak >&3 +-bash: 3: Bad file descriptor +``` + +You can also override the default file descriptors, as the following example script demonstrates: + +``` +❯ cat grandmaster.sh +#!/usr/bin/env bash + +# Write a file data-file containing two lines +echo Learn You a Haskell > data-file +echo for Great Good >> data-file + +# Link fd with fd 6 (saves default stdin) +exec 6<&0 + +# Overwrite stdin with data-file +exec < data-file + +# Read the first two lines from it +declare LINE1 LINE2 +read LINE1 +read LINE2 + +# Print them +echo First line: $LINE1 +echo Second line: $LINE2 + +# Restore default stdin and delete fd 6 +exec 0<&6 6<&- +``` + +Let's execute it: + +``` +❯ chmod 750 ./grandmaster.sh +❯ ./grandmaster.sh +First line: Learn You a Haskell +Second line: for Great Good +``` + +## HERE + +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). + +``` +❯ cat <<END +> Hello World +> It’s $(date) +> END +Hello World +It's Fri 26 Nov 08:46:52 GMT 2021 +``` + +You can also write it the following way, but that's less readable (it's good for an obfuscation contest): + +``` +❯ <<END cat +> Hello Universe +> It’s $(date) +> END +Hello Universe +It's Fri 26 Nov 08:47:32 GMT 2021 +``` + +Besides of an HERE-document, there is also a so-called HERE-string. Besides of... + +``` +❯ declare VAR=foo +❯ if echo "$VAR" | grep -q foo; then +> echo '$VAR ontains foo' +> fi +$VAR ontains foo +``` + +...you can use a HERE-string like that: + +``` +❯ if grep -q foo <<< "$VAR"; then +> echo '$VAR contains foo' +> fi +$VAR contains foo +``` + +Or even shorter, you can do: + +``` +❯ grep -q foo <<< "$VAR" && echo '$VAR contains foo' +$VAR contains foo +``` + +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: + +``` +❯ if [[ "$VAR" =~ foo ]]; then echo yay; fi +yay +``` + +You can also use it with "read": + +``` +❯ read a <<< ja +❯ echo $a +ja +❯ read b <<< 'NEIN!!!' +❯ echo $b +NEIN!!! +❯ dumdidumstring='Learn you a Golang for Great Good' +❯ read -a words <<< "$dumdidumstring" +❯ echo ${words[0]} +Learn +❯ echo ${words[3]} +Golang +``` + +The following is good for an obfuscation contest too: + +``` +❯ echo 'I like Perl too' > perllove.txt +❯ cat - perllove.txt <<< "$dumdidumstring" +Learn you a Golang for Great Good +I like Perl too +``` + +## RANDOM + +Random is a special built-in variable containing a different pseudo random number each time it's used. + +``` +❯ echo $RANDOM +11811 +❯ echo $RANDOM +14997 +❯ echo $RANDOM +9104 +``` + +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. + +Let's say you want to introduce a random delay of 1 minute. You can accomplish it with: + +``` +❯ cat ./calc_answer_to_ultimate_question_in_life.sh +#!/usr/bin/env bash + +declare -i MAX_DELAY=60 + +random_delay () { + local -i sleep_for=$((RANDOM % MAX_DELAY)) + echo "Delaying script execution for $sleep_for seconds..." + sleep $sleep_for + echo 'Continuing script execution...' +} + +main () { + random_delay + # From here, do the real work. Calculating the answer to + # the ultimate question can take billions of years.... + : .... +} + +main + +❯ +❯ ./calc_answer_to_ultimate_question_in_life.sh +Delaying script execution for 42 seconds... +Continuing script execution... +``` + +## set -x and set -e and pipefile + +In my opinion, -x and -e and pipefile are the most useful Bash options. Let's have a look at them one after another. + +### -x + +-x prints commands and their arguments as they are executed. This helps to develop and debug your Bash code: + +``` +❯ set -x +❯ square () { local -i num=$1; echo $((num*num)); } +❯ num=11; echo "Square of $num is $(square $num)" ++ num=11 +++ square 11 +++ local -i num=11 +++ echo 121 ++ echo 'Square of 11 is 121' +Square of 11 is 121 +``` + +You can also set it when calling an external script without modifying the script itself: + +``` +❯ bash -x ./half_broken_script_to_be_debugged.sh +``` + +Let's do that on one of the example scripts we covered earlier: + +``` +❯ bash -x ./grandmaster.sh ++ bash -x ./grandmaster.sh ++ echo Learn You a Haskell ++ echo for Great Good ++ exec ++ exec ++ declare LINE1 LINE2 ++ read LINE1 ++ read LINE2 ++ echo First line: Learn You a Haskell +First line: Learn You a Haskell ++ echo Second line: for Great Good +Second line: for Great Good ++ exec +❯ +``` + +### -e + +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). + +Ok, let's dig deeper: + +``` +❯ help set | grep -- -e + -e Exit immediately if a command exits with a non-zero status. +``` + +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: + +``` +❯ bash -c 'set -e; echo hello; grep -q bar <<< foo; echo bar' +hello +❯ echo $? +1 +``` + +Whereas the outcome changes when the regex matches: + +``` +❯ bash -c 'set -e; echo hello; grep -q bar <<< barman; echo bar' +hello +bar +❯ echo $? +0 +``` + +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: + +``` +❯ bash -c 'set -e +> grep -q bar <<< foo +> if [ $? -eq 0 ]; then +> echo "matching" +> else +> echo "not matching" +> fi' +❯ echo $? +1 +``` + +...but the example above won't reach any of the branches and won't print out anything, as the script terminates right after grep. + +The proper solution is to use grep as an expression in a conditional (e.g. in an if-else statement): + +``` +❯ bash -c 'set -e +> if grep -q bar <<< foo; then +> echo "matching" +> else +> echo "not matching" +> fi' +not matching +❯ echo $? +0 +❯ bash -c 'set -e +> if grep -q bar <<< barman; then +> echo "matching" +> else +> echo "not matching" +> fi' +matching +❯ echo $? +0 +``` + +You can also temporally undo "set -e" if there is no other way: + +``` +❯ cat ./e.sh +#!/usr/bin/env bash + +set -e + +foo () { + local arg="$1"; shift + + if [ -z "$arg" ]; then + arg='You!' + fi + echo "Hello $arg" +} + +bar () { + # Temporally disable e + set +e + local arg="$1"; shift + # Enable e again. + set -e + + if [ -z "$arg" ]; then + arg='You!' + fi + echo "Hello $arg" +} + +# Will succeed +bar World +foo Universe +bar + +# Will terminate the script +foo + +❯ ./e.sh +Hello World +Hello Universe +Hello You! +``` + +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. + +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 :-): + +=> ./2021-05-16-personal-bash-coding-style-guide.gmi + +### pipefail + +The pipefail option makes it so that not only the exit code of the last command of the pipe counts regards its exit code but any command of the pipe: + +``` +❯ help set | grep pipefail -A 2 + 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 +``` + +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: + +``` +❯ grep paul /etc/passwd | tr '[a-z]' '[A-Z]' +PAUL:X:1000:1000:PAUL BUETOW:/HOME/PAUL:/BIN/BASH +❯ echo $? +0 +``` + +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: + +``` +❯ grep TheRock /etc/passwd +❯ echo $? +1 +❯ grep TheRock /etc/passwd | tr '[a-z]' '[A-Z]' +❯ echo $? +0 +``` + +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: + +``` +❯ set -o pipefail +❯ grep TheRock /etc/passwd | tr '[a-z]' '[A-Z]' +❯ echo $? +1 +``` + +More related posts are: + +<< template::inline::index bash + +E-Mail your comments to hi@paul.cyou :-) + +=> ../ Back to the main site diff --git a/gemfeed/2022-03-06-the-release-of-dtail-4.0.0.gmi b/gemfeed/2022-03-06-the-release-of-dtail-4.0.0.gmi index 4787ae20..16177d9f 100644 --- a/gemfeed/2022-03-06-the-release-of-dtail-4.0.0.gmi +++ b/gemfeed/2022-03-06-the-release-of-dtail-4.0.0.gmi @@ -16,11 +16,7 @@ | | ``` -I have recently released DTail 4.0.0 and this blog post goes through all the new goodies. You can also read my previous post about DTail in case you wonder what DTail is: - -=> ./2021-04-22-dtail-the-distributed-log-tail-program.gmi DTail - The distributed log tail program - -If you want to jump directly to DTail, do it here (there are nice animated gifs which demonstrates the usage pretty well): +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): => https://dtail.dev @@ -290,6 +286,12 @@ I use usually DTail at work, but I have recently installed it on my personal Ope I am a bit busy at the moment with two other pet projects of mine (one internal work-project, and one personal one, the latter you will read about in the next couple of months). If you have ideas (or even a patch), then please don't hesitate to contact me (either via E-Mail or a request at GitHub). +More related posts are: + +=> ./2022-10-30-installing-dtail-on-openbsd.gmi 2022-10-30 Installing DTail on OpenBSD +=> ./2022-03-06-the-release-of-dtail-4.0.0.gmi 2022-03-06 The release of DTail 4.0.0 (You are currently reading this) +=> ./2021-04-22-dtail-the-distributed-log-tail-program.gmi 2021-04-22 DTail - The distributed log tail program + Thanks! Paul diff --git a/gemfeed/2022-03-06-the-release-of-dtail-4.0.0.gmi.tpl b/gemfeed/2022-03-06-the-release-of-dtail-4.0.0.gmi.tpl new file mode 100644 index 00000000..81bae318 --- /dev/null +++ b/gemfeed/2022-03-06-the-release-of-dtail-4.0.0.gmi.tpl @@ -0,0 +1,299 @@ +# The release of DTail 4.0.0 + +> Published at 2022-03-06T18:11:39+00:00 + +``` + ,_---~~~~~----._ + _,,_,*^____ _____``*g*\"*, + ____ _____ _ _ / __/ /' ^. / \ ^@q f + | _ \_ _|_ _(_) | @f | @)) | | @)) l 0 _/ + | | | || |/ _` | | | \`/ \~____ / __ \_____/ \ + | |_| || | (_| | | | | _l__l_ I + |____/ |_|\__,_|_|_| } [______] I + ] | | | | + ] ~ ~ | + | | + | | +``` + +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): + +=> https://dtail.dev + +## So, what's new in 4.0.0? + +### Rewritten logging + +For DTail 4, logging has been completely rewritten. The new package name is "internal/io/dlog". I rewrote the logging because DTail is a special case here: There are logs processed by DTail, there are logs produced by the DTail server itself, there are logs produced by a DTail client itself, there are logs only logged by a DTail client, there are logs only logged by the DTail server, and there are logs logged by both, server and client. There are also different logging levels and outputs involved. + +As you can imagine, it becomes fairly complex. There is no ready Go off-shelf logging library which suits my needs and the logging code in DTail 3 was just one big source code file with global variables and it wasn't sustainable to maintain anymore. So why not rewrite it for profit and fun? + +There's a are new log level structure now (The log level now can be specified with the "-logLevel" command line flag): + +``` +// Available log levels. +const ( + None level = iota + Fatal level = iota + Error level = iota + Warn level = iota + Info level = iota + Default level = iota + Verbose level = iota + Debug level = iota + Devel level = iota + Trace level = iota + All level = iota +) +``` + +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). + +### Configurable terminal color codes + +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, ...). + +The example "dtail.json" configuration file represents the default (now, more reasonable default) color codes used, and it is free to the user to customize them: + +``` +{ + "Client": { + "TermColorsEnable": true, + "TermColors": { + "Remote": { + "DelimiterAttr": "Dim", + "DelimiterBg": "Blue", + "DelimiterFg": "Cyan", + "RemoteAttr": "Dim", + "RemoteBg": "Blue", + "RemoteFg": "White", + "CountAttr": "Dim", + "CountBg": "Blue", + "CountFg": "White", + "HostnameAttr": "Bold", + "HostnameBg": "Blue", + "HostnameFg": "White", + "IDAttr": "Dim", + "IDBg": "Blue", + "IDFg": "White", + "StatsOkAttr": "None", + "StatsOkBg": "Green", + "StatsOkFg": "Black", + "StatsWarnAttr": "None", + "StatsWarnBg": "Red", + "StatsWarnFg": "White", + "TextAttr": "None", + "TextBg": "Black", + "TextFg": "White" + }, + "Client": { + "DelimiterAttr": "Dim", + "DelimiterBg": "Yellow", + "DelimiterFg": "Black", + "ClientAttr": "Dim", + "ClientBg": "Yellow", + "ClientFg": "Black", + "HostnameAttr": "Dim", + "HostnameBg": "Yellow", + "HostnameFg": "Black", + "TextAttr": "None", + "TextBg": "Black", + "TextFg": "White" + }, + "Server": { + "DelimiterAttr": "AttrDim", + "DelimiterBg": "BgCyan", + "DelimiterFg": "FgBlack", + "ServerAttr": "AttrDim", + "ServerBg": "BgCyan", + "ServerFg": "FgBlack", + "HostnameAttr": "AttrBold", + "HostnameBg": "BgCyan", + "HostnameFg": "FgBlack", + "TextAttr": "AttrNone", + "TextBg": "BgBlack", + "TextFg": "FgWhite" + }, + "Common": { + "SeverityErrorAttr": "AttrBold", + "SeverityErrorBg": "BgRed", + "SeverityErrorFg": "FgWhite", + "SeverityFatalAttr": "AttrBold", + "SeverityFatalBg": "BgMagenta", + "SeverityFatalFg": "FgWhite", + "SeverityWarnAttr": "AttrBold", + "SeverityWarnBg": "BgBlack", + "SeverityWarnFg": "FgWhite" + }, + "MaprTable": { + "DataAttr": "AttrNone", + "DataBg": "BgBlue", + "DataFg": "FgWhite", + "DelimiterAttr": "AttrDim", + "DelimiterBg": "BgBlue", + "DelimiterFg": "FgWhite", + "HeaderAttr": "AttrBold", + "HeaderBg": "BgBlue", + "HeaderFg": "FgWhite", + "HeaderDelimiterAttr": "AttrDim", + "HeaderDelimiterBg": "BgBlue", + "HeaderDelimiterFg": "FgWhite", + "HeaderSortKeyAttr": "AttrUnderline", + "HeaderGroupKeyAttr": "AttrReverse", + "RawQueryAttr": "AttrDim", + "RawQueryBg": "BgBlack", + "RawQueryFg": "FgCyan" + } + } + }, + ... +} +``` + +You notice the different sections - these are different contexts: + +* Remote: Color configuration for all log lines sent remotely from the server to the client. +* Client: Color configuration for all lines produced by a DTail client by itself (e.g. status information). +* Server: Color configuration for all lines produced by the DTail server by itself and sent to the client (e.g. server warnings or errors) +* MaprTable: Color configuration for the map-reduce table output. +* Common: Common color configuration used in various places (e.g. when it's not clear what's the current context of a line). + +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: + +``` +jsonschema -i dtail.json schemas/dtail.schema.json +``` + +### Serverless mode + +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: + +``` +% dtail --files /var/log/foo.log +``` + +or + +``` +% dmap --files /var/log/foo.log --query 'from TABLE select .... outfile result.csv' +``` + +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. + +### Shorthand flags + +The "--files" from the previous example is now redundant. As a shorthand, It is now possible to do the following instead: + +``` +% dtail /var/log/foo.log +``` + +Of course, this also works with all other DTail client commands (dgrep, dcat, ... etc). + +### Spartan (aka plain) mode + +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.: + +``` +% dcat --plain /etc/passwd > /etc/test +% diff /etc/test /etc/passwd # Same content, no diff +``` + +This might be useful if you wanted to post-process the output. + +### Standard input pipe + +In serverless mode, you might want to process your data in a pipeline. You can do that now too through an input pipe: + +``` +% dgrep --plain --regex 'somethingspecial' /var/log/foo.log | + dmap --query 'from TABLE select .... outfile result.csv' +``` + +Or, use any other "standard" tool: + +``` +% awk '.....' < /some/file | dtail .... +``` + +### New command dtailhealth + +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). + +``` +% cat check_dtail.sh +#!/bin/sh + +exec /usr/local/bin/dtailhealth --server localhost:2222 +``` + +### Improved documentation + +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). + +I must admit that not all features are documented yet: + +* Server side scheduled map-reduce queries +* Server side continuous map-reduce queries +* Some more docs about terminal color customization +* Some more docs about log levels + +That will be added in one of the future releases. + +### Integration testing suite + +DTail comes already with some unit tests, but what's new is a full integration testing suite which covers all common use cases of all the commands (dtail, dcat, dgrep, dmap) with a server backend and also in serverless mode. + +How are the tests implemented? All integration tests are simply unit tests in the "./integrationtests" folder. They must be explicitly activated with: + +``` +% export DTAIL_INTEGRATION_TEST_RUN_MODE=yes +``` + +Once done, first compile all commands, and then run the integration tests: + +``` +% make +. +. +. +% go clean -testcache +% go test -race -v ./integrationtests +``` + +### Improved code + +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: + +* Reduce the complexity of a couple of functions (splitting code up into several smaller functions) +* Avoid repeating code (this version of DTail doesn't use Go generics yet, though). + +Other than that, a lot of other code has been refactored as I saw fit. + +### Use of memory pools + +DTail makes excessive use of string builder and byte buffer objects. For performance reasons, those are now re-used from memory pools. + +## What's next + +DTail 5 won't be released any time soon I guess, but some 4.x.y releases will follow this year fore sure. I can think of: + +* New (but backwards compatible) features which don't require a new major version bump (some features have been requested at work internally). +* Even more improved documentation. +* Dependency updates. + +I use usually DTail at work, but I have recently installed it on my personal OpenBSD machines too. I might write a small tutorial here (and I might also add the rc scripts as examples to one of the next DTail releases). + +I am a bit busy at the moment with two other pet projects of mine (one internal work-project, and one personal one, the latter you will read about in the next couple of months). If you have ideas (or even a patch), then please don't hesitate to contact me (either via E-Mail or a request at GitHub). + +More related posts are: + +<< template::inline::index dtail + +Thanks! + +Paul + +E-Mail your comments to hi@paul.cyou :-) + +=> ../ Back to the main site diff --git a/gemfeed/2022-08-27-gemtexter-1.1.0-lets-gemtext-again.gmi b/gemfeed/2022-08-27-gemtexter-1.1.0-lets-gemtext-again.gmi index 229c71a2..aa290d23 100644 --- a/gemfeed/2022-08-27-gemtexter-1.1.0-lets-gemtext-again.gmi +++ b/gemfeed/2022-08-27-gemtexter-1.1.0-lets-gemtext-again.gmi @@ -14,10 +14,7 @@ jgs `"""""""""` ``` -I am proud to announce that I've released Gemtexter version `1.1.0`. What is Gemtexter? It's my static site generator written in GNU Bash: - -=> ./2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi Gemtexter - One Bash script to rule it all -=> https://codeberg.org/snonux/gemtexter +I am proud to announce that I've released Gemtexter version `1.1.0`. What is Gemtexter? It's my static site generator written in GNU Bash. It has been around a year since I released the first version `1.0.0`. 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. @@ -86,6 +83,12 @@ Additionally, there were a couple of bug fixes, refactorings and overall improve Overall I think it's a pretty solid `1.1.0` release without anything groundbreaking (therefore no major version jump). But I am happy about it. +More related posts are: + +=> ./2022-08-27-gemtexter-1.1.0-lets-gemtext-again.gmi 2022-08-27 Gemtexter 1.1.0 - Let's Gemtext again (You are currently reading this) +=> ./2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi 2021-06-05 Gemtexter - One Bash script to rule it all +=> ./2021-04-24-welcome-to-the-geminispace.gmi 2021-04-24 Welcome to the Geminispace + E-Mail your comments to hi@paul.cyou :-) => ../ Back to the main site diff --git a/gemfeed/2022-08-27-gemtexter-1.1.0-lets-gemtext-again.gmi.tpl b/gemfeed/2022-08-27-gemtexter-1.1.0-lets-gemtext-again.gmi.tpl new file mode 100644 index 00000000..09471d0f --- /dev/null +++ b/gemfeed/2022-08-27-gemtexter-1.1.0-lets-gemtext-again.gmi.tpl @@ -0,0 +1,92 @@ +# Gemtexter 1.1.0 - Let's Gemtext again + +> Published at 2022-08-27T18:25:57+01:00 + +``` +-=[ typewriter ]=- 1/98 + + .-------. + _|~~ ~~ |_ + =(_|_______|_)= + |:::::::::| + |:::::::[]| + |o=======.| + jgs `"""""""""` +``` + +I am proud to announce that I've released Gemtexter version `1.1.0`. What is Gemtexter? It's my static site generator written in GNU Bash. + +It has been around a year since I released the first version `1.0.0`. 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. + +## What's new? + +### Automatic check for GNU version requirements + +Gemtexter relies on the GNU versions of the tools `grep`, `sed` and `date` and it also requires the Bash shell in version 5 at least. That's now done in the `check_dependencies()` function: + +``` +check_dependencies () { + # At least, Bash 5 is required + local -i required_version=5 + IFS=. read -ra version <<< "$BASH_VERSION" + if [ "${version[0]}" -lt $required_version ]; then + log ERROR "ERROR, \"bash\" must be at least at major version $required_version!" + exit 2 + fi + + # These must be the GNU versions of the commands + for tool in $DATE $SED $GREP; do + if ! $tool --version | grep -q GNU; then + log ERROR "ERROR, \"$tool\" command is not the GNU version, please install!" + exit 2 + fi + done +} +``` + +Especially macOS users didn't read the `README` carefully enough to install GNU Grep, GNU Sed and GNU Date before using Gemtexter. + +### Backticks now produce `inline code blocks` in the HTML output + +The Gemtext format doesn't support inline code blocks, but Gemtexter now produces `inline code blocks` (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. + +### Cache for Atom feed generation + +The Bash is not the most performant language. Gemtexter already takes a couple of seconds only to generate the Atom feed for around two hand full of articles on my slightly underpowered Surface Go 2 Linux tablet. Therefore, I introduced a cache, so that subsequent Atom feed generation runs finish much quicker. The cache uses a checksum of the Gemtext `.gmi` file to decide whether anything of the content has changed or not. + +### Input filter support + +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: + +``` +./gemtexter --generate '.*hello.*' +``` + +### Revamped `git` support + +The Git support has been completely rewritten. It's now more reliable and faster too. Have a look at the `README` for more information. + +### Addition of `htmlextras` and web font support + +The `htmlextras` folder now contains all extra files required for the HTML output format such as cascading style sheet (CSS) files and web fonts. + +### Sub-section support + +It's now possible to define sub-sections within a Gemtexter capsule. For the HTML output, each sub-section can use its own CSS and web font definitions. E.g.: + +=> https://foo.zone The foo.zone main site +=> https://foo.zone/notes The notes sub-section (with different fonts) + +### More + +Additionally, there were a couple of bug fixes, refactorings and overall improvements in the documentation made. + +Overall I think it's a pretty solid `1.1.0` release without anything groundbreaking (therefore no major version jump). But I am happy about it. + +More related posts are: + +<< template::inline::index gemtext gemini + +E-Mail your comments to hi@paul.cyou :-) + +=> ../ Back to the main site diff --git a/gemfeed/2022-10-30-installing-dtail-on-openbsd.gmi b/gemfeed/2022-10-30-installing-dtail-on-openbsd.gmi index 51588c4a..33153aba 100644 --- a/gemfeed/2022-10-30-installing-dtail-on-openbsd.gmi +++ b/gemfeed/2022-10-30-installing-dtail-on-openbsd.gmi @@ -339,6 +339,12 @@ Check out the following for more information: => https://github.com/mimecast/dtail => https://www.rexify.org +More related posts are: + +=> ./2022-10-30-installing-dtail-on-openbsd.gmi 2022-10-30 Installing DTail on OpenBSD (You are currently reading this) +=> ./2022-03-06-the-release-of-dtail-4.0.0.gmi 2022-03-06 The release of DTail 4.0.0 +=> ./2021-04-22-dtail-the-distributed-log-tail-program.gmi 2021-04-22 DTail - The distributed log tail program + E-Mail your comments to hi@paul.cyou :-) => ../ Back to the main site diff --git a/gemfeed/2022-10-30-installing-dtail-on-openbsd.gmi.tpl b/gemfeed/2022-10-30-installing-dtail-on-openbsd.gmi.tpl new file mode 100644 index 00000000..8e00011a --- /dev/null +++ b/gemfeed/2022-10-30-installing-dtail-on-openbsd.gmi.tpl @@ -0,0 +1,348 @@ +# Installing DTail on OpenBSD + +> Published at 2022-10-30T11:03:19+02:00 + +``` + ,_---~~~~~----._ + _,,_,*^____ _____``*g*\"*, +/ __/ /' ^. / \ ^@q f + @f | | | | 0 _/ +\`/ \~__((@/ __ \__((@/ \ + | _l__l_ I <--- The Go Gopher + } [______] I + ] | | | | + ] ~ ~ | + | | + | | + | | A ; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~|~~~,--,-/ \---,-/|~~,~~~~~~~~~~~~~~~~~~~~~~~~~~~ + _|\,'. /| /| `/|-. + \`.' /| , `;. + ,'\ A A A A _ /| `.; + ,/ _ A _ / _ /| ; + /\ / \ , , A / / `/| + /_| | _ \ , , ,/ \ + // | |/ `.\ ,- , , ,/ ,/ \/ + / @| |@ / /' \ \ , > /| ,--. + |\_/ \_/ / | | , ,/ \ ./' __:.. + | __ __ | | | .--. , > > |-' / ` + ,/| / ' \ | | | \ , | / + / |<--.__,->| | | . `. > > / ( + /_,' \\ ^ / \ / / `. >-- /^\ | + \\___/ \ / / \__' \ \ \/ \ | + `. |/ , , /`\ \ ) + \ ' |/ , V \ / `-\ + OpenBSD Puffy ---> `|/ ' V V \ \.' \_ + '`-. V V \./'\ + `|/-. \ / \ /,---`\ kat + / `._____V_____V' + ' ' +``` + +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 :-) + + 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! + +=> https://dtail.dev + +I am using Rexify for my OpenBSD automation. Check out the following article covering my Rex setup in a little bit more detail: + +=> ./2022-07-30-lets-encrypt-with-openbsd-and-rex.gmi Let's Encrypt with OpenBSD and Rex + +I will also mention some relevant `Rexfile` snippets in this post! + +## Compile it + +First of all, DTail needs to be downloaded and compiled. For that, `git`, `go`, and `gmake` are required: + +``` +$ doas pkg_add git go gmake +``` + +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: + +``` +$ mkdir git +$ cd git +$ git clone https://github.com/mimecast/dtail +$ cd dtail +$ gmake +``` + +You can verify the version by running the following command: + +``` +$ ./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 +``` + +Now, there isn't any need anymore to keep `git`, `go` and `gmake`, so they can be deinstalled now: + +``` +$ doas pkg_delete git go gmake +``` + +One day I shall create an official OpenBSD port for DTail. + +## Install it + +Installing the binaries is now just a matter of copying them to `/usr/local/bin` as follows: + +``` +$ 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 +``` + +Also, we will be creating the `_dserver` service user: + +``` +$ doas adduser -class nologin -group _dserver -batch _dserver +$ doas usermod -d /var/run/dserver/ _dserver +``` + +The OpenBSD init script is created from scratch (not part of the official DTail project). Run the following to install the bespoke script: + +``` +$ cat <<'END' | doas tee /etc/rc.d/dserver +#!/bin/ksh + +daemon="/usr/local/bin/dserver" +daemon_flags="-cfg /etc/dserver/dtail.json" +daemon_user="_dserver" + +. /etc/rc.d/rc.subr + +rc_reload=NO + +rc_pre() { + install -d -o _dserver /var/log/dserver + install -d -o _dserver /var/run/dserver/cache +} + +rc_cmd $1 & +END +$ doas chmod 755 /etc/rc.d/dserver +``` + +### Rexification + +This is the task for setting it up via Rex. Note the `. . . .`, that's a placeholder which we will fill up more and more during this blog post: + +``` +desc 'Setup DTail'; +task 'dtail', group => 'frontends', + sub { + my $restart = FALSE; + + file '/etc/rc.d/dserver': + content => template('./etc/rc.d/dserver.tpl'), + owner => 'root', + group => 'wheel', + mode => '755', + on_change => sub { $restart = TRUE }; + + . + . + . + . + + service 'dserver' => 'restart' if $restart; + service 'dserver', ensure => 'started'; + }; +``` + +## Configure it + +Now, DTail is fully installed but still needs to be configured. Grab the default config file from GitHub ... + +``` +$ doas mkdir /etc/dserver +$ curl https://raw.githubusercontent.com/mimecast/dtail/master/samples/dtail.json.sample | + doas tee /etc/dserver/dtail.json +``` + +... and then edit it and adjust `LogDir` in the `Common` section to `/var/log/dserver`. The result will look like this: + +``` + "Common": { + "LogDir": "/var/log/dserver", + "Logger": "Fout", + "LogRotation": "Daily", + "CacheDir": "cache", + "SSHPort": 2222, + "LogLevel": "Info" + } +``` + +### Rexification + +That's as simple as adding the following to the Rex task: + +``` +file '/etc/dserver', + ensure => 'directory'; + +file '/etc/dserver/dtail.json', + content => template('./etc/dserver/dtail.json.tpl'), + owner => 'root', + group => 'wheel', + mode => '755', + on_change => sub { $restart = TRUE }; +``` + +## Update the key cache for it + +DTail relies on SSH for secure authentication and communication. However, the system user `_dserver` 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 `/var/run/dserver/cache`. + +The following script, populating the DTail server key cache, can be run periodically via `CRON`: + +``` +$ cat <<'END' | doas tee /usr/local/bin/dserver-update-key-cache.sh +#!/bin/ksh + +CACHEDIR=/var/run/dserver/cache +DSERVER_USER=_dserver +DSERVER_GROUP=_dserver + +echo 'Updating SSH key cache' + +ls /home/ | while read remoteuser; do + keysfile=/home/$remoteuser/.ssh/authorized_keys + + if [ -f $keysfile ]; then + cachefile=$CACHEDIR/$remoteuser.authorized_keys + echo "Caching $keysfile -> $cachefile" + + cp $keysfile $cachefile + chown $DSERVER_USER:$DSERVER_GROUP $cachefile + chmod 600 $cachefile + fi +done + +# Cleanup obsolete public SSH keys +find $CACHEDIR -name \*.authorized_keys -type f | +while read cachefile; do + remoteuser=$(basename $cachefile | cut -d. -f1) + keysfile=/home/$remoteuser/.ssh/authorized_keys + + if [ ! -f $keysfile ]; then + echo 'Deleting obsolete cache file $cachefile' + rm $cachefile + fi +done + +echo 'All set...' +END +$ doas chmod 500 /usr/local/bin/dserver-update-key-cache.sh +``` + +Note that the script above is a slight variation of the official DTail script. The official DTail one is a `bash` script, but on OpenBSD, there's `ksh`. I run it once daily by adding it to the `daily.local`: + +``` +$ echo /usr/local/bin/dserver-update-key-cache.sh | doas tee -a /etc/daily.local +/usr/local/bin/dserver-update-key-cache.sh +``` + +### Rexification + +That's done by adding ... + +``` +file '/usr/local/bin/dserver-update-key-cache.sh', + content => template('./scripts/dserver-update-key-cache.sh.tpl'), + owner => 'root', + group => 'wheel', + mode => '500'; + +append_if_no_such_line '/etc/daily.local', '/usr/local/bin/dserver-update-key-cache.sh'; +``` + +... to the Rex task! + +## Start it + +Now, it's time to enable and start the DTail server: + +``` +$ sudo rcctl enable dserver +$ sudo rcctl start dserver +$ tail -f /var/log/dserver/*.log +INFO|1022-090634|Starting scheduled job runner after 2s +INFO|1022-090634|Starting continuous job runner after 2s +INFO|1022-090644|24204|stats.go:53|2|11|7|||MAPREDUCE:STATS|currentConnections=0|lifetimeConnections=0 +INFO|1022-090654|24204|stats.go:53|2|11|7|||MAPREDUCE:STATS|currentConnections=0|lifetimeConnections=0 +INFO|1022-090719|Starting server|DTail 4.1.0 Protocol 4.1 Have a lot of fun! +INFO|1022-090719|Generating private server RSA host key +INFO|1022-090719|Starting server +INFO|1022-090719|Binding server|0.0.0.0:2222 +INFO|1022-090719|Starting scheduled job runner after 2s +INFO|1022-090719|Starting continuous job runner after 2s +INFO|1022-090729|86050|stats.go:53|2|11|7|||MAPREDUCE:STATS|currentConnections=0|lifetimeConnections=0 +INFO|1022-090739|86050|stats.go:53|2|11|7|||MAPREDUCE:STATS|currentConnections=0|lifetimeConnect +. +. +. +Ctr+C +``` + +As we don't want to wait until tomorrow, let's populate the key cache manually: + +``` +$ doas /usr/local/bin/dserver-update-key-cache.sh +Updating SSH key cache +Caching /home/_dserver/.ssh/authorized_keys -> /var/cache/dserver/_dserver.authorized_keys +Caching /home/admin/.ssh/authorized_keys -> /var/cache/dserver/admin.authorized_keys +Caching /home/failunderd/.ssh/authorized_keys -> /var/cache/dserver/failunderd.authorized_keys +Caching /home/git/.ssh/authorized_keys -> /var/cache/dserver/git.authorized_keys +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... +``` + +## Use it + +The DTail server is now ready to serve connections. You can use any DTail commands, such as `dtail`, `dgrep`, `dmap`, `dcat`, `dtailhealth`, to do so. Checkout out all the usage examples on the official DTail page. + +I have installed DTail server this way on my personal OpenBSD frontends `blowfish`, and `fishfinger`, and the following command connects as user `rex` to both machines and greps the file `/etc/fstab` for the string `local`: + +``` +❯ ./dgrep -user rex -servers blowfish.buetow.org,fishfinger.buetow.org --regex local /etc/fstab +CLIENT|earth|WARN|Encountered unknown host|{blowfish.buetow.org:2222 0xc0000a00f0 0xc0000a61e0 [blowfish.buetow.org]:2222 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9ZnF/LAk14SgqCzk38yENVTNfqibcluMTuKx1u53cKSp2xwHWzy0Ni5smFPpJDIQQljQEJl14ZdXvhhjp1kKHxJ79ubqRtIXBlC0PhlnP8Kd+mVLLHYpH9VO4rnaSfHE1kBjWkI7U6lLc6ks4flgAgGTS5Bb7pLAjwdWg794GWcnRh6kSUEQd3SftANqQLgCunDcP2Vc4KR9R78zBmEzXH/OPzl/ANgNA6wWO2OoKKy2VrjwVAab6FW15h3Lr6rYIw3KztpG+UMmEj5ReexIjXi/jUptdnUFWspvAmzIl6kwzzF8ExVyT9D75JRuHvmxXKKjyJRxqb8UnSh2JD4JN [23.88.35.144]:2222 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9ZnF/LAk14SgqCzk38yENVTNfqibcluMTuKx1u53cKSp2xwHWzy0Ni5smFPpJDIQQljQEJl14ZdXvhhjp1kKHxJ79ubqRtIXBlC0PhlnP8Kd+mVLLHYpH9VO4rnaSfHE1kBjWkI7U6lLc6ks4flgAgGTS5Bb7pLAjwdWg794GWcnRh6kSUEQd3SftANqQLgCunDcP2Vc4KR9R78zBmEzXH/OPzl/ANgNA6wWO2OoKKy2VrjwVAab6FW15h3Lr6rYIw3KztpG+UMmEj5ReexIjXi/jUptdnUFWspvAmzIl6kwzzF8ExVyT9D75JRuHvmxXKKjyJRxqb8UnSh2JD4JN 0xc0000a2180} +CLIENT|earth|WARN|Encountered unknown host|{fishfinger.buetow.org:2222 0xc0000a0150 0xc000460110 [fishfinger.buetow.org]:2222 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDNiikdL7+tWSN0rCaw1tOd9aQgeUFgb830V9ejkyJ5h93PKLCWZSMMCtiabc1aUeUZR//rZjcPHFLuLq/YC+Y3naYtGd6j8qVrcfG8jy3gCbs4tV9SZ9qd5E24mtYqYdGlee6JN6kEWhJxFkEwPfNlG+YAr3KC8lvEAE2JdWvaZavqsqMvHZtAX3b25WCBf2HGkyLZ+d9cnimRUOt+/+353BQFCEct/2mhMVlkr4I23CY6Tsufx0vtxx25nbFdZias6wmhxaE9p3LiWXygPWGU5iZ4RSQSImQz4zyOc9rnJeP1rwGk0OWDJhdKNXuf0kIPdzMfwxv2otgY32/DJj6L [46.23.94.99]:2222 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDNiikdL7+tWSN0rCaw1tOd9aQgeUFgb830V9ejkyJ5h93PKLCWZSMMCtiabc1aUeUZR//rZjcPHFLuLq/YC+Y3naYtGd6j8qVrcfG8jy3gCbs4tV9SZ9qd5E24mtYqYdGlee6JN6kEWhJxFkEwPfNlG+YAr3KC8lvEAE2JdWvaZavqsqMvHZtAX3b25WCBf2HGkyLZ+d9cnimRUOt+/+353BQFCEct/2mhMVlkr4I23CY6Tsufx0vtxx25nbFdZias6wmhxaE9p3LiWXygPWGU5iZ4RSQSImQz4zyOc9rnJeP1rwGk0OWDJhdKNXuf0kIPdzMfwxv2otgY32/DJj6L 0xc0000a2240} +Encountered 2 unknown hosts: 'blowfish.buetow.org:2222,fishfinger.buetow.org:2222' +Do you want to trust these hosts?? (y=yes,a=all,n=no,d=details): a +CLIENT|earth|INFO|STATS:STATS|cgocalls=11|cpu=8|connected=2|servers=2|connected%=100|new=2|throttle=0|goroutines=19 +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 +``` + +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: + +``` +❯ ./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 +``` + +## Conclusions + +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! + +Check out the following for more information: + +=> https://dtail.dev +=> https://github.com/mimecast/dtail +=> https://www.rexify.org + +More related posts are: + +<< template::inline::index dtail + +E-Mail your comments to hi@paul.cyou :-) + +=> ../ Back to the main site diff --git a/gemfeed/atom.xml b/gemfeed/atom.xml index c723424a..830f5c56 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-23T10:59:21+02:00</updated> + <updated>2023-03-24T12:10:42+02:00</updated> <title>foo.zone feed</title> <subtitle>To be in the .zone!</subtitle> <link href="gemini://foo.zone/gemfeed/atom.xml" rel="self" /> @@ -770,6 +770,10 @@ REMOTE|fishfinger|100|7|fstab|093f510ec5c0f512.h /usr/local ffs rw,wxallowed,nod <a class="textlink" href="https://dtail.dev">https://dtail.dev</a><br /> <a class="textlink" href="https://github.com/mimecast/dtail">https://github.com/mimecast/dtail</a><br /> <a class="textlink" href="https://www.rexify.org">https://www.rexify.org</a><br /> +<p>More related posts are:</p> +<a class="textlink" href="https://foo.zone/gemfeed/2022-10-30-installing-dtail-on-openbsd.html">2022-10-30 Installing DTail on OpenBSD (You are currently reading this)</a><br /> +<a class="textlink" href="https://foo.zone/gemfeed/2022-03-06-the-release-of-dtail-4.0.0.html">2022-03-06 The release of DTail 4.0.0</a><br /> +<a class="textlink" href="https://foo.zone/gemfeed/2021-04-22-dtail-the-distributed-log-tail-program.html">2021-04-22 DTail - The distributed log tail program</a><br /> <p>E-Mail your comments to hi@paul.cyou :-)</p> <a class="textlink" href="../">Back to the main site</a><br /> </div> @@ -859,7 +863,7 @@ jgs (________\ \ <name>Paul Buetow</name> <email>hi@paul.cyou</email> </author> - <summary>I am proud to announce that I've released Gemtexter version `1.1.0`. What is Gemtexter? It's my static site generator written in GNU Bash:</summary> + <summary>I am proud to announce that I've released Gemtexter version `1.1.0`. What is Gemtexter? It's my static site generator written in GNU Bash.</summary> <content type="xhtml"> <div xmlns="http://www.w3.org/1999/xhtml"> <h1>Gemtexter 1.1.0 - Let's Gemtext again</h1> @@ -875,9 +879,7 @@ jgs (________\ \ |o=======.| jgs `"""""""""` </pre><br /> -<p>I am proud to announce that I've released Gemtexter version <span class="inlinecode">1.1.0</span>. What is Gemtexter? It's my static site generator written in GNU Bash:</p> -<a class="textlink" href="https://foo.zone/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html">Gemtexter - One Bash script to rule it all</a><br /> -<a class="textlink" href="https://codeberg.org/snonux/gemtexter">https://codeberg.org/snonux/gemtexter</a><br /> +<p>I am proud to announce that I've released Gemtexter version <span class="inlinecode">1.1.0</span>. What is Gemtexter? It's my static site generator written in GNU Bash.</p> <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> <h2>What's new?</h2> <h3>Automatic check for GNU version requirements</h3> @@ -922,6 +924,10 @@ check_dependencies () { <h3>More</h3> <p>Additionally, there were a couple of bug fixes, refactorings and overall improvements in the documentation made. </p> <p>Overall I think it's a pretty solid <span class="inlinecode">1.1.0</span> release without anything groundbreaking (therefore no major version jump). But I am happy about it.</p> +<p>More related posts are:</p> +<a class="textlink" href="https://foo.zone/gemfeed/2022-08-27-gemtexter-1.1.0-lets-gemtext-again.html">2022-08-27 Gemtexter 1.1.0 - Let's Gemtext again (You are currently reading this)</a><br /> +<a class="textlink" href="https://foo.zone/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html">2021-06-05 Gemtexter - One Bash script to rule it all</a><br /> +<a class="textlink" href="https://foo.zone/gemfeed/2021-04-24-welcome-to-the-geminispace.html">2021-04-24 Welcome to the Geminispace</a><br /> <p>E-Mail your comments to hi@paul.cyou :-)</p> <a class="textlink" href="../">Back to the main site</a><br /> </div> @@ -2004,7 +2010,7 @@ learn () { <name>Paul Buetow</name> <email>hi@paul.cyou</email> </author> - <summary>I have recently released DTail 4.0.0 and this blog post goes through all the new goodies. You can also read my previous post about DTail in case you wonder what DTail is:</summary> + <summary>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):</summary> <content type="xhtml"> <div xmlns="http://www.w3.org/1999/xhtml"> <h1>The release of DTail 4.0.0</h1> @@ -2022,9 +2028,7 @@ learn () { | | | | </pre><br /> -<p>I have recently released DTail 4.0.0 and this blog post goes through all the new goodies. You can also read my previous post about DTail in case you wonder what DTail is:</p> -<a class="textlink" href="https://foo.zone/gemfeed/2021-04-22-dtail-the-distributed-log-tail-program.html">DTail - The distributed log tail program</a><br /> -<p>If you want to jump directly to DTail, do it here (there are nice animated gifs which demonstrates the usage pretty well):</p> +<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> <h3>Rewritten logging</h3> @@ -2241,6 +2245,10 @@ exec /usr/local/bin/dtailhealth --server localhost:2222 </ul> <p>I use usually DTail at work, but I have recently installed it on my personal OpenBSD machines too. I might write a small tutorial here (and I might also add the rc scripts as examples to one of the next DTail releases).</p> <p>I am a bit busy at the moment with two other pet projects of mine (one internal work-project, and one personal one, the latter you will read about in the next couple of months). If you have ideas (or even a patch), then please don't hesitate to contact me (either via E-Mail or a request at GitHub).</p> +<p>More related posts are:</p> +<a class="textlink" href="https://foo.zone/gemfeed/2022-10-30-installing-dtail-on-openbsd.html">2022-10-30 Installing DTail on OpenBSD</a><br /> +<a class="textlink" href="https://foo.zone/gemfeed/2022-03-06-the-release-of-dtail-4.0.0.html">2022-03-06 The release of DTail 4.0.0 (You are currently reading this)</a><br /> +<a class="textlink" href="https://foo.zone/gemfeed/2021-04-22-dtail-the-distributed-log-tail-program.html">2021-04-22 DTail - The distributed log tail program</a><br /> <p>Thanks!</p> <p>Paul</p> <p>E-Mail your comments to hi@paul.cyou :-)</p> @@ -2489,8 +2497,8 @@ jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Art by Joan Stark, mod. by Paul Buetow </pre><br /> <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/2021-11-29-bash-golf-part-1.html">Bash Golf Part 1</a><br /> -<a class="textlink" href="https://foo.zone/gemfeed/2022-01-01-bash-golf-part-2.html">Bash Golf Part 2 (you are reading this atm.)</a><br /> +<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 /> <h2>Redirection</h2> <p>Let's have a closer look at Bash redirection. As you might already know that there are 3 standard file descriptors:</p> <ul> @@ -2871,6 +2879,11 @@ PAUL:X:1000:1000:PAUL BUETOW:/HOME/PAUL:/BIN/BASH ❯ echo $? 1 </pre><br /> +<p>More 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 /> +<a class="textlink" href="https://foo.zone/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html">2021-06-05 Gemtexter - One Bash script to rule it all</a><br /> +<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</a><br /> <p>E-Mail your comments to hi@paul.cyou :-)</p> <a class="textlink" href="../">Back to the main site</a><br /> </div> @@ -2995,8 +3008,8 @@ jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Art by Joan Stark </pre><br /> <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/2021-11-29-bash-golf-part-1.html">Bash Golf Part 1 (you are reading this atm.)</a><br /> -<a class="textlink" href="https://foo.zone/gemfeed/2022-01-01-bash-golf-part-2.html">Bash Golf Part 2</a><br /> +<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 /> <h2>TCP/IP networking</h2> <p>You probably know the Netcat tool, which is a swiss army knife for TCP/IP networking on the command line. But did you know that the Bash natively supports TCP/IP networking?</p> <p>Have a look here how that works:</p> @@ -3349,6 +3362,11 @@ bash: line 1: 1/10.0 : syntax error: invalid arithmetic operator (error token is .10 </pre><br /> <p>See you later for the next post of this series.</p> +<p>More 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</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 /> +<a class="textlink" href="https://foo.zone/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html">2021-06-05 Gemtexter - One Bash script to rule it all</a><br /> +<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</a><br /> <p>E-Mail your comments to hi@paul.cyou :-)</p> <a class="textlink" href="../">Back to the main site</a><br /> </div> @@ -3681,7 +3699,7 @@ Hello World <name>Paul Buetow</name> <email>hi@paul.cyou</email> </author> - <summary>You might have read my previous blog post about entering the Geminispace, where I pointed out the benefits of having and maintaining an internet presence there. This whole site (the blog and all other pages) is composed in the Gemtext markup language. </summary> + <summary>You might have read my previous blog posts about entering the Geminispace, where I pointed out the benefits of having and maintaining an internet presence there. This whole site (the blog and all other pages) is composed in the Gemtext markup language.</summary> <content type="xhtml"> <div xmlns="http://www.w3.org/1999/xhtml"> <h1>Gemtexter - One Bash script to rule it all</h1> @@ -3726,8 +3744,7 @@ Hello World \___.>`''-.||:.__,' SSt |_______`> <_____:::. . . \ _/ `+a:f:......jrei''' </pre><br /> -<p>You might have read my previous blog post about entering the Geminispace, where I pointed out the benefits of having and maintaining an internet presence there. This whole site (the blog and all other pages) is composed in the Gemtext markup language. </p> -<a class="textlink" href="https://foo.zone/gemfeed/2021-04-24-welcome-to-the-geminispace.html">Welcome to the Geminispace</a><br /> +<p>You might have read my previous blog posts about entering the Geminispace, where I pointed out the benefits of having and maintaining an internet presence there. This whole site (the blog and all other pages) is composed in the Gemtext markup language.</p> <p>This comes with the benefit that I can write content in my favourite text editor (Vim). </p> <h2>Motivation</h2> <p>Another benefit of using Gemini is that the Gemtext markup language is easy to parse. As my site is dual-hosted (Gemini+HTTP), I could, in theory, just write a shell script to deal with the conversion from Gemtext to HTML; there is no need for a full-featured programming language here. I have done a lot of Bash in the past, but I am also often revisiting old tools and techniques for refreshing and keeping the knowledge up to date here.</p> @@ -3807,6 +3824,13 @@ assert::equals "$(generate::make_link md "$gemtext")" \ <h2>Conclusion</h2> <p>It was quite a lot of fun writing Gemtexter. It's a relatively small project, but given that I worked on that in my spare time once in a while, it kept me busy for several weeks. </p> <p>I finally revamped my personal internet site and started to blog again. I wanted the result to be exactly how it is now: A slightly retro-inspired internet site built for fun with unconventional tools. </p> +<p>More related posts are:</p> +<a class="textlink" href="https://foo.zone/gemfeed/2022-08-27-gemtexter-1.1.0-lets-gemtext-again.html">2022-08-27 Gemtexter 1.1.0 - Let's Gemtext again</a><br /> +<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</a><br /> +<a class="textlink" href="https://foo.zone/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html">2021-06-05 Gemtexter - One Bash script to rule it all (You are currently reading this)</a><br /> +<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</a><br /> +<a class="textlink" href="https://foo.zone/gemfeed/2021-04-24-welcome-to-the-geminispace.html">2021-04-24 Welcome to the Geminispace</a><br /> <p>E-Mail your comments to hi@paul.cyou :-)</p> <a class="textlink" href="../">Back to the main site</a><br /> </div> @@ -4116,6 +4140,11 @@ fi <h2>Advanced Bash learning pro tip</h2> <p>I also highly recommend having a read through the "Advanced Bash-Scripting Guide" (not from Google). I use it as the universal Bash reference and learn something new every time I look at it.</p> <a class="textlink" href="https://tldp.org/LDP/abs/html/">Advanced Bash-Scripting Guide</a><br /> +<p>More 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</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 /> +<a class="textlink" href="https://foo.zone/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html">2021-06-05 Gemtexter - One Bash script to rule it all</a><br /> +<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 (You are currently reading this)</a><br /> <p>E-Mail your comments to hi@paul.cyou :-)</p> <a class="textlink" href="../">Back to the main site</a><br /> </div> @@ -4188,6 +4217,10 @@ fi <p>Check out one of the following links for more information about Gemini. For example, you will find a FAQ that explains why the protocol is named Gemini. Many Gemini capsules are dual-hosted via Gemini and HTTP(S) so that people new to Gemini can sneak peek at the content with a regular web browser. Some people go as far as tri-hosting all their content via HTTP(S), Gemini and Gopher.</p> <a class="textlink" href="gemini://gemini.circumlunar.space">gemini://gemini.circumlunar.space</a><br /> <a class="textlink" href="https://gemini.circumlunar.space">https://gemini.circumlunar.space</a><br /> +<p>More related posts are:</p> +<a class="textlink" href="https://foo.zone/gemfeed/2022-08-27-gemtexter-1.1.0-lets-gemtext-again.html">2022-08-27 Gemtexter 1.1.0 - Let's Gemtext again</a><br /> +<a class="textlink" href="https://foo.zone/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html">2021-06-05 Gemtexter - One Bash script to rule it all</a><br /> +<a class="textlink" href="https://foo.zone/gemfeed/2021-04-24-welcome-to-the-geminispace.html">2021-04-24 Welcome to the Geminispace (You are currently reading this)</a><br /> <p>E-Mail your comments to hi@paul.cyou :-)</p> <a class="textlink" href="../">Back to the main site</a><br /> </div> @@ -4270,6 +4303,10 @@ dtail –servers serverlist.txt –files ‘/var/log/*.log’ –regex ‘(?i:er <h2>Open Source</h2> <p>Mimecast highly encourages you to have a look at DTail and submit an issue for any features you would like to see. Have you found a bug? Maybe you just have a question or comment? If you want to go a step further: We would also love to see pull requests for any features or improvements. Either way, if in doubt just contact us via the DTail GitHub page.</p> <a class="textlink" href="https://dtail.dev">https://dtail.dev</a><br /> +<p>More related posts are:</p> +<a class="textlink" href="https://foo.zone/gemfeed/2022-10-30-installing-dtail-on-openbsd.html">2022-10-30 Installing DTail on OpenBSD</a><br /> +<a class="textlink" href="https://foo.zone/gemfeed/2022-03-06-the-release-of-dtail-4.0.0.html">2022-03-06 The release of DTail 4.0.0</a><br /> +<a class="textlink" href="https://foo.zone/gemfeed/2021-04-22-dtail-the-distributed-log-tail-program.html">2021-04-22 DTail - The distributed log tail program (You are currently reading this)</a><br /> <p>E-Mail your comments to hi@paul.cyou :-)</p> <a class="textlink" href="../">Back to the main site</a><br /> </div> @@ -1,6 +1,6 @@ # foo.zone -> This site was generated at 2023-03-23T11:01:35+02:00 by `Gemtexter` +> This site was generated at 2023-03-24T12:13:59+02:00 by `Gemtexter` ``` |\---/| diff --git a/uptime-stats.gmi b/uptime-stats.gmi index 21f14382..934f210a 100644 --- a/uptime-stats.gmi +++ b/uptime-stats.gmi @@ -1,6 +1,6 @@ # My machine uptime stats -> This site was last updated at 2023-03-23T11:01:35+02:00 +> This site was last updated at 2023-03-24T12:13:59+02:00 The following stats were collected via `uptimed` on all of my personal computers over many years and the output was generated by `guprecords`, the global uptime records stats analyser. |
