mb-admin/scripts/utils.sh

27 lines
831 B
Bash
Raw Normal View History

2025-08-06 13:24:04 +00:00
#!/bin/bash
# Reusable function for search and replace operations
# Usage: search_and_replace "search_pattern" "replace_string" "file_path"
search_and_replace() {
local search_pattern="$1"
local replace_string="$2"
local file_path="$3"
if [ ! -f "$file_path" ]; then
echo "Error: File not found at $file_path"
return 1
fi
# Escape special characters for sed
local escaped_replace_string=$(printf '%s\n' "$replace_string" | sed 's:[\/&]:\\&:g')
sudo sed -i.bak "s/${search_pattern}/${escaped_replace_string}/" "$file_path"
if grep -q "$replace_string" "$file_path"; then
echo "Successfully replaced '${search_pattern}' with '${replace_string}' in '$file_path'."
else
echo "Failed to replace '${search_pattern}' in '$file_path'."
return 1
fi
}