From 6db60eef3dd9d0db9ae094d3043523a8236147b1 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 10 Dec 2023 11:38:30 +0200 Subject: Update content for html --- ...021-05-16-personal-bash-coding-style-guide.html | 1 + ...5-gemtexter-one-bash-script-to-rule-it-all.html | 1 + gemfeed/2021-11-29-bash-golf-part-1.html | 2 + gemfeed/2022-01-01-bash-golf-part-2.html | 2 + ...static-web-photo-albums-with-photoalbum.sh.html | 1 + gemfeed/2023-12-10-bash-golf-part-3.html | 420 ++++++++++++++++ .../2023-12-10-bash-golf-part-3/bash-fork-bomb.jpg | Bin 0 -> 209399 bytes gemfeed/atom.xml | 560 ++++++++++++++++----- gemfeed/index.html | 1 + 9 files changed, 854 insertions(+), 134 deletions(-) create mode 100644 gemfeed/2023-12-10-bash-golf-part-3.html create mode 100644 gemfeed/2023-12-10-bash-golf-part-3/bash-fork-bomb.jpg (limited to 'gemfeed') diff --git a/gemfeed/2021-05-16-personal-bash-coding-style-guide.html b/gemfeed/2021-05-16-personal-bash-coding-style-guide.html index 72c24941..9a43b775 100644 --- a/gemfeed/2021-05-16-personal-bash-coding-style-guide.html +++ b/gemfeed/2021-05-16-personal-bash-coding-style-guide.html @@ -396,6 +396,7 @@ fi 2021-06-05 Gemtexter - One Bash script to rule it all
2021-11-29 Bash Golf Part 1
2022-01-01 Bash Golf Part 2
+2023-12-10 Bash Golf Part 3

E-Mail your comments to paul@nospam.buetow.org :-)

diff --git a/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html b/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html index 62bfcceb..53bc7d7d 100644 --- a/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html +++ b/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html @@ -194,6 +194,7 @@ assert::equals "$(generate::m 2022-08-27 Gemtexter 1.1.0 - Let's Gemtext again
2023-03-25 Gemtexter 2.0.0 - Let's Gemtext again²
2023-07-21 Gemtexter 2.1.0 - Let's Gemtext again³
+2023-12-10 Bash Golf Part 3

E-Mail your comments to paul@nospam.buetow.org :-)

diff --git a/gemfeed/2021-11-29-bash-golf-part-1.html b/gemfeed/2021-11-29-bash-golf-part-1.html index 4bccd556..fb9b0260 100644 --- a/gemfeed/2021-11-29-bash-golf-part-1.html +++ b/gemfeed/2021-11-29-bash-golf-part-1.html @@ -27,6 +27,7 @@ jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2021-11-29 Bash Golf Part 1 (You are currently reading this)
2022-01-01 Bash Golf Part 2
+2023-12-10 Bash Golf Part 3

TCP/IP networking



@@ -479,6 +480,7 @@ bash: line 1: 1/10.0 : syntax error: invalid arithmetic operator (error token is 2021-06-05 Gemtexter - One Bash script to rule it all
2021-11-29 Bash Golf Part 1 (You are currently reading this)
2022-01-01 Bash Golf Part 2
+2023-12-10 Bash Golf Part 3

E-Mail your comments to paul@nospam.buetow.org :-)

diff --git a/gemfeed/2022-01-01-bash-golf-part-2.html b/gemfeed/2022-01-01-bash-golf-part-2.html index 0e24262b..4b1f9cb8 100644 --- a/gemfeed/2022-01-01-bash-golf-part-2.html +++ b/gemfeed/2022-01-01-bash-golf-part-2.html @@ -27,6 +27,7 @@ jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2021-11-29 Bash Golf Part 1
2022-01-01 Bash Golf Part 2 (You are currently reading this)
+2023-12-10 Bash Golf Part 3

Redirection



@@ -499,6 +500,7 @@ PAUL:X:1000:1000:PAUL BUETOW:/HOME/PAUL:/BIN/BASH 2021-06-05 Gemtexter - One Bash script to rule it all
2021-11-29 Bash Golf Part 1
2022-01-01 Bash Golf Part 2 (You are currently reading this)
+2023-12-10 Bash Golf Part 3

E-Mail your comments to paul@nospam.buetow.org :-)

