new blog entrance
This commit is contained in:
parent
350b827ea4
commit
8f636dd2b3
|
@ -0,0 +1,85 @@
|
|||
<!doctype html>
|
||||
{% extends "template.html" %}
|
||||
{% block head %}
|
||||
{{ super() }}
|
||||
{% endblock %}
|
||||
{% block content %}
|
||||
{{ _date.date() }}
|
||||
<p><h1>The Joy of One-Liners</h1>
|
||||
There is an engineering idiom which withstands time like no other. As old as the hills but never forgotten. In informational technology relations, one could say it is ancient. Its dawn proclaimed from the shores of Seattle, defeated by an apocalyptic horseman with the melodious name <i>NT</i>. Shot! Point blank into the chest. Buried under the hills of an IT dark age which we call the nineties, dead. But is it? Well, not exactly. There is still life in the old dog. Like Beatrix Kiddo, it one-inch punches itself back to daylight. Coal black as a miner. Topsoil sticks to its beige 1978’s leather jacket, it trickles to the ground with every step taken to the nearest diner. The door opens again. “May I have a glass of water, please?”
|
||||
</p>
|
||||
<p>
|
||||
An art passed down from the ancestors. Its most visible interface: <i>The Shell</i>. Its name: <i>The Unix Philosophy</i>.
|
||||
<p>
|
||||
Coined by Doug McIlroy in the <a href="https://archive.org/details/bstj57-6-1899/mode/2up">Bell System Technical Journal</a> as a foreword to the UNIX Time-Sharing System. There is an even more concise version<a href=#references" style="text-decoration:none">[1]</a> and according to <a href="http://www.catb.org/~esr/writings/taoup/html/ch01s06.html">Eric Steven Raymond</a> this is <a href="#references" style="text-decoration:none">[2]</a>:
|
||||
<blockquote>“This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.”<br>— Doug McIlroy —</br></blockquote>
|
||||
</p>
|
||||
<p>
|
||||
In some way, this is similar to the concept of the on vogue philosophy of microservices everywhere. Services are written to do one thing and do it well. They work together. Also, text is the connecting link between programs, services, and humans. The possibility to read a program’s output as text seems benign, as long as everything is working well. The advantage is interactivity at any time, testability of every component, and debuggability at every step of the process in an on the fly manner. Impromptu, out of reflex like a punch.
|
||||
</p>
|
||||
<h2>Pipe Dreams</h2>
|
||||
<hr/>
|
||||
Mc Ilroy made another very important contribution, which is the Unix pipe. It is the practical implementation of connecting the programs inside the shell. He saw the programs as tools, plugged together via pipes building a <a href="http://www.princeton.edu/~hos/Mahoney/expotape.htm">toolbox</a>. Program A’s <code>stdout</code> is put into the <code>stdin</code> of program B. This leaves us with a single line of chained, well-defined programs to solve a problem that would otherwise be an extensive, multi-line program in any programming language of your choice.
|
||||
</p>
|
||||
Let’s say we’ve got two programs. The first is one is <i>fortune</i>, the fortune cookie program from BSD games, which outputs random quotes as text. The second one is <i>cowsay</i>, which displays various kinds of animals as ASCII art. In order to work in a coherent manner a <code>|</code> is used. The pipe connects the first one to the second one.
|
||||
<p>
|
||||
<pre>
|
||||
<code>
|
||||
❯ fortune | cowsay
|
||||
_________________________________________
|
||||
/ Gil-galad was an Elven-king. Of him the \
|
||||
| harpers sadly sing: the last whose |
|
||||
| realm was fair and free between the |
|
||||
| Mountains and the Sea. |
|
||||
| |
|
||||
| His sword was long, his lance was keen, |
|
||||
| his shining helm afar was seen; the |
|
||||
| countless stars of heaven's field were |
|
||||
| mirrored in his silver shield. |
|
||||
| |
|
||||
| But long ago he rode away, and where he |
|
||||
| dwelleth none can say; for into |
|
||||
| darkness fell his star in Mordor where |
|
||||
| the shadows are. |
|
||||
| |
|
||||
\ -- J. R. R. Tolkien /
|
||||
-----------------------------------------
|
||||
\ ^__^
|
||||
\ (oo)\_______
|
||||
(__)\ )\/\
|
||||
||----w |
|
||||
|| ||
|
||||
</code>
|
||||
</pre>
|
||||
<p>
|
||||
To mix in some colors pipe everything through lolcat. Try it out!
|
||||
</p>
|
||||
<pre>
|
||||
<code>
|
||||
❯ fortune | cowsay | lolcat
|
||||
</code>
|
||||
</pre>
|
||||
<p>
|
||||
So far so good, let us gain some real-world practice.
|
||||
</p>
|
||||
<h2>Punching Lines — Retire Your Password Generator</h2>
|
||||
<hr/>
|
||||
Have you ever questioned the integrity of your one-click, GUI, proprietary password generator? Is its randomness really as unpredictable as it proclaims? Are you the only one which ends up knowing your generated password? There is a one-liner that ups your opsec instantly. Right from the source of randomness:
|
||||
<pre>
|
||||
<code>
|
||||
❯ head -n 42 /dev/urandom | tr -cd '[:alnum:]' | cut -c-12
|
||||
</code>
|
||||
</pre>
|
||||
So, what is going on here? Everything is a file, that is the way *nix systems see it, even the device which puts out random data. To read the upper part of a file we use <code>head</code>. It will be sent to <code>stdout</code> and displayed via the shell. Parameter <code>-n</code> reads exactly 42 lines of the file given to gather enough randomness. The output of <code>head</code> will be sent to <code>tr</code>. In order to select only alphanumeric characters for our password, we use <code>'[:alnum:]'</code>. Parameters <code>-c</code> selects the complement of alphanumeric characters and <code>-d</code> deletes them. <code>cut</code> does exactly what it says, <code>-c-12</code> cuts after 12 characters with leaves us with a random password of length 12. Every time you execute this one-liner a freshly made random password is returned.
|
||||
<p>
|
||||
To extend the pool of possible symbols the manual of <code>tr</code> lists various sets of filtered symbols. In the case of a password generator, further sensible filters are <code>'[:alpha:]'</code> or <code>'[:digit:]'</code>. While <code>'[:graph:]'</code> is also viable in this case it returns one of the greater sets of symbols. Use it with caution, especially if you create passwords for database users. These might be terminated prematurely through <code>`</code> or <code>'</code> inside the returned string.
|
||||
</p>
|
||||
<h2>Sunday Punch</h2>
|
||||
<hr/>
|
||||
This blog entrance can be seen as a prelude to a series of one-liners I use time and time again. The are multiple in the pipeline. One-liners should save you the time you otherwise would spend coding a complex solution. There is little better than solving a challenging problem in a single line.
|
||||
<p id="references" class="references">
|
||||
<h2>References</h2>
|
||||
<a href="https://www.bookfinder.com/?isbn=9780201547771">[1] A Quarter Century of Unix, 1994, Peter Salus, ISBN:9780201547771</a><br>
|
||||
<a href="https://www.bookfinder.com/?isbn=9780131429017">[2] The Art of UNIX Programming, 2003, Eric Raymond, ISBN:9780131429017</a><br>
|
||||
</p>
|
||||
{% endblock %}
|
Loading…
Reference in New Issue