-
Notifications
You must be signed in to change notification settings - Fork 71
Fix macOS sed compatibility across all shell scripts #746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,7 @@ | ||||||||||||||||||
| #! /bin/bash | ||||||||||||||||||
|
|
||||||||||||||||||
| sedi() { sed --version 2>/dev/null | grep -q GNU && sed -i "$@" || sed -i '' "$@"; } | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: cat -n telco-hub/configuration/reference-crs-kube-compare/compare.sh | head -10Repository: openshift-kni/telco-reference Length of output: 331
The Suggested fix-sedi() { sed --version 2>/dev/null | grep -q GNU && sed -i "$@" || sed -i '' "$@"; }
+sedi() {
+ if sed --version 2>/dev/null | grep -q GNU; then
+ sed -i "$@"
+ else
+ sed -i '' "$@"
+ fi
+}📝 Committable suggestion
Suggested change
🧰 Tools🪛 Shellcheck (0.11.0)[info] 3-3: Note that A && B || C is not if-then-else. C may run when A is true. (SC2015) 🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
| trap cleanup EXIT | ||||||||||||||||||
|
|
||||||||||||||||||
| function cleanup() { | ||||||||||||||||||
|
|
@@ -38,8 +40,8 @@ function compare_cr { | |||||||||||||||||
| while IFS= read -r file; do | ||||||||||||||||||
| [[ ${file::1} != "#" ]] || continue # Skip any comment lines in the exclusionfile | ||||||||||||||||||
| [[ -n ${file} ]] || continue # Skip empty lines | ||||||||||||||||||
| sed -i "/${file##*/}/d" source_file | ||||||||||||||||||
| sed -i "/${file##*/}/d" rendered_file | ||||||||||||||||||
| sedi "/${file##*/}/d" source_file | ||||||||||||||||||
| sedi "/${file##*/}/d" rendered_file | ||||||||||||||||||
| done < "$exclusionfile" | ||||||||||||||||||
|
|
||||||||||||||||||
| local source_cr rendered | ||||||||||||||||||
|
|
@@ -65,8 +67,8 @@ function compare_cr { | |||||||||||||||||
| while IFS= read -r file; do | ||||||||||||||||||
| [[ ${file::1} != "#" ]] || continue # Skip any comment lines in the exclusionfile | ||||||||||||||||||
| [[ -n ${file} ]] || continue # Skip empty lines | ||||||||||||||||||
| sed -i "/${file##*/}/d" source_file | ||||||||||||||||||
| sed -i "/${file##*/}/d" rendered_file | ||||||||||||||||||
| sedi "/${file##*/}/d" source_file | ||||||||||||||||||
| sedi "/${file##*/}/d" rendered_file | ||||||||||||||||||
| done < <(cat same_file "$exclusionfile") | ||||||||||||||||||
|
|
||||||||||||||||||
| if [[ -s source_file || -s rendered_file ]]; then | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,7 @@ | ||||||||||||||||||
| #! /bin/bash | ||||||||||||||||||
|
|
||||||||||||||||||
| sedi() { sed --version 2>/dev/null | grep -q GNU && sed -i "$@" || sed -i '' "$@"; } | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # First, locate the file in the repository
find . -path "*telco-hub/configuration/reference-crs/required/gitops/get_ztp_installation.sh"Repository: openshift-kni/telco-reference Length of output: 153 🏁 Script executed: # Read the first 20 lines of the file to see the code and context
head -20 ./telco-hub/configuration/reference-crs/required/gitops/get_ztp_installation.shRepository: openshift-kni/telco-reference Length of output: 1192 🏁 Script executed: # Count usages of sedi function in the file
grep -n "sedi " ./telco-hub/configuration/reference-crs/required/gitops/get_ztp_installation.sh | head -20Repository: openshift-kni/telco-reference Length of output: 247 🏁 Script executed: # Count all occurrences of sedi (function definition + calls)
grep -c "sedi" ./telco-hub/configuration/reference-crs/required/gitops/get_ztp_installation.sh
# Get full context around the sedi usage
grep -n -B 2 -A 2 "sedi " ./telco-hub/configuration/reference-crs/required/gitops/get_ztp_installation.shRepository: openshift-kni/telco-reference Length of output: 386 🏁 Script executed: # Verify the && || pattern behavior to confirm the issue
# Create a test to show the problem
cat <<'EOF' > /tmp/test_pattern.sh
#!/bin/bash
# Test the problematic pattern
test_pattern() {
# Simulate a failing command in position B of "A && B || C"
(exit 0) && (exit 1) || echo "C was executed (error masked)"
# Verify with explicit if-then-else
if (exit 0); then
(exit 1) && echo "This won't print"
else
echo "This won't print either"
fi
}
test_pattern
EOF
bash /tmp/test_pattern.shRepository: openshift-kni/telco-reference Length of output: 103 Use explicit if-then-else instead of The pattern Suggested fix-sedi() { sed --version 2>/dev/null | grep -q GNU && sed -i "$@" || sed -i '' "$@"; }
+sedi() {
+ if sed --version 2>/dev/null | grep -q GNU; then
+ sed -i "$@"
+ else
+ sed -i '' "$@"
+ fi
+}📝 Committable suggestion
Suggested change
🧰 Tools🪛 Shellcheck (0.11.0)[info] 3-3: Note that A && B || C is not if-then-else. C may run when A is true. (SC2015) 🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
| # This script is only needed to create the ztp-installation manifest | ||||||||||||||||||
| # once per each Minor version. | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -21,7 +23,7 @@ find ./ztp-installation/ -name "*.yaml" -exec yq -i eval '.metadata.annotations. | |||||||||||||||||
|
|
||||||||||||||||||
| # patch the ztp-site-generate version | ||||||||||||||||||
| echo " - Patch ztp-site-generate version" | ||||||||||||||||||
| 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 | ||||||||||||||||||
| 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 | ||||||||||||||||||
|
|
||||||||||||||||||
| echo " - Adding elements to the whitelist" | ||||||||||||||||||
| yq '.spec.namespaceResourceWhitelist += {"group": "'metal3.io'", "kind": "DataImage"}' ztp-installation/app-project.yaml | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,8 @@ | |||||||||||||||||
|
|
||||||||||||||||||
| set -euo pipefail | ||||||||||||||||||
|
|
||||||||||||||||||
| sedi() { sed --version 2>/dev/null | grep -q GNU && sed -i "$@" || sed -i '' "$@"; } | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # First, let's examine the file structure and find it
fd update_file_references.shRepository: openshift-kni/telco-reference Length of output: 128 🏁 Script executed: cat -n telco-ran/configuration/hack/update_file_references.shRepository: openshift-kni/telco-reference Length of output: 3790
Under Use explicit Suggested fix-sedi() { sed --version 2>/dev/null | grep -q GNU && sed -i "$@" || sed -i '' "$@"; }
+sedi() {
+ if sed --version 2>/dev/null | grep -q GNU; then
+ sed -i "$@"
+ else
+ sed -i '' "$@"
+ fi
+}📝 Committable suggestion
Suggested change
🧰 Tools🪛 Shellcheck (0.11.0)[info] 15-15: Note that A && B || C is not if-then-else. C may run when A is true. (SC2015) 🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
| if [[ ! -d "source-crs" ]]; then | ||||||||||||||||||
| echo "Error: This script must be run from a directory containing 'source-crs' (e.g., the 'configuration' directory)." >&2 | ||||||||||||||||||
| exit 1 | ||||||||||||||||||
|
|
@@ -67,12 +69,12 @@ for source_file in "${source_files[@]}"; do | |||||||||||||||||
| # Choose the replacement strategy based on the file's content. | ||||||||||||||||||
| if grep -q "source-crs/.*$file_name" "$target_file"; then | ||||||||||||||||||
| # This file contains an incorrect 'source-crs' path. Fix it. | ||||||||||||||||||
| sed -i -e "s|source-crs/[^[:space:]\`[]*${escaped_file_name}|source-crs/$replacement_path|g" "$target_file" | ||||||||||||||||||
| sedi -e "s|source-crs/[^[:space:]\`[]*${escaped_file_name}|source-crs/$replacement_path|g" "$target_file" | ||||||||||||||||||
| else | ||||||||||||||||||
| # This file contains a different incorrect full path. Fix it. | ||||||||||||||||||
| # This regex finds a path-like string ending in the filename. | ||||||||||||||||||
| replacement_path=${replacement_path#%source-crs/} | ||||||||||||||||||
| sed -i -e "s|[^[:space:]\`[,]*${escaped_file_name}|${replacement_path}|g" "$target_file" | ||||||||||||||||||
| sedi -e "s|[^[:space:]\`[,]*${escaped_file_name}|${replacement_path}|g" "$target_file" | ||||||||||||||||||
| fi | ||||||||||||||||||
| done | ||||||||||||||||||
| done | ||||||||||||||||||
|
|
||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
cat -n telco-core/configuration/compare.sh | head -20Repository: openshift-kni/telco-reference
Length of output: 565
sedi()fallback executes even when GNUsed -ifails, masking errors (Line 3).The
A && B || Cpattern is unsafe: ifsed -i "$@"fails, the|| sed -i '' "$@"still executes, hiding the original failure and potentially causing nondeterministic behavior. Use explicit if-else branching to ensure the fallback only runs when the condition check fails, not when the command fails.Suggested fix
🧰 Tools
🪛 Shellcheck (0.11.0)
[info] 3-3: Note that A && B || C is not if-then-else. C may run when A is true.
(SC2015)
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sebrandon1 please review/consider this suggestion. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.