diff --git a/gemfeed/2023-10-29-kiss-static-web-photo-albums-with-photoalbum.sh.html b/gemfeed/2023-10-29-kiss-static-web-photo-albums-with-photoalbum.sh.html index 8e64a105..19bdee49 100644 --- a/gemfeed/2023-10-29-kiss-static-web-photo-albums-with-photoalbum.sh.html +++ b/gemfeed/2023-10-29-kiss-static-web-photo-albums-with-photoalbum.sh.html @@ -285,6 +285,7 @@ blurs html index.html photos thumbs 2022-01-01 Bash Golf Part 2
2023-06-01 KISS server monitoring with Gogios
2023-10-29 KISS static web photo albums with photoalbum.sh (You are currently reading this)
+2023-12-10 Bash Golf Part 3

E-Mail your comments to paul@nospam.buetow.org :-)

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 @@ + + + + +Bash Golf Part 3 + + + + + +

Bash Golf Part 3


+
+Published at 2023-12-10T11:35:54+02:00
+
+
+    '\       '\        '\                   .  .          |>18>>
+      \        \         \              .         ' .     |
+     O>>      O>>       O>>         .                 'o  |
+      \       .\. ..    .\. ..   .                        |
+      /\    .  /\     .  /\    . .                        |
+     / /   .  / /  .'.  / /  .'    .                      |
+jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+                        Art by Joan Stark, mod. by Paul Buetow
+
+
+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.
+
+2021-11-29 Bash Golf Part 1
+2022-01-01 Bash Golf Part 2
+2023-12-10 Bash Golf Part 3 (You are currently reading this)
+
+

FUNCNAME


+
+FUNCNAME 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 FUNCNAME. This is an array variable that contains the names of all shell functions currently in the execution call stack. The element FUNCNAME[0] holds the name of the currently executing function, FUNCNAME[1] the name of the function that called that, and so on.
+
+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:
+
+ +
#!/usr/bin/env bash
+
+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)
+
+    echo "$level|$stamp|$pid|$callee|$message" >&2
+}
+
+at_home_friday_evening () {
+    log INFO 'One Peperoni Pizza, please'
+}
+
+at_home_friday_evening
+
+
+The output is as follows:
+
+ +
./logexample.sh
+INFO|20231210-082732|123002|at_home_friday_evening|One Peperoni Pizza, please
+
+
+

:(){ :|:& };:


+
+This one may be widely known already, but I am including it here as I found a cute image illustrating it. But to break :(){ :|:& };: down:
+
+
+Let's break down the function body :|:&:
+
+
+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 root!)
+
+And here is the cute illustration:
+
+Bash fork bomb
+
+

Inner functions


+
+Bash defines variables as it is interpreting the code. The same applies to function declarations. Let's consider this code:
+
+ +
#!/usr/bin/env bash
+
+outer() {
+  inner() {
+    echo 'Intel inside!'
+  }
+  inner
+}
+
+inner
+outer
+inner
+
+
+And let's execute it:
+
+
+❯ ./inner.sh
+/tmp/inner.sh: line 10: inner: command not found
+Intel inside!
+Intel inside!
+
+
+What happened? The first time inner was called, it wasn't defined yet. That only happens after the outer run. Note that inner will still be globally defined. But functions can be declared multiple times (the last version wins):
+
+ +
#!/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:
+
+
+❯ ./inner2.sh
+Intel inside!
+Intel inside!
+Wintel inside!
+Wintel inside!
+
+
+

Exporting functions


+
+Have you ever wondered how to execute a shell function in parallel through xargs? The problem is that this won't work:
+
+ +
#!/usr/bin/env bash
+
+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 "{}"'
+
+
+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:
+
+
+❯ ./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
+
+
+There's an easy solution for this. Just export the function! It will then be magically available in any sub-shell!
+
+ +
#!/usr/bin/env bash
+
+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 "{}"'
+
+
+When we run this now, we get:
+
+
+❯ ./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
+
+
+If some_expensive_function 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:
+
+ +
#!/usr/bin/env bash
+
+some_other_function() {
+  echo "$1"
+}
+
+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 "{}"'
+
+
+... because some_other_function isn't exported! You will also need to add an export -f some_other_function!
+
+

Dynamic variables with local


