From ee75979b5d94ae18f930ff91e5b2d51cd554b60d Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 9 Mar 2026 22:45:54 +0200 Subject: Update content for html --- gemfeed/2023-12-10-bash-golf-part-3.html | 253 +++++++++++++++---------------- 1 file changed, 124 insertions(+), 129 deletions(-) (limited to 'gemfeed/2023-12-10-bash-golf-part-3.html') diff --git a/gemfeed/2023-12-10-bash-golf-part-3.html b/gemfeed/2023-12-10-bash-golf-part-3.html index db32cfae..0b848821 100644 --- a/gemfeed/2023-12-10-bash-golf-part-3.html +++ b/gemfeed/2023-12-10-bash-golf-part-3.html @@ -2,17 +2,12 @@ - Bash Golf Part 3 -
-
-
-

Home | Markdown | Gemini

@@ -61,24 +56,24 @@ jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
#!/usr/bin/env bash
+
#!/usr/bin/env bash
 
-log () {
-    local -r level="$1"; shift
-    local -r message="$1"; shift
-    local -i pid="$$"
+log () {
+    local -r level="$1"; shift
+    local -r message="$1"; shift
+    local -i pid="$$"
 
-    local -r callee=${FUNCNAME[1]}
-    local -r stamp=$(date +%Y%m%d-%H%M%S)
+    local -r callee=${FUNCNAME[1]}
+    local -r stamp=$(date +%Y%m%d-%H%M%S)
 
-    echo "$level|$stamp|$pid|$callee|$message" >&2
-}
+    echo "$level|$stamp|$pid|$callee|$message" >&2
+}
 
-at_home_friday_evening () {
-    log INFO 'One Peperoni Pizza, please'
-}
+at_home_friday_evening () {
+    log INFO 'One Peperoni Pizza, please'
+}
 
-at_home_friday_evening
+at_home_friday_evening
 

The output is as follows:
@@ -87,8 +82,8 @@ http://www.gnu.org/software/src-highlite --> by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
./logexample.sh
-INFO|20231210-082732|123002|at_home_friday_evening|One Peperoni Pizza, please
+
❯ ./logexample.sh
+INFO|20231210-082732|123002|at_home_friday_evening|One Peperoni Pizza, please
 

:(){ :|:& };:


@@ -122,18 +117,18 @@ http://www.gnu.org/software/src-highlite --> by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
#!/usr/bin/env bash
-
-outer() {
-  inner() {
-    echo 'Intel inside!'
-  }
-  inner
-}
-
-inner
-outer
-inner
+
#!/usr/bin/env bash
+
+outer() {
+  inner() {
+    echo 'Intel inside!'
+  }
+  inner
+}
+
+inner
+outer
+inner
 

And let's execute it:
@@ -151,26 +146,26 @@ Intel inside! by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
#!/usr/bin/env bash
-
-outer1() {
-  inner() {
-    echo 'Intel inside!'
-  }
-  inner
-}
-
-outer2() {
-  inner() {
-    echo 'Wintel inside!'
-  }
-  inner
-}
-
-outer1
-inner
-outer2
-inner
+
#!/usr/bin/env bash
+
+outer1() {
+  inner() {
+    echo 'Intel inside!'
+  }
+  inner
+}
+
+outer2() {
+  inner() {
+    echo 'Wintel inside!'
+  }
+  inner
+}
+
+outer1
+inner
+outer2
+inner
 

And let's run it:
@@ -191,14 +186,14 @@ Wintel inside! by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
#!/usr/bin/env bash
+
#!/usr/bin/env bash
 
-some_expensive_operations() {
-  echo "Doing expensive operations with '$1' from pid $$"
-}
+some_expensive_operations() {
+  echo "Doing expensive operations with '$1' from pid $$"
+}
 
-for i in {0..9}; do echo $i; done \
-  | xargs -P10 -I{} bash -c 'some_expensive_operations "{}"'
+for i in {0..9}; do echo $i; done \
+  | xargs -P10 -I{} bash -c 'some_expensive_operations "{}"'
 

We try here to run ten parallel processes; each of them should run the some_expensive_operations function with a different argument. The arguments are provided to xargs through STDIN one per line. When executed, we get this:
@@ -223,15 +218,15 @@ bash: line 1: some_expensive_operations: command not found by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
#!/usr/bin/env bash
+
#!/usr/bin/env bash
 
-some_expensive_operations() {
-  echo "Doing expensive operations with '$1' from pid $$"
-}
-export -f some_expensive_operations
+some_expensive_operations() {
+  echo "Doing expensive operations with '$1' from pid $$"
+}
+export -f some_expensive_operations
 
