diff options
| author | Paul Buetow <paul@buetow.org> | 2023-12-10 11:38:30 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-12-10 11:38:30 +0200 |
| commit | 6db60eef3dd9d0db9ae094d3043523a8236147b1 (patch) | |
| tree | 44dfef68018bf416865a434b1762fdbf81ab86fa /gemfeed/2023-12-10-bash-golf-part-3.html | |
| parent | 625f513e15548a9bbf60134ff6e8b350a44ec90b (diff) | |
Update content for html
Diffstat (limited to 'gemfeed/2023-12-10-bash-golf-part-3.html')
| -rw-r--r-- | gemfeed/2023-12-10-bash-golf-part-3.html | 420 |
1 files changed, 420 insertions, 0 deletions
diff --git a/gemfeed/2023-12-10-bash-golf-part-3.html b/gemfeed/2023-12-10-bash-golf-part-3.html new file mode 100644 index 00000000..daa426b9 --- /dev/null +++ b/gemfeed/2023-12-10-bash-golf-part-3.html @@ -0,0 +1,420 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> +<title>Bash Golf Part 3</title> +<link rel="shortcut icon" type="image/gif" href="/favicon.ico" /> +<link rel="stylesheet" href="../style.css" /> +<link rel="stylesheet" href="style-override.css" /> +</head> +<body> +<h1 style='display: inline'>Bash Golf Part 3</h1><br /> +<br /> +<span class='quote'>Published at 2023-12-10T11:35:54+02:00</span><br /> +<br /> +<pre> + '\ '\ '\ . . |>18>> + \ \ \ . ' . | + O>> O>> O>> . 'o | + \ .\. .. .\. .. . | + /\ . /\ . /\ . . | + / / . / / .'. / / .' . | +jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + Art by Joan Stark, mod. by Paul Buetow +</pre> +<br /> +<span>This is the third blog post about my Bash Golf series. This series is random Bash tips, tricks, and weirdnesses I have encountered over time. </span><br /> +<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='./2022-01-01-bash-golf-part-2.html'>2022-01-01 Bash Golf Part 2</a><br /> +<a class='textlink' href='./2023-12-10-bash-golf-part-3.html'>2023-12-10 Bash Golf Part 3 (You are currently reading this)</a><br /> +<br /> +<h2 style='display: inline'><span class='inlinecode'>FUNCNAME</span></h2><br /> +<br /> +<span><span class='inlinecode'>FUNCNAME</span> is an array you are looking for a way to dynamically determine the name of the current function (which could be considered the callee in the context of its own execution), you can use the special variable <span class='inlinecode'>FUNCNAME</span>. This is an array variable that contains the names of all shell functions currently in the execution call stack. The element <span class='inlinecode'>FUNCNAME[0]</span> holds the name of the currently executing function, <span class='inlinecode'>FUNCNAME[1]</span> the name of the function that called that, and so on.</span><br /> +<br /> +<span>This is particularly useful for logging when you want to include the callee function in the log output. E.g. look at this log helper:</span><br /> +<br /> +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="#9A1900">#!/usr/bin/env bash</font></i> + +<b><font color="#000000">log ()</font></b> { + <b><font color="#0000FF">local</font></b> -r <font color="#009900">level</font><font color="#990000">=</font><font color="#FF0000">"$1"</font><font color="#990000">;</font> <b><font color="#0000FF">shift</font></b> + <b><font color="#0000FF">local</font></b> -r <font color="#009900">message</font><font color="#990000">=</font><font color="#FF0000">"$1"</font><font color="#990000">;</font> <b><font color="#0000FF">shift</font></b> + <b><font color="#0000FF">local</font></b> -i <font color="#009900">pid</font><font color="#990000">=</font><font color="#FF0000">"$$"</font> + + <b><font color="#0000FF">local</font></b> -r <font color="#009900">callee</font><font color="#990000">=</font><font color="#009900">${FUNCNAME[1]}</font> + <b><font color="#0000FF">local</font></b> -r <font color="#009900">stamp</font><font color="#990000">=</font><font color="#009900">$(</font>date <font color="#990000">+%</font>Y<font color="#990000">%</font>m<font color="#990000">%</font>d-<font color="#990000">%</font>H<font color="#990000">%</font>M<font color="#990000">%</font>S<font color="#990000">)</font> + + echo <font color="#FF0000">"$level|$stamp|$pid|$callee|$message"</font> <font color="#990000">>&</font><font color="#993399">2</font> +} + +<b><font color="#000000">at_home_friday_evening ()</font></b> { + log INFO <font color="#FF0000">'One Peperoni Pizza, please'</font> +} + +at_home_friday_evening +</pre> +<br /> +<span>The output is as follows:</span><br /> +<br /> +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre>❯ <font color="#990000">.</font>/logexample<font color="#990000">.</font>sh +INFO<font color="#990000">|</font><font color="#993399">20231210</font>-<font color="#993399">082732</font><font color="#990000">|</font><font color="#993399">123002</font><font color="#990000">|</font>at_home_friday_evening<font color="#990000">|</font>One Peperoni Pizza<font color="#990000">,</font> please +</pre> +<br /> +<h2 style='display: inline'><span class='inlinecode'>:(){ :|:& };:</span></h2><br /> +<br /> +<span>This one may be widely known already, but I am including it here as I found a cute image illustrating it. But to break <span class='inlinecode'>:(){ :|:& };:</span> down:</span><br /> +<br /> +<ul> +<li><span class='inlinecode'>:(){ }</span> is really a declaration of the function <span class='inlinecode'>:</span></li> +<li>The <span class='inlinecode'>;</span> is ending the current statement</li> +<li>The <span class='inlinecode'>:</span> at the end is calling the function <span class='inlinecode'>:</span></li> +<li><span class='inlinecode'>:|:&</span> is the function body</li> +</ul><br /> +<span>Let's break down the function body <span class='inlinecode'>:|:&</span>: </span><br /> +<br /> +<ul> +<li>The first <span class='inlinecode'>:</span> is calling the function recursively</li> +<li>The <span class='inlinecode'>|:</span> is piping the output to the function <span class='inlinecode'>:</span> again (parallel recursion)</li> +<li>The <span class='inlinecode'>&</span> lets it run in the background.</li> +</ul><br /> +<span>So, it's a fork bomb. If you run it, your computer will run out of resources eventually. (Modern Linux distributions could have reasonable limits configured for your login session, so it won't bring down your whole system anymore unless you run it as <span class='inlinecode'>root</span>!)</span><br /> +<br /> +<span>And here is the cute illustration:</span><br /> +<br /> +<a href='./2023-12-10-bash-golf-part-3/bash-fork-bomb.jpg'><img alt='Bash fork bomb' title='Bash fork bomb' src='./2023-12-10-bash-golf-part-3/bash-fork-bomb.jpg' /></a><br /> +<br /> +<h2 style='display: inline'>Inner functions</h2><br /> +<br /> +<span>Bash defines variables as it is interpreting the code. The same applies to function declarations. Let's consider this code:</span><br /> +<br /> +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="#9A1900">#!/usr/bin/env bash</font></i> + +<b><font color="#000000">outer()</font></b> { + <b><font color="#000000">inner()</font></b> { + echo <font color="#FF0000">'Intel inside!'</font> + } + inner +} + +inner +outer +inner +</pre> +<br /> +<span>And let's execute it:</span><br /> +<br /> +<pre> +❯ ./inner.sh +/tmp/inner.sh: line 10: inner: command not found +Intel inside! +Intel inside! +</pre> +<br /> +<span>What happened? The first time <span class='inlinecode'>inner</span> was called, it wasn't defined yet. That only happens after the <span class='inlinecode'>outer</span> run. Note that <span class='inlinecode'>inner</span> will still be globally defined. But functions can be declared multiple times (the last version wins):</span><br /> +<br /> +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="#9A1900">#!/usr/bin/env bash</font></i> + +<b><font color="#000000">outer1()</font></b> { + <b><font color="#000000">inner()</font></b> { + echo <font color="#FF0000">'Intel inside!'</font> + } + inner +} + +<b><font color="#000000">outer2()</font></b> { + <b><font color="#000000">inner()</font></b> { + echo <font color="#FF0000">'Wintel inside!'</font> + } + inner +} + +outer1 +inner +outer2 +inner +</pre> +<br /> +<span>And let's run it:</span><br /> +<br /> +<pre> +❯ ./inner2.sh +Intel inside! +Intel inside! +Wintel inside! +Wintel inside! +</pre> +<br /> +<h2 style='display: inline'>Exporting functions</h2><br /> +<br /> +<span>Have you ever wondered how to execute a shell function in parallel through <span class='inlinecode'>xargs</span>? The problem is that this won't work:</span><br /> +<br /> +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="#9A1900">#!/usr/bin/env bash</font></i> + +<b><font color="#000000">some_expensive_operations()</font></b> { + echo <font color="#FF0000">"Doing expensive operations with '$1' from pid $$"</font> +} + +<b><font color="#0000FF">for</font></b> i <b><font color="#0000FF">in</font></b> {<font color="#993399">0</font><font color="#990000">..</font><font color="#993399">9</font>}<font color="#990000">;</font> <b><font color="#0000FF">do</font></b> echo <font color="#009900">$i</font><font color="#990000">;</font> <b><font color="#0000FF">done</font></b> <font color="#990000">\</font> + <font color="#990000">|</font> xargs -P<font color="#993399">10</font> -I{} bash -c <font color="#FF0000">'some_expensive_operations "{}"'</font> +</pre> +<br /> +<span>We try here to run ten parallel processes; each of them should run the <span class='inlinecode'>some_expensive_operations</span> function with a different argument. The arguments are provided to <span class='inlinecode'>xargs</span> through <span class='inlinecode'>STDIN</span> one per line. When executed, we get this:</span><br /> +<br /> +<pre> +❯ ./xargs.sh +bash: line 1: some_expensive_operations: command not found +bash: line 1: some_expensive_operations: command not found +bash: line 1: some_expensive_operations: command not found +bash: line 1: some_expensive_operations: command not found +bash: line 1: some_expensive_operations: command not found +bash: line 1: some_expensive_operations: command not found +bash: line 1: some_expensive_operations: command not found +bash: line 1: some_expensive_operations: command not found +bash: line 1: some_expensive_operations: command not found +bash: line 1: some_expensive_operations: command not found +</pre> +<br /> +<span>There's an easy solution for this. Just export the function! It will then be magically available in any sub-shell!</span><br /> +<br /> +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="#9A1900">#!/usr/bin/env bash</font></i> + +<b><font color="#000000">some_expensive_operations()</font></b> { + echo <font color="#FF0000">"Doing expensive operations with '$1' from pid $$"</font> +} +<b><font color="#0000FF">export</font></b> -f some_expensive_operations + +<b><font color="#0000FF">for</font></b> i <b><font color="#0000FF">in</font></b> {<font color="#993399">0</font><font color="#990000">..</font><font color="#993399">9</font>}<font color="#990000">;</font> <b><font color="#0000FF">do</font></b> echo <font color="#009900">$i</font><font color="#990000">;</font> <b><font color="#0000FF">done</font></b> <font color="#990000">\</font> + <font color="#990000">|</font> xargs -P<font color="#993399">10</font> -I{} bash -c <font color="#FF0000">'some_expensive_operations "{}"'</font> +</pre> +<br /> +<span>When we run this now, we get:</span><br /> +<br /> +<pre> +❯ ./xargs.sh +Doing expensive operations with '0' from pid 132831 +Doing expensive operations with '1' from pid 132832 +Doing expensive operations with '2' from pid 132833 +Doing expensive operations with '3' from pid 132834 +Doing expensive operations with '4' from pid 132835 +Doing expensive operations with '5' from pid 132836 +Doing expensive operations with '6' from pid 132837 +Doing expensive operations with '7' from pid 132838 +Doing expensive operations with '8' from pid 132839 +Doing expensive operations with '9' from pid 132840 +</pre> +<br /> +<span>If <span class='inlinecode'>some_expensive_function</span> would call another function, the other function must also be exported. Otherwise, there will be a runtime error again. E.g., this won't work:</span><br /> +<br /> +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="#9A1900">#!/usr/bin/env bash</font></i> + +<b><font color="#000000">some_other_function()</font></b> { + echo <font color="#FF0000">"$1"</font> +} + +<b><font color="#000000">some_expensive_operations()</font></b> { + some_other_function <font color="#FF0000">"Doing expensive operations with '$1' from pid $$"</font> +} +<b><font color="#0000FF">export</font></b> -f some_expensive_operations + +<b><font color="#0000FF">for</font></b> i <b><font color="#0000FF">in</font></b> {<font color="#993399">0</font><font color="#990000">..</font><font color="#993399">9</font>}<font color="#990000">;</font> <b><font color="#0000FF">do</font></b> echo <font color="#009900">$i</font><font color="#990000">;</font> <b><font color="#0000FF">done</font></b> <font color="#990000">\</font> + <font color="#990000">|</font> xargs -P<font color="#993399">10</font> -I{} bash -c <font color="#FF0000">'some_expensive_operations "{}"'</font> +</pre> +<br /> +<span>... because <span class='inlinecode'>some_other_function</span> isn't exported! You will also need to add an <span class='inlinecode'>export -f some_other_function</span>!</span><br /> +<br /> +<h2 style='display: inline'>Dynamic variables with <span class='inlinecode'>local</span></h2><br /> +<br /> +<span>You may know that <span class='inlinecode'>local</span> is how to declare local variables in a function. Most don't know that those variables actually have dynamic scope. Let's consider the following example:</span><br /> +<br /> +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="#9A1900">#!/usr/bin/env bash</font></i> + +<b><font color="#000000">foo()</font></b> { + <b><font color="#0000FF">local</font></b> <font color="#009900">foo</font><font color="#990000">=</font>bar <i><font color="#9A1900"># Declare local/dynamic variable</font></i> + bar + echo <font color="#FF0000">"$foo"</font> +} + +<b><font color="#000000">bar()</font></b> { + echo <font color="#FF0000">"$foo"</font> + <font color="#009900">foo</font><font color="#990000">=</font>baz +} + +<font color="#009900">foo</font><font color="#990000">=</font>foo <i><font color="#9A1900"># Declare global variable</font></i> +foo <i><font color="#9A1900"># Call function foo</font></i> +echo <font color="#FF0000">"$foo"</font> +</pre> +<br /> +<span>Let's pause a minute. What do you think the output would be?</span><br /> +<br /> +<span>Let's run it:</span><br /> +<br /> +<pre> +❯ ./dynamic.sh +bar +baz +foo +</pre> +<br /> +<span>What happened? The variable <span class='inlinecode'>foo</span> (declared with <span class='inlinecode'>local</span>) is available in the function it was declared in and in all other functions down the call stack! We can even modify the value of <span class='inlinecode'>foo', and the change will be visible up the call stack. It's not a global variable; on the last line, </span>echo "$foo"` echoes the global variable content.</span><br /> +<br /> +<br /> +<h2 style='display: inline'><span class='inlinecode'>if</span> conditionals</h2><br /> +<br /> +<span>Consider all variants here more or less equivalent:</span><br /> +<br /> +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="#9A1900">#!/usr/bin/env bash</font></i> + +<b><font color="#0000FF">declare</font></b> -r <font color="#009900">foo</font><font color="#990000">=</font>foo +<b><font color="#0000FF">declare</font></b> -r <font color="#009900">bar</font><font color="#990000">=</font>bar + +<b><font color="#0000FF">if</font></b> <font color="#990000">[</font> <font color="#FF0000">"$foo"</font> <font color="#990000">=</font> foo <font color="#990000">];</font> <b><font color="#0000FF">then</font></b> + <b><font color="#0000FF">if</font></b> <font color="#990000">[</font> <font color="#FF0000">"$bar"</font> <font color="#990000">=</font> bar <font color="#990000">];</font> <b><font color="#0000FF">then</font></b> + echo ok1 + <b><font color="#0000FF">fi</font></b> +<b><font color="#0000FF">fi</font></b> + +<b><font color="#0000FF">if</font></b> <font color="#990000">[</font> <font color="#FF0000">"$foo"</font> <font color="#990000">=</font> foo <font color="#990000">]</font> <font color="#990000">&&</font> <font color="#990000">[</font> <font color="#FF0000">"$bar"</font> <font color="#990000">==</font> bar <font color="#990000">];</font> <b><font color="#0000FF">then</font></b> + echo ok2a +<b><font color="#0000FF">fi</font></b> + +<font color="#990000">[</font> <font color="#FF0000">"$foo"</font> <font color="#990000">=</font> foo <font color="#990000">]</font> <font color="#990000">&&</font> <font color="#990000">[</font> <font color="#FF0000">"$bar"</font> <font color="#990000">==</font> bar <font color="#990000">]</font> <font color="#990000">&&</font> echo ok2b + +<b><font color="#0000FF">if</font></b> <font color="#990000">[[</font> <font color="#FF0000">"$foo"</font> <font color="#990000">=</font> foo <font color="#990000">&&</font> <font color="#FF0000">"$bar"</font> <font color="#990000">==</font> bar <font color="#990000">]];</font> <b><font color="#0000FF">then</font></b> + echo ok3a +<b><font color="#0000FF">fi</font></b> + + <font color="#990000">[[</font> <font color="#FF0000">"$foo"</font> <font color="#990000">=</font> foo <font color="#990000">&&</font> <font color="#FF0000">"$bar"</font> <font color="#990000">==</font> bar <font color="#990000">]]</font> <font color="#990000">&&</font> echo ok3b + +<b><font color="#0000FF">if</font></b> <b><font color="#0000FF">test</font></b> <font color="#FF0000">"$foo"</font> <font color="#990000">=</font> foo <font color="#990000">&&</font> <b><font color="#0000FF">test</font></b> <font color="#FF0000">"$bar"</font> <font color="#990000">=</font> bar<font color="#990000">;</font> <b><font color="#0000FF">then</font></b> + echo ok4a +<b><font color="#0000FF">fi</font></b> + +<b><font color="#0000FF">test</font></b> <font color="#FF0000">"$foo"</font> <font color="#990000">=</font> foo <font color="#990000">&&</font> <b><font color="#0000FF">test</font></b> <font color="#FF0000">"$bar"</font> <font color="#990000">=</font> bar <font color="#990000">&&</font> echo ok4b +</pre> +<br /> +<span>The output we get is:</span><br /> +<br /> +<pre> +❯ ./if.sh +ok1 +ok2a +ok2b +ok3a +ok3b +ok4a +ok4b +</pre> +<br /> +<h2 style='display: inline'>Multi-line comments</h2><br /> +<br /> +<span>You all know how to comment. Put a <span class='inlinecode'>#</span> in front of it. You could use multiple single-line comments or abuse heredocs and redirect it to the <span class='inlinecode'>:</span> no-op command to emulate multi-line comments. </span><br /> +<br /> +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="#9A1900">#!/usr/bin/env bash</font></i> + +<i><font color="#9A1900"># Single line comment</font></i> + +<i><font color="#9A1900"># These are two single line</font></i> +<i><font color="#9A1900"># comments one after another</font></i> + +<font color="#990000">:</font> <font color="#990000"><<</font>COMMENT +This is another way a +multi line comment +could be written<font color="#990000">!</font> +COMMENT +</pre> +<br /> +<span>I will not demonstrate the execution of this script, as it won't print anything! It's obviously not the most pretty way of commenting on your code, but it could sometimes be handy!</span><br /> +<br /> +<h2 style='display: inline'>Don't change it while it's executed</h2><br /> +<br /> +<span>Consider this script:</span><br /> +<br /> +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="#9A1900">#!/usr/bin/env bash</font></i> + +echo foo +echo echo baz <font color="#990000">>></font> <font color="#009900">$0</font> +echo bar +</pre> +<br /> +<span>When it is run, it will do:</span><br /> +<br /> +<pre> +❯ ./if.sh +foo +bar +baz +❯ cat if.sh +#!/usr/bin/env bash + +echo foo +echo echo baz >> $0 +echo bar +echo baz +</pre> +<br /> +<span>So what happened? The <span class='inlinecode'>echo baz</span> line was appended to the script while it was still executed! And the interpreter also picked it up! It tells us that Bash evaluates each line as it encounters it. This can lead to nasty side effects when editing the script while it is still being executed! You should always keep this in mind!</span><br /> +<br /> +<br /> +<span>Other related posts are:</span><br /> +<br /> +<a class='textlink' href='./2021-05-16-personal-bash-coding-style-guide.html'>2021-05-16 Personal Bash coding style guide</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-11-29-bash-golf-part-1.html'>2021-11-29 Bash Golf Part 1</a><br /> +<a class='textlink' href='./2022-01-01-bash-golf-part-2.html'>2022-01-01 Bash Golf Part 2</a><br /> +<a class='textlink' href='./2023-12-10-bash-golf-part-3.html'>2023-12-10 Bash Golf Part 3 (You are currently reading this)</a><br /> +<br /> +<span>E-Mail your comments to <span class='inlinecode'>paul@nospam.buetow.org</span> :-)</span><br /> +<br /> +<a class='textlink' href='../'>Back to the main site</a><br /> +<p class="footer"> +Generated by <a href="https://codeberg.org/snonux/gemtexter">Gemtexter 2.1.0-release</a> | +served by <a href="https://www.OpenBSD.org">OpenBSD</a>/<a href="https://man.openbsd.org/httpd.8">httpd(8)</a> | +<a href="https://www.foo.zone/site-mirrors.html">Site Mirrors</a> +</p> +</body> +</html> |