+
+You may know that local 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:
+
+ +
#!/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?
+
+Let's run it:
+
+
+❯ ./dynamic.sh
+bar
+baz
+foo
+
+
+What happened? The variable foo (declared with local) 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 foo', and the change will be visible up the call stack. It's not a global variable; on the last line, echo "$foo"` echoes the global variable content.
+
+
+

if conditionals


+
+Consider all variants here more or less equivalent:
+
+ +
#!/usr/bin/env bash
+
+declare -r foo=foo
+declare -r bar=bar
+
+if [ "$foo" = foo ]; then
+  if [ "$bar" = bar ]; then
+    echo ok1
+  fi
+fi
+
+if [ "$foo" = foo ] && [ "$bar" == bar ]; then
+  echo ok2a
+fi
+
+[ "$foo" = foo ] && [ "$bar" == bar ] && echo ok2b
+
+if [[ "$foo" = foo && "$bar" == bar ]]; then
+  echo ok3a
+fi
+
+ [[ "$foo" = foo && "$bar" == bar ]] && echo ok3b
+
+if test "$foo" = foo && test "$bar" = bar; then
+  echo ok4a
+fi
+
+test "$foo" = foo && test "$bar" = bar && echo ok4b
+
+
+The output we get is:
+
+
+❯ ./if.sh
+ok1
+ok2a
+ok2b
+ok3a
+ok3b
+ok4a
+ok4b
+
+
+

Multi-line comments


+
+You all know how to comment. Put a # in front of it. You could use multiple single-line comments or abuse heredocs and redirect it to the : no-op command to emulate multi-line comments.
+
+ +
#!/usr/bin/env bash
+
+# Single line comment
+
+# These are two single line
+# comments one after another
+
+: <<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!
+
+

Don't change it while it's executed


+
+Consider this script:
+
+ +
#!/usr/bin/env bash
+
+echo foo
+echo echo baz >> $0
+echo bar
+
+
+When it is run, it will do:
+
+
+❯ ./if.sh
+foo
+bar
+baz
+❯ cat if.sh
+#!/usr/bin/env bash
+
+echo foo
+echo echo baz >> $0
+echo bar
+echo baz
+
+
+So what happened? The echo baz 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!
+
+
+Other related posts are:
+
+2021-05-16 Personal Bash coding style guide
+2021-06-05 Gemtexter - One Bash script to rule it all
+2021-11-29 Bash Golf Part 1
+2022-01-01 Bash Golf Part 2
+2023-12-10 Bash Golf Part 3 (You are currently reading this)
+
+E-Mail your comments to paul@nospam.buetow.org :-)
+
+Back to the main site
+ + + diff --git a/gemfeed/2023-12-10-bash-golf-part-3/bash-fork-bomb.jpg b/gemfeed/2023-12-10-bash-golf-part-3/bash-fork-bomb.jpg new file mode 100644 index 00000000..6967c03a Binary files /dev/null and b/gemfeed/2023-12-10-bash-golf-part-3/bash-fork-bomb.jpg differ diff --git a/gemfeed/atom.xml b/gemfeed/atom.xml index 5418661c..67abc077 100644 --- a/gemfeed/atom.xml +++ b/gemfeed/atom.xml @@ -1,11 +1,429 @@ - 2023-12-10T07:56:06+02:00 + 2023-12-10T11:37:46+02:00 foo.zone feed To be in the .zone! https://foo.zone/ + + Bash Golf Part 3 + + https://foo.zone/gemfeed/2023-12-10-bash-golf-part-3.html + 2023-12-10T11:35:54+02:00 + + Paul Buetow aka snonux + paul@dev.buetow.org + + 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. + +
+

Bash Golf Part 3


+
+Published at 2023-12-10T11:35:54+02:00
+
+
+    '\       '\        '\                   .  .          |>18>>
+      \        \         \              .         ' .     |
+     O>>      O>>       O>>         .                 'o  |
+      \       .\. ..    .\. ..   .                        |
+      /\    .  /\     .  /\    . .                        |
+     / /   .  / /  .'.  / /  .'    .                      |
+jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+                        Art by Joan Stark, mod. by Paul Buetow
+
+
+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.
+
+2021-11-29 Bash Golf Part 1
+2022-01-01 Bash Golf Part 2
+2023-12-10 Bash Golf Part 3 (You are currently reading this)
+
+

FUNCNAME


+
+FUNCNAME 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 FUNCNAME. This is an array variable that contains the names of all shell functions currently in the execution call stack. The element FUNCNAME[0] holds the name of the currently executing function, FUNCNAME[1] the name of the function that called that, and so on.
+
+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:
+
+ +
#!/usr/bin/env bash
+
+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)
+
+    echo "$level|$stamp|$pid|$callee|$message" >&2
+}
+
+at_home_friday_evening () {
+    log INFO 'One Peperoni Pizza, please'
+}
+
+at_home_friday_evening
+
+
+The output is as follows:
+
+ +
./logexample.sh
+INFO|20231210-082732|123002|at_home_friday_evening|One Peperoni Pizza, please
+
+
+

:(){ :|:& };:


+
+This one may be widely known already, but I am including it here as I found a cute image illustrating it. But to break :(){ :|:& };: down:
+
+
    +
  • :(){ } is really a declaration of the function :
  • +
  • The ; is ending the current statement
  • +
  • The : at the end is calling the function :
  • +
  • :|:& is the function body
  • +

+Let's break down the function body :|:&:
+
+
    +
  • The first : is calling the function recursively
  • +
  • The |: is piping the output to the function : again (parallel recursion)
  • +
  • The & lets it run in the background.
  • +

+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 root!)
+
+And here is the cute illustration:
+
+Bash fork bomb
+
+

Inner functions


+
+Bash defines variables as it is interpreting the code. The same applies to function declarations. Let's consider this code:
+
+ +
#!/usr/bin/env bash
+
+outer() {
+  inner() {
+    echo 'Intel inside!'
+  }
+  inner
+}
+
+inner
+outer
+inner
+
+
+And let's execute it:
+
+
+❯ ./inner.sh
+/tmp/inner.sh: line 10: inner: command not found
+Intel inside!
+Intel inside!
+
+
+What happened? The first time inner was called, it wasn't defined yet. That only happens after the outer run. Note that inner will still be globally defined. But functions can be declared multiple times (the last version wins):
+
+ +
#!/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:
+
+
+❯ ./inner2.sh
+Intel inside!
+Intel inside!
+Wintel inside!
+Wintel inside!
+
+
+

Exporting functions


+
+Have you ever wondered how to execute a shell function in parallel through xargs? The problem is that this won't work:
+
+ +
#!/usr/bin/env bash
+
+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 "{}"'
+
+
+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:
+
+
+❯ ./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
+
+
+There's an easy solution for this. Just export the function! It will then be magically available in any sub-shell!
+
+ +
#!/usr/bin/env bash
+
+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 "{}"'
+
+
+When we run this now, we get:
+
+
+❯ ./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
+
+
+If some_expensive_function 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:
+
+ +
#!/usr/bin/env bash
+
+some_other_function() {
+  echo "$1"
+}
+
+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 "{}"'
+
+
+... because some_other_function isn't exported! You will also need to add an export -f some_other_function!
+
+

Dynamic variables with local


+
+You may know that local 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:
+
+ +
#!/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?
+
+Let's run it:
+
+
+❯ ./dynamic.sh
+bar
+baz
+foo
+
+
+What happened? The variable foo (declared with local) 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 foo', and the change will be visible up the call stack. It's not a global variable; on the last line, echo "$foo"` echoes the global variable content.
+
+
+

if conditionals


+
+Consider all variants here more or less equivalent:
+
+ +
#!/usr/bin/env bash
+
+declare -r foo=foo
+declare -r bar=bar
+
+if [ "$foo" = foo ]; then
+  if [ "$bar" = bar ]; then
+    echo ok1
+  fi
+fi
+
+if [ "$foo" = foo ] && [ "$bar" == bar ]; then
+  echo ok2a
+fi
+
+[ "$foo" = foo ] && [ "$bar" == bar ] && echo ok2b
+
+if [[ "$foo" = foo && "$bar" == bar ]]; then
+  echo ok3a
+fi
+
+ [[ "$foo" = foo && "$bar" == bar ]] && echo ok3b
+
+if test "$foo" = foo && test "$bar" = bar; then
+  echo ok4a
+fi
+
+test "$foo" = foo && test "$bar" = bar && echo ok4b
+
+
+The output we get is:
+
+
+❯ ./if.sh
+ok1
+ok2a
+ok2b
+ok3a
+ok3b
+ok4a
+ok4b
+
+
+

Multi-line comments


+
+You all know how to comment. Put a # in front of it. You could use multiple single-line comments or abuse heredocs and redirect it to the : no-op command to emulate multi-line comments.
+
+ +
#!/usr/bin/env bash
+
+# Single line comment
+
+# These are two single line
+# comments one after another
+
+: <<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!
+
+