-for i in {0..9}; do echo $i; done \
-  | xargs -P10 -I{} bash -c 'some_expensive_operations "{}"'
+for i in {0..9}; do echo $i; done \
+  | xargs -P10 -I{} bash -c 'some_expensive_operations "{}"'
 

When we run this now, we get:
@@ -256,19 +251,19 @@ Doing expensive operations with '9' from pid 132840 by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
#!/usr/bin/env bash
+
#!/usr/bin/env bash
 
-some_other_function() {
-  echo "$1"
-}
+some_other_function() {
+  echo "$1"
+}
 
-some_expensive_operations() {
-  some_other_function "Doing expensive operations with '$1' from pid $$"
-}
-export -f some_expensive_operations
+some_expensive_operations() {
+  some_other_function "Doing expensive operations with '$1' from pid $$"
+}
+export -f some_expensive_operations
 
-for i in {0..9}; do echo $i; done \
-  | xargs -P10 -I{} bash -c 'some_expensive_operations "{}"'
+for i in {0..9}; do echo $i; done \
+  | xargs -P10 -I{} bash -c 'some_expensive_operations "{}"'
 

... because some_other_function isn't exported! You will also need to add an export -f some_other_function!
@@ -281,22 +276,22 @@ http://www.gnu.org/software/src-highlite --> by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
#!/usr/bin/env bash
-
-foo() {
-  local foo=bar # Declare local/dynamic variable
-  bar
-  echo "$foo"
-}
-
-bar() {
-  echo "$foo"
-  foo=baz
-}
-
-foo=foo # Declare global variable
-foo # Call function foo
-echo "$foo"
+
#!/usr/bin/env bash
+
+foo() {
+  local foo=bar # Declare local/dynamic variable
+  bar
+  echo "$foo"
+}
+
+bar() {
+  echo "$foo"
+  foo=baz
+}
+
+foo=foo # Declare global variable
+foo # Call function foo
+echo "$foo"
 

Let's pause a minute. What do you think the output would be?
@@ -321,34 +316,34 @@ foo by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
#!/usr/bin/env bash
+
#!/usr/bin/env bash
 
-declare -r foo=foo
-declare -r bar=bar
+declare -r foo=foo
+declare -r bar=bar
 
-if [ "$foo" = foo ]; then
-  if [ "$bar" = bar ]; then
-    echo ok1
-  fi
-fi
+if [ "$foo" = foo ]; then
+  if [ "$bar" = bar ]; then
+    echo ok1
+  fi
+fi
 
-if [ "$foo" = foo ] && [ "$bar" == bar ]; then
-  echo ok2a
-fi
+if [ "$foo" = foo ] && [ "$bar" == bar ]; then
+  echo ok2a
+fi
 
-[ "$foo" = foo ] && [ "$bar" == bar ] && echo ok2b
+[ "$foo" = foo ] && [ "$bar" == bar ] && echo ok2b
 
-if [[ "$foo" = foo && "$bar" == bar ]]; then
-  echo ok3a
-fi
+if [[ "$foo" = foo && "$bar" == bar ]]; then
+  echo ok3a
+fi
 
- [[ "$foo" = foo && "$bar" == bar ]] && echo ok3b
+ [[ "$foo" = foo && "$bar" == bar ]] && echo ok3b
 
-if test "$foo" = foo && test "$bar" = bar; then
-  echo ok4a
-fi
+if test "$foo" = foo && test "$bar" = bar; then
+  echo ok4a
+fi
 
-test "$foo" = foo && test "$bar" = bar && echo ok4b
+test "$foo" = foo && test "$bar" = bar && echo ok4b
 

The output we get is:
@@ -372,18 +367,18 @@ ok4b by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
#!/usr/bin/env bash
+
#!/usr/bin/env bash
 
-# Single line comment
+# Single line comment
 
-# These are two single line
-# comments one after another
+# These are two single line
+# comments one after another
 
-: <<COMMENT
-This is another way a
-multi line comment
-could be written!
-COMMENT
+: <<COMMENT
+This is another way a
+multi line comment
+could be written!
+COMMENT
 

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!
@@ -396,11 +391,11 @@ http://www.gnu.org/software/src-highlite --> by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
#!/usr/bin/env bash
+
#!/usr/bin/env bash
 
-echo foo
-echo echo baz >> $0
-echo bar
+echo foo
+echo echo baz >> $0
+echo bar
 

When it is run, it will do:
@@ -434,11 +429,11 @@ echo baz
Back to the main site
-- cgit v1.2.3