summaryrefslogtreecommitdiff
path: root/gemfeed/2022-01-01-bash-golf-part-2.html
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-04-07 00:49:19 +0300
committerPaul Buetow <paul@buetow.org>2023-04-07 00:49:19 +0300
commit30fc4e8f74315a92aaec36dfb9a8d4efa0a21791 (patch)
tree97ef2b26fd54a366d72f5289ba11ea311f8a0173 /gemfeed/2022-01-01-bash-golf-part-2.html
parent5c9e3886de0f9041e4a6fb6fea9917e29a8fbd76 (diff)
exact style
Diffstat (limited to 'gemfeed/2022-01-01-bash-golf-part-2.html')
-rw-r--r--gemfeed/2022-01-01-bash-golf-part-2.html221
1 files changed, 156 insertions, 65 deletions
diff --git a/gemfeed/2022-01-01-bash-golf-part-2.html b/gemfeed/2022-01-01-bash-golf-part-2.html
index 88d04c2f..bbf12b21 100644
--- a/gemfeed/2022-01-01-bash-golf-part-2.html
+++ b/gemfeed/2022-01-01-bash-golf-part-2.html
@@ -8,8 +8,10 @@
<link rel="stylesheet" href="style-override.css" />
</head>
<body>
-<h1>Bash Golf Part 2</h1>
-<p class="quote"><i>Published at 2022-01-01T23:36:15+00:00; Updated at 2022-01-05</i></p>
+<h1 style='display: inline'>Bash Golf Part 2</h1><br />
+<br />
+<span class=quote>Published at 2022-01-01T23:36:15+00:00; Updated at 2022-01-05</span><br />
+<br />
<pre>
'\ '\ . . |&gt;18&gt;&gt;
@@ -21,17 +23,23 @@
jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Art by Joan Stark, mod. by Paul Buetow
</pre>
-<p>This is the second blog post about my Bash Golf series. This series is random Bash tips, tricks and weirdnesses I came across. It's a collection of smaller articles I wrote in an older (in German language) blog, which I translated and refreshed with some new content.</p>
-<a class="textlink" href="./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="./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>
+<br />
+<span>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.</span><br />
+<br />
+<a class=textlink href='./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='./2021-11-29-bash-golf-part-1.html'>2021-11-29 Bash Golf Part 1</a><br />
+<br />
+<h2 style='display: inline'>Redirection</h2><br />
+<br />
+<span>Let's have a closer look at Bash redirection. As you might already know that there are 3 standard file descriptors:</span><br />
+<br />
<ul>
<li>0 aka stdin (standard input)</li>
<li>1 aka stdout (standard output)</li>
<li>2 aka stderr (standard error output)</li>
-</ul>
-<p>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):</p>
+</ul><br />
+<span>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):</span><br />
+<br />
<pre>
❯ ls -l /proc/self/fd/
total 0
@@ -40,32 +48,44 @@ lrwx------. 1 paul paul 64 Nov 23 09:46 1 -&gt; /dev/pts/9
lrwx------. 1 paul paul 64 Nov 23 09:46 2 -&gt; /dev/pts/9
lr-x------. 1 paul paul 64 Nov 23 09:46 3 -&gt; /proc/162912/fd
</pre>
-<p>The following examples demonstrate two different ways to accomplish the same thing. The difference is that the first command is directly printing out "Foo" to stdout and the second command is explicitly redirecting stdout to its own stdout file descriptor:</p>
+<br />
+<span>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:</span><br />
+<br />
<pre>
❯ echo Foo
Foo
❯ echo Foo &gt; /proc/self/fd/0
Foo
</pre>
-<p>Other useful redirections are:</p>
+<br />
+<span>Other useful redirections are:</span><br />
+<br />
<ul>
<li>Redirect stderr to stdin: "echo foo 2&gt;&amp;1"</li>
<li>Redirect stdin to stderr: "echo foo &gt;&amp;2"</li>
-</ul>
-<p>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:</p>
+</ul><br />
+<span>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:</span><br />
+<br />
<pre>
❯ echo Foo 1&gt;&amp;2 2&gt;/dev/null
Foo
</pre>
-<p class="quote"><i>Update: A reader sent me an email and pointed out that the order of the redirections is important. </i></p>
-<p>As you can see, the following will not print out anything:</p>
+<br />
+<span class=quote>Update: A reader sent me an email and pointed out that the order of the redirections is important. </span><br />
+<br />
+<span>As you can see, the following will not print out anything:</span><br />
+<br />
<pre>
❯ echo Foo 2&gt;/dev/null 1&gt;&amp;2
</pre>
-<p>A good description (also pointed out by the reader) can be found here:</p>
-<a class="textlink" href="https://wiki.bash-hackers.org/howto/redirection_tutorial#order_of_redirection_ie_file_2_1_vs_2_1_file">Order of redirection</a><br />
-<p>Ok, back to the original blog post. You can also use grouping here (neither of these commands will print out anything to stdout):</p>
+<br />
+<span>A good description (also pointed out by the reader) can be found here:</span><br />
+<br />
+<a class=textlink href='https://wiki.bash-hackers.org/howto/redirection_tutorial#order_of_redirection_ie_file_2_1_vs_2_1_file'>Order of redirection</a><br />
+<br />
+<span>Ok, back to the original blog post. You can also use grouping here (neither of these commands will print out anything to stdout):</span><br />
+<br />
<pre>
❯ { echo Foo 1&gt;&amp;2; } 2&gt;/dev/null
❯ ( echo Foo 1&gt;&amp;2; ) 2&gt;/dev/null
@@ -73,7 +93,9 @@ Foo
❯ ( ( ( echo Foo 1&gt;&amp;2; ) 2&gt;&amp;1; ) 1&gt;&amp;2; ) 2&gt;/dev/null
</pre>
-<p>A handy way to list all open file descriptors is to use the "lsof" command (that's not a Bash built-in), whereas $$ is the process id (pid) of the current shell process:</p>
+<br />
+<span>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:</span><br />
+<br />
<pre>
❯ lsof -a -p $$ -d0,1,2
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
@@ -81,7 +103,9 @@ bash 62676 paul 0u CHR 136,9 0t0 12 /dev/pts/9
bash 62676 paul 1u CHR 136,9 0t0 12 /dev/pts/9
bash 62676 paul 2u CHR 136,9 0t0 12 /dev/pts/9
</pre>
-<p>Let's create our own descriptor "3" for redirection to a file named "foo":</p>
+<br />
+<span>Let's create our own descriptor "3" for redirection to a file named "foo":</span><br />
+<br />
<pre>
❯ touch foo
❯ exec 3&gt;foo # This opens fd 3 and binds it to file foo.
@@ -96,7 +120,9 @@ Bratwurst
❯ echo Steak &gt;&amp;3
-bash: 3: Bad file descriptor
</pre>
-<p>You can also override the default file descriptors, as the following example script demonstrates:</p>
+<br />
+<span>You can also override the default file descriptors, as the following example script demonstrates:</span><br />
+<br />
<pre>
❯ cat grandmaster.sh
#!/usr/bin/env bash
@@ -123,15 +149,20 @@ echo Second line: $LINE2
# Restore default stdin and delete fd 6
exec 0&lt;&amp;6 6&lt;&amp;-
</pre>
-<p>Let's execute it:</p>
+<br />
+<span>Let's execute it:</span><br />
+<br />
<pre>
❯ chmod 750 ./grandmaster.sh
❯ ./grandmaster.sh
First line: Learn You a Haskell
Second line: for Great Good
</pre>
-<h2>HERE</h2>
-<p>I have mentioned HERE-documents and HERE-strings already in this post. Let's do some more examples. The following "cat" receives a multi line string from stdin. In this case, the input multi line string is a HERE-document. As you can see, it also interpolates variables (in this case the output of "date" running in a subshell).</p>
+<br />
+<h2 style='display: inline'>HERE</h2><br />
+<br />
+<span>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).</span><br />
+<br />
<pre>
❯ cat &lt;&lt;END
&gt; Hello World
@@ -140,7 +171,9 @@ Second line: for Great Good
Hello World
It's Fri 26 Nov 08:46:52 GMT 2021
</pre>
-<p>You can also write it the following way, but that's less readable (it's good for an obfuscation contest):</p>
+<br />
+<span>You can also write it the following way, but that's less readable (it's good for an obfuscation contest):</span><br />
+<br />
<pre>
❯ &lt;&lt;END cat
&gt; Hello Universe
@@ -149,7 +182,9 @@ It's Fri 26 Nov 08:46:52 GMT 2021
Hello Universe
It's Fri 26 Nov 08:47:32 GMT 2021
</pre>
-<p>Besides of an HERE-document, there is also a so-called HERE-string. Besides of...</p>
+<br />
+<span>Besides of an HERE-document, there is also a so-called HERE-string. Besides of...</span><br />
+<br />
<pre>
❯ declare VAR=foo
❯ if echo "$VAR" | grep -q foo; then
@@ -157,24 +192,32 @@ It's Fri 26 Nov 08:47:32 GMT 2021
&gt; fi
$VAR ontains foo
</pre>
-<p>...you can use a HERE-string like that:</p>
+<br />
+<span>...you can use a HERE-string like that:</span><br />
+<br />
<pre>
❯ if grep -q foo &lt;&lt;&lt; "$VAR"; then
&gt; echo '$VAR contains foo'
&gt; fi
$VAR contains foo
</pre>
-<p>Or even shorter, you can do:</p>
+<br />
+<span>Or even shorter, you can do:</span><br />
+<br />
<pre>
❯ grep -q foo &lt;&lt;&lt; "$VAR" &amp;&amp; echo '$VAR contains foo'
$VAR contains foo
</pre>
-<p>You can also use a Bash regex to accomplish the same thing, but the points of the examples so far were to demonstrate HERE-{documents,strings} and not Bash regular expressions:</p>
+<br />
+<span>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:</span><br />
+<br />
<pre>
❯ if [[ "$VAR" =~ foo ]]; then echo yay; fi
yay
</pre>
-<p>You can also use it with "read":</p>
+<br />
+<span>You can also use it with "read":</span><br />
+<br />
<pre>
❯ read a &lt;&lt;&lt; ja
❯ echo $a
@@ -189,15 +232,20 @@ Learn
❯ echo ${words[3]}
Golang
</pre>
-<p>The following is good for an obfuscation contest too:</p>
+<br />
+<span>The following is good for an obfuscation contest too:</span><br />
+<br />
<pre>
❯ echo 'I like Perl too' &gt; perllove.txt
❯ cat - perllove.txt &lt;&lt;&lt; "$dumdidumstring"
Learn you a Golang for Great Good
I like Perl too
</pre>
-<h2>RANDOM</h2>
-<p>Random is a special built-in variable containing a different pseudo random number each time it's used.</p>
+<br />
+<h2 style='display: inline'>RANDOM</h2><br />
+<br />
+<span>Random is a special built-in variable containing a different pseudo random number each time it's used.</span><br />
+<br />
<pre>
❯ echo $RANDOM
11811
@@ -206,8 +254,11 @@ I like Perl too
❯ echo $RANDOM
9104
</pre>
-<p>That's very useful if you want to randomly delay the execution of your scripts when you run it on many servers concurrently, just to spread the server load (which might be caused by the script run) better.</p>
-<p>Let's say you want to introduce a random delay of 1 minute. You can accomplish it with:</p>
+<br />
+<span>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.</span><br />
+<br />
+<span>Let's say you want to introduce a random delay of 1 minute. You can accomplish it with:</span><br />
+<br />
<pre>
❯ cat ./calc_answer_to_ultimate_question_in_life.sh
#!/usr/bin/env bash
@@ -235,10 +286,15 @@ main
Delaying script execution for 42 seconds...
Continuing script execution...
</pre>
-<h2>set -x and set -e and pipefile</h2>
-<p>In my opinion, -x and -e and pipefile are the most useful Bash options. Let's have a look at them one after another.</p>
-<h3>-x</h3>
-<p>-x prints commands and their arguments as they are executed. This helps to develop and debug your Bash code:</p>
+<br />
+<h2 style='display: inline'>set -x and set -e and pipefile</h2><br />
+<br />
+<span>In my opinion, -x and -e and pipefile are the most useful Bash options. Let's have a look at them one after another.</span><br />
+<br />
+<h3 style='display: inline'>-x</h3><br />
+<br />
+<span>-x prints commands and their arguments as they are executed. This helps to develop and debug your Bash code:</span><br />
+<br />
<pre>
❯ set -x
❯ square () { local -i num=$1; echo $((num*num)); }
@@ -250,11 +306,15 @@ Continuing script execution...
+ echo 'Square of 11 is 121'
Square of 11 is 121
</pre>
-<p>You can also set it when calling an external script without modifying the script itself:</p>
+<br />
+<span>You can also set it when calling an external script without modifying the script itself:</span><br />
+<br />
<pre>
❯ bash -x ./half_broken_script_to_be_debugged.sh
</pre>
-<p>Let's do that on one of the example scripts we covered earlier:</p>
+<br />
+<span>Let's do that on one of the example scripts we covered earlier:</span><br />
+<br />
<pre>
❯ bash -x ./grandmaster.sh
+ bash -x ./grandmaster.sh
@@ -272,21 +332,29 @@ Second line: for Great Good
+ exec
</pre>
-<h3>-e</h3>
-<p>This is a very important option you want to use when you are paranoid. This means, you should always "set -e" in your scripts when you need to make absolutely sure that your script runs successfully (with that I mean that no command should exit with an unexpected status code).</p>
-<p>Ok, let's dig deeper:</p>
+<br />
+<h3 style='display: inline'>-e</h3><br />
+<br />
+<span>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).</span><br />
+<br />
+<span>Ok, let's dig deeper:</span><br />
+<br />
<pre>
❯ help set | grep -- -e
-e Exit immediately if a command exits with a non-zero status.
</pre>
-<p>As you can see in the following example, the Bash terminates after the execution of "grep" as "foo" is not matching "bar". Therefore, grep exits with 1 (unsuccessfully) and the shell aborts. And therefore, "bar" will not be printed out anymore:</p>
+<br />
+<span>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:</span><br />
+<br />
<pre>
❯ bash -c 'set -e; echo hello; grep -q bar &lt;&lt;&lt; foo; echo bar'
hello
❯ echo $?
1
</pre>
-<p>Whereas the outcome changes when the regex matches:</p>
+<br />
+<span>Whereas the outcome changes when the regex matches:</span><br />
+<br />
<pre>
❯ bash -c 'set -e; echo hello; grep -q bar &lt;&lt;&lt; barman; echo bar'
hello
@@ -294,7 +362,9 @@ bar
❯ echo $?
0
</pre>
-<p>So does it mean that grep will always make the shell terminate whenever its exit code isn't 0? This will render "set -e" quite unusable. Frankly, there are other commands where an exit status other than 0 should not terminate the whole script abruptly. Usually, what you want is to branch your code based on the outcome (exit code) of a command:</p>
+<br />
+<span>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:</span><br />
+<br />
<pre>
❯ bash -c 'set -e
&gt; grep -q bar &lt;&lt;&lt; foo
@@ -306,8 +376,11 @@ bar
❯ echo $?
1
</pre>
-<p>...but the example above won't reach any of the branches and won't print out anything, as the script terminates right after grep.</p>
-<p>The proper solution is to use grep as an expression in a conditional (e.g. in an if-else statement):</p>
+<br />
+<span>...but the example above won't reach any of the branches and won't print out anything, as the script terminates right after grep.</span><br />
+<br />
+<span>The proper solution is to use grep as an expression in a conditional (e.g. in an if-else statement):</span><br />
+<br />
<pre>
❯ bash -c 'set -e
&gt; if grep -q bar &lt;&lt;&lt; foo; then
@@ -328,7 +401,9 @@ matching
❯ echo $?
0
</pre>
-<p>You can also temporally undo "set -e" if there is no other way:</p>
+<br />
+<span>You can also temporally undo "set -e" if there is no other way:</span><br />
+<br />
<pre>
❯ cat ./e.sh
#!/usr/bin/env bash
@@ -370,25 +445,35 @@ Hello World
Hello Universe
Hello You!
</pre>
-<p>Why does calling "foo" with no arguments make the script terminate? Because as no argument was given, the "shift" won't have anything to do as the argument list $@ is empty, and therefore "shift" fails with a non-zero status.</p>
-<p>Why would you want to use "shift" after function-local variable assignments? Have a look at my personal Bash coding style guide for an explanation :-):</p>
-<a class="textlink" href="./2021-05-16-personal-bash-coding-style-guide.html">./2021-05-16-personal-bash-coding-style-guide.html</a><br />
-<h3>pipefail</h3>
-<p>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:</p>
+<br />
+<span>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.</span><br />
+<br />
+<span>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 :-):</span><br />
+<br />
+<a class=textlink href='./2021-05-16-personal-bash-coding-style-guide.html'>./2021-05-16-personal-bash-coding-style-guide.html</a><br />
+<br />
+<h3 style='display: inline'>pipefail</h3><br />
+<br />
+<span>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:</span><br />
+<br />
<pre>
❯ 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
</pre>
-<p>The following greps for paul in passwd and converts all lowercase letters to uppercase letters. The exit code of the pipe is 0, as the last command of the pipe (converting from lowercase to uppercase) succeeded:</p>
+<br />
+<span>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:</span><br />
+<br />
<pre>
❯ grep paul /etc/passwd | tr '[a-z]' '[A-Z]'
PAUL:X:1000:1000:PAUL BUETOW:/HOME/PAUL:/BIN/BASH
❯ echo $?
0
</pre>
-<p>Let's look at another example, where "TheRock" doesn't exist in the passwd file. However, the pipes exit status is still 0 (success). This is so because the last command ("tr" in this case) still succeeded. It is just that it didn't get any input on stdin to process:</p>
+<br />
+<span>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:</span><br />
+<br />
<pre>
❯ grep TheRock /etc/passwd
❯ echo $?
@@ -397,20 +482,26 @@ PAUL:X:1000:1000:PAUL BUETOW:/HOME/PAUL:/BIN/BASH
❯ echo $?
0
</pre>
-<p>To change this behaviour, pipefile can be used. Now, the pipes exit status is 1 (fail), because the pipe contains at least one command (in this case grep) which exited with status 1:</p>
+<br />
+<span>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:</span><br />
+<br />
<pre>
❯ set -o pipefail
❯ grep TheRock /etc/passwd | tr '[a-z]' '[A-Z]'
❯ echo $?
1
</pre>
-<p>Other related posts are:</p>
-<a class="textlink" href="./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="./2021-11-29-bash-golf-part-1.html">2021-11-29 Bash Golf Part 1</a><br />
-<a class="textlink" href="./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="./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 />
+<br />
+<span>Other related posts are:</span><br />
+<br />
+<a class=textlink href='./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='./2021-11-29-bash-golf-part-1.html'>2021-11-29 Bash Golf Part 1</a><br />
+<a class=textlink href='./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='./2021-05-16-personal-bash-coding-style-guide.html'>2021-05-16 Personal Bash coding style guide</a><br />
+<br />
+<span>E-Mail your comments to hi@paul.cyou :-)</span><br />
+<br />
+<a class=textlink href='../'>Back to the main site</a><br />
<p class="footer">
Generated with <a href="https://codeberg.org/snonux/gemtexter">Gemtexter</a> |
served by <a href="https://www.OpenBSD.org">OpenBSD</a>/<a href="https://man.openbsd.org/httpd.8">httpd(8)</a> |