Don't change it while it's executed


+
+Consider this script:
+
+ +
#!/usr/bin/env bash
+
+echo foo
+echo echo baz >> $0
+echo bar
+
+
+When it is run, it will do:
+
+
+❯ ./if.sh
+foo
+bar
+baz
+❯ cat if.sh
+#!/usr/bin/env bash
+
+echo foo
+echo echo baz >> $0
+echo bar
+echo baz
+
+
+So what happened? The echo baz 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!
+
+
+Other related posts are:
+
+2021-05-16 Personal Bash coding style guide
+2021-06-05 Gemtexter - One Bash script to rule it all
+2021-11-29 Bash Golf Part 1
+2022-01-01 Bash Golf Part 2
+2023-12-10 Bash Golf Part 3 (You are currently reading this)
+
+E-Mail your comments to paul@nospam.buetow.org :-)
+
+Back to the main site
+
+
+
'Mind Management' book notes @@ -422,6 +840,7 @@ blurs html index.html photos thumbs 2022-01-01 Bash Golf Part 2
2023-06-01 KISS server monitoring with Gogios
2023-10-29 KISS static web photo albums with photoalbum.sh (You are currently reading this)
+2023-12-10 Bash Golf Part 3

E-Mail your comments to paul@nospam.buetow.org :-)

@@ -5821,6 +6240,7 @@ jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2021-11-29 Bash Golf Part 1
2022-01-01 Bash Golf Part 2 (You are currently reading this)
+2023-12-10 Bash Golf Part 3

Redirection



@@ -6293,6 +6713,7 @@ PAUL:X:1000:1000:PAUL BUETOW:/HOME/PAUL:/BIN/BASH 2021-06-05 Gemtexter - One Bash script to rule it all
2021-11-29 Bash Golf Part 1
2022-01-01 Bash Golf Part 2 (You are currently reading this)
+2023-12-10 Bash Golf Part 3

E-Mail your comments to paul@nospam.buetow.org :-)

@@ -6475,6 +6896,7 @@ jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2021-11-29 Bash Golf Part 1 (You are currently reading this)
2022-01-01 Bash Golf Part 2
+2023-12-10 Bash Golf Part 3

TCP/IP networking



@@ -6927,6 +7349,7 @@ bash: line 1: 1/10.0 : syntax error: invalid arithmetic operator (error token is 2021-06-05 Gemtexter - One Bash script to rule it all
2021-11-29 Bash Golf Part 1 (You are currently reading this)
2022-01-01 Bash Golf Part 2
+2023-12-10 Bash Golf Part 3

E-Mail your comments to paul@nospam.buetow.org :-)

@@ -7633,6 +8056,7 @@ assert::equals "$(generate::m 2022-08-27 Gemtexter 1.1.0 - Let's Gemtext again
2023-03-25 Gemtexter 2.0.0 - Let's Gemtext again²
2023-07-21 Gemtexter 2.1.0 - Let's Gemtext again³
+2023-12-10 Bash Golf Part 3

E-Mail your comments to paul@nospam.buetow.org :-)

@@ -8040,6 +8464,7 @@ fi 2021-06-05 Gemtexter - One Bash script to rule it all
2021-11-29 Bash Golf Part 1
2022-01-01 Bash Golf Part 2
+2023-12-10 Bash Golf Part 3

E-Mail your comments to paul@nospam.buetow.org :-)

@@ -8488,139 +8913,6 @@ Total time: 1213.00s
E-Mail your comments to paul@nospam.buetow.org :-)

-Back to the main site
- - -
- - Object oriented programming with ANSI C - - https://foo.zone/gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.html - 2016-11-20T22:10:57+00:00 - - Paul Buetow aka snonux - paul@dev.buetow.org - - You can do a little of object-oriented programming in the C Programming Language. However, that is, in my humble opinion, limited. It's easier to use a different programming language than C for OOP. But still it's an interesting exercise to try using C for this. - -
-

Object oriented programming with ANSI C


-
-Published at 2016-11-20T22:10:57+00:00; Updated at 2022-01-29
-
-
-  ___   ___  ____        ____ 
- / _ \ / _ \|  _ \      / ___|
-| | | | | | | |_) |____| |    
-| |_| | |_| |  __/_____| |___ 
- \___/ \___/|_|         \____|
-                              
-
-
-You can do a little of object-oriented programming in the C Programming Language. However, that is, in my humble opinion, limited. It's easier to use a different programming language than C for OOP. But still it's an interesting exercise to try using C for this.
-
-

Function pointers


-
-Let's have a look at the following sample program. All you have to do is to add a function pointer such as "calculate" to the definition of struct "something_s". Later, during the struct initialization, assign a function address to that function pointer:
-
- -
#include <stdio.h>
-
-typedef struct {
-    double (*calculate)(const double, const double);
-    char *name;
-} something_s;
-
-double multiplication(const double a, const double b) {
-    return a * b;
-}
-
-double division(const double a, const double b) {
-    return a / b;
-}
-
-int main(void) {
-    something_s mult = (something_s) {
-        .calculate = multiplication,
-        .name = "Multiplication"
-    };
-
-    something_s div = (something_s) {
-        .calculate = division,
-        .name = "Division"
-    };
-
-    const double a = 3, b = 2;
-
-    printf("%s(%f, %f) => %f\n", mult.name, a, b, mult.calculate(a,b));
-    printf("%s(%f, %f) => %f\n", div.name, a, b, div.calculate(a,b));
-}
-
-
-As you can see, you can call the function (pointed by the function pointer) with the same syntax as in C++ or Java:
-
- -
printf("%s(%f, %f) => %f\n", mult.name, a, b, mult.calculate(a,b));
-printf("%s(%f, %f) => %f\n", div.name, a, b, div.calculate(a,b));
-
-
-However, that's just syntactic sugar for:
-
- -
printf("%s(%f, %f) => %f\n", mult.name, a, b, (*mult.calculate)(a,b));
-printf("%s(%f, %f) => %f\n", div.name, a, b, (*div.calculate)(a,b));
-
-
-Output:
-
- -
pbuetow ~/git/blog/source [38268]% gcc oop-c-example.c -o oop-c-example
-pbuetow ~/git/blog/source [38269]% ./oop-c-example
-Multiplication(3.000000, 2.000000) => 6.000000
-Division(3.000000, 2.000000) => 1.500000
-
-
-Not complicated at all, but nice to know and helps to make the code easier to read!
-
-

That's not OOP, though


-
-However, that's not really how it works in object-oriented languages such as Java and C++. The method call in this example is not a method call as "mult" and "div" in this example are not "message receivers". I mean that the functions can not access the state of the "mult" and "div" struct objects. In C, you would need to do something like this instead if you wanted to access the state of "mult" from within the calculate function, you would have to pass it as an argument:
-
- -
mult.calculate(mult,a,b));
-
-
-

Real object oriented programming with C


-
-If you want to take it further, hit "Object-Oriented Programming with ANSI-C" into your favourite internet search engine or follow the link below. It goes as far as writing a C preprocessor in AWK, which takes some object-oriented pseudo-C and transforms it to plain C so that the C compiler can compile it to machine code. This is similar to how the C++ language had its origins.
-
-https://www.cs.rit.edu/~ats/books/ooc.pdf
-
-

OOP design patterns in the Linux Kernel


-
-Big C software projects, like Linux, also follow some OOP techniques:
-
-https://lwn.net/Articles/444910/
-
-C is a very old programming language with it's quirks. This might be one of the reasons why Linux will also let Rust code in.
-
-E-Mail your comments to paul@nospam.buetow.org :-)
-
Back to the main site
diff --git a/gemfeed/index.html b/gemfeed/index.html index f5c3bcbd..11b68e6f 100644 --- a/gemfeed/index.html +++ b/gemfeed/index.html @@ -12,6 +12,7 @@

To be in the .zone!



+2023-12-10 - Bash Golf Part 3
2023-11-11 - 'Mind Management' book notes
2023-10-29 - KISS static web photo albums with photoalbum.sh
2023-09-25 - DTail usage examples
-- cgit v1.2.3