Files
wekan/find-replace.sh
T
Lauri Ojansivu 283e51eac0 Shell scripts: the root and releases/ scripts run on macOS too.
Started from zsh on macOS, a script gets bash 3.2 as /bin/bash and BSD
tools. Every .sh at the repository root and under releases/ was checked
with bash 3.2 and for GNU-only tool options; what failed:

- test-menu.sh: declare -A (bash 4) is a case function, runner_for, and
  an empty match list no longer trips set -u in bash 3.2.
- releases/ferretdb-latest-tag.sh: with no GitHub token, "${auth[@]}"
  was an unbound variable under set -u in bash 3.2; it uses
  ${auth[@]+...} as ferretdb-asset.sh already did.
- find-replace.sh: never worked anywhere ('$1' in single quotes searched
  for the text $1) and used GNU grep -Z and sed -i. It replaces literal
  text with perl now, skipping the directories find.sh skips.
- sha256sum (missing on older macOS) falls back to shasum -a 256 in
  release-x2.sh, repack-bundle-for-arch.sh, install-node-for-arch.sh and
  sandstorm-capnp-schemas.sh; -c works with both.
- sandstorm-capnp-schemas.sh: nproc falls back to sysctl hw.ncpu, and
  sed -i takes a .bak suffix (GNU and BSD alike) that is then removed.
- sandstorm-slim-spk.sh: ln -sfT is ln -sfn, stat -c%s is wc -c.
- virtualbox/ipaddress.sh: ifconfig where there is no ip.

tests/macosShellPortability.test.cjs parses every bash script with
bash 3.2 when /bin/bash is 3.x, and fails on any of these shapes coming
back: associative arrays or mapfile, flock without a fallback, tar
--transform, a GNU-only sed -i, sha256sum / nproc / ip without a
fallback, ln -T or stat -c.

Thanks to xet7 !
2026-10-27 23:02:14 +02:00

22 lines
915 B
Bash
Executable File

#!/bin/bash
# Recursive find/replace of literal text, skipping the same generated and
# temporary directories as find.sh.
# Syntax: ./find-replace.sh searchtext replacetext
#
# The text is matched and replaced as written: no regular expression, so
# "a.b" or "C++" mean exactly that. perl does the work because `sed -i`
# differs between GNU (Linux) and BSD (macOS), and BSD grep's -Z means
# decompress rather than GNU's NUL-separated output.
if (( $# != 2 )); then
echo 'Usage: ./find-replace.sh searchtext replacetext'
exit 1
fi
export FIND_REPLACE_SEARCH="$1" FIND_REPLACE_WITH="$2"
find . -type f -not -path '*/node_modules/*' -not -path '*/.build/*' -not -path '*/_build/*' \
-not -path '*/.meteor/*' -not -path '*/.git/*' -not -path '*/.tools/*' -print0 |
xargs -0 grep -IlF --null -- "$1" 2>/dev/null |
xargs -0 perl -pi -e 's/\Q$ENV{FIND_REPLACE_SEARCH}\E/$ENV{FIND_REPLACE_WITH}/g'