From ea66e5a0a76de4f6625b99e100cb5bdc120e82b0 Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Thu, 29 Feb 2024 14:00:01 -0500 Subject: [PATCH] fix: compatibility with bash v3 (mac) --- Readme.org | 35 ++++++++++++++++++++--------------- tomono | 5 ++++- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Readme.org b/Readme.org index ab8e237..a150c7d 100644 --- a/Readme.org +++ b/Readme.org @@ -597,9 +597,14 @@ every command before it gets executed. #+NAME: set-flags #+BEGIN_SRC shell set -euo pipefail ${DEBUGSH+-x} -shopt -s inherit_errexit + +if ((BASH_VERSINFO[0] > 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 4))); then + shopt -s inherit_errexit +fi #+END_SRC +Also contains a monstrosity which is essentially a version guard around the =inherit_errexit= option, which was only introduced in Bash 4.4. Notably Mac’s default bash doesn’t support it so the version guard is useful. + *** Windows newline fix On Windows the config file could contain windows newline endings (CRLF). Bash doesn’t handle those as proper field separators. Even on Windows... @@ -795,9 +800,9 @@ The examples from this document can be combined into a test script: #+name: test #+BEGIN_SRC shell :tangle test :shebang "#!/usr/bin/env bash" :noweb yes :references yes -set -xeuo pipefail -shopt -s inherit_errexit -shopt -s globstar +<> +# In tests always echo the command: +set -x export DEBUGSH=true # The tomono script is tangled right next to the test script @@ -834,37 +839,37 @@ EOF echo "Checking master" git checkout master -diff -u <(head **/*.*) <(cat < foo/i-am-foo.txt <== +diff -u <(find . -name '*.txt' | sort | xargs head) <(cat < ./foo/i-am-foo.txt <== This is foo -==> lib/zim/i-am-zim.txt <== +==> ./lib/zim/i-am-zim.txt <== This is zim -==> 中文/你好.txt <== +==> ./中文/你好.txt <== 你好 EOF ) echo "Checking branch-a" git checkout branch-a -diff -u <(head **/*.*) <(cat < foo/feature-a.txt <== +diff -u <(find . -name '*.txt' | sort | xargs head) <(cat < ./foo/feature-a.txt <== I am a new foo feature -==> foo/i-am-foo.txt <== +==> ./foo/i-am-foo.txt <== This is foo -==> lib/zim/feature-a.txt <== +==> ./lib/zim/feature-a.txt <== I am a new zim feature -==> lib/zim/i-am-zim.txt <== +==> ./lib/zim/i-am-zim.txt <== This is zim -==> 中文/feature-a.txt <== +==> ./中文/feature-a.txt <== 你好 from feature-a -==> 中文/你好.txt <== +==> ./中文/你好.txt <== 你好 EOF ) diff --git a/tomono b/tomono index 0dc87dd..a732fda 100755 --- a/tomono +++ b/tomono @@ -1,6 +1,9 @@ #!/usr/bin/env bash set -euo pipefail ${DEBUGSH+-x} -shopt -s inherit_errexit + +if ((BASH_VERSINFO[0] > 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 4))); then + shopt -s inherit_errexit +fi # Poor man’s arg parse :/ arg="${1-}"