From d4ee6684b7d6c8c8e5ff96f6998755c42465ec22 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 18 May 2024 13:24:42 +0300 Subject: Update content for html --- gemfeed/2023-12-10-bash-golf-part-3.html | 238 +++++++++++++++---------------- 1 file changed, 119 insertions(+), 119 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 021faf6f..5baa4fb8 100644 --- a/gemfeed/2023-12-10-bash-golf-part-3.html +++ b/gemfeed/2023-12-10-bash-golf-part-3.html @@ -39,24 +39,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:
@@ -65,8 +65,8 @@ at_home_friday_evening 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
 

:(){ :|:& };:


@@ -100,18 +100,18 @@ INFO|20231210- -
#!/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:
@@ -129,26 +129,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:
@@ -169,14 +169,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:
@@ -201,15 +201,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:
@@ -234,19 +234,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!
@@ -259,22 +259,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?
@@ -299,34 +299,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:
@@ -350,18 +350,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!
@@ -374,11 +374,11 @@ COMMENT 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:
-- cgit v1.2.3