Skip to content

Commit abe971f

Browse files
committed
Fix sed portability across macOS and Linux
Add a sedi() wrapper function to all shell scripts that use in-place sed editing. BSD sed (macOS) requires a separate empty-string backup argument (sed -i '' 's/...'), while GNU sed (Linux) treats the empty string as a separate argument and fails. The wrapper detects GNU vs BSD sed at runtime and calls the correct form.
1 parent a88ca23 commit abe971f

4 files changed

Lines changed: 17 additions & 9 deletions

File tree

telco-core/configuration/compare.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#! /bin/bash
22

3+
sedi() { sed --version 2>/dev/null | grep -q GNU && sed -i "$@" || sed -i '' "$@"; }
4+
35
trap cleanup EXIT
46

57
function cleanup() {
@@ -41,8 +43,8 @@ function compare_cr {
4143
while IFS= read -r file; do
4244
[[ ${file::1} != "#" ]] || continue # Skip any comment lines in the exclusionfile
4345
[[ -n ${file} ]] || continue # Skip empty lines
44-
sed -i "/${file##*/}/d" source_file
45-
sed -i "/${file##*/}/d" rendered_file
46+
sedi "/${file##*/}/d" source_file
47+
sedi "/${file##*/}/d" rendered_file
4648
done < <(cat same_file "$exclusionfile")
4749

4850
if [[ -s source_file || -s rendered_file ]]; then

telco-hub/configuration/reference-crs-kube-compare/compare.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#! /bin/bash
22

3+
sedi() { sed --version 2>/dev/null | grep -q GNU && sed -i "$@" || sed -i '' "$@"; }
4+
35
trap cleanup EXIT
46

57
function cleanup() {
@@ -38,8 +40,8 @@ function compare_cr {
3840
while IFS= read -r file; do
3941
[[ ${file::1} != "#" ]] || continue # Skip any comment lines in the exclusionfile
4042
[[ -n ${file} ]] || continue # Skip empty lines
41-
sed -i "/${file##*/}/d" source_file
42-
sed -i "/${file##*/}/d" rendered_file
43+
sedi "/${file##*/}/d" source_file
44+
sedi "/${file##*/}/d" rendered_file
4345
done < "$exclusionfile"
4446

4547
local source_cr rendered
@@ -65,8 +67,8 @@ function compare_cr {
6567
while IFS= read -r file; do
6668
[[ ${file::1} != "#" ]] || continue # Skip any comment lines in the exclusionfile
6769
[[ -n ${file} ]] || continue # Skip empty lines
68-
sed -i "/${file##*/}/d" source_file
69-
sed -i "/${file##*/}/d" rendered_file
70+
sedi "/${file##*/}/d" source_file
71+
sedi "/${file##*/}/d" rendered_file
7072
done < <(cat same_file "$exclusionfile")
7173

7274
if [[ -s source_file || -s rendered_file ]]; then

telco-hub/configuration/reference-crs/required/gitops/get_ztp_installation.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#! /bin/bash
22

3+
sedi() { sed --version 2>/dev/null | grep -q GNU && sed -i "$@" || sed -i '' "$@"; }
4+
35
# This script is only needed to create the ztp-installation manifest
46
# once per each Minor version.
57

@@ -21,7 +23,7 @@ find ./ztp-installation/ -name "*.yaml" -exec yq -i eval '.metadata.annotations.
2123

2224
# patch the ztp-site-generate version
2325
echo " - Patch ztp-site-generate version"
24-
sed -i 's|quay.io/openshift-kni/ztp-site-generator:latest|registry.redhat.io/openshift4/ztp-site-generate-rhel8:v4.21|g' ztp-installation/argocd-openshift-gitops-patch.json
26+
sedi 's|quay.io/openshift-kni/ztp-site-generator:latest|registry.redhat.io/openshift4/ztp-site-generate-rhel8:v4.21|g' ztp-installation/argocd-openshift-gitops-patch.json
2527

2628
echo " - Adding elements to the whitelist"
2729
yq '.spec.namespaceResourceWhitelist += {"group": "'metal3.io'", "kind": "DataImage"}' ztp-installation/app-project.yaml

telco-ran/configuration/hack/update_file_references.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
set -euo pipefail
1414

15+
sedi() { sed --version 2>/dev/null | grep -q GNU && sed -i "$@" || sed -i '' "$@"; }
16+
1517
if [[ ! -d "source-crs" ]]; then
1618
echo "Error: This script must be run from a directory containing 'source-crs' (e.g., the 'configuration' directory)." >&2
1719
exit 1
@@ -67,12 +69,12 @@ for source_file in "${source_files[@]}"; do
6769
# Choose the replacement strategy based on the file's content.
6870
if grep -q "source-crs/.*$file_name" "$target_file"; then
6971
# This file contains an incorrect 'source-crs' path. Fix it.
70-
sed -i -e "s|source-crs/[^[:space:]\`[]*${escaped_file_name}|source-crs/$replacement_path|g" "$target_file"
72+
sedi -e "s|source-crs/[^[:space:]\`[]*${escaped_file_name}|source-crs/$replacement_path|g" "$target_file"
7173
else
7274
# This file contains a different incorrect full path. Fix it.
7375
# This regex finds a path-like string ending in the filename.
7476
replacement_path=${replacement_path#%source-crs/}
75-
sed -i -e "s|[^[:space:]\`[,]*${escaped_file_name}|${replacement_path}|g" "$target_file"
77+
sedi -e "s|[^[:space:]\`[,]*${escaped_file_name}|${replacement_path}|g" "$target_file"
7678
fi
7779
done
7880
done

0 commit comments

Comments
 (0)