Script `config' relies on extensions of `GNU sed', and is thus not working on all Unixes: - in-place edition of files (-i), which can be replaced with a temporary file; - extended-regexps (-r), which can be split into basic regexps; - single-line calls to `a' command, while some implementations require a leading newline before the parameter. Rewrite calls to `sed' to comply with POSIX interface, and move them to helper functions.
Showing the changes to scripts (1 of 1 file). Show all
Unified · Split
| @@ -62,15 +62,52 @@ checkarg() { · 61 unmodified lines | ||
| 62 | 62 | fi |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | +txt_append() { | |
| 66 | + local anchor="$1" | |
| 67 | + local insert="$2" | |
| 68 | + local infile="$3" | |
| 69 | + local tmpfile="$infile.swp" | |
| 70 | + | |
| 71 | + # sed append cmd: 'a\' + newline + text + newline | |
| 72 | + cmd="$(printf "a\\%b$insert" "\n")" | |
| 73 | + | |
| 74 | + sed -e "/$anchor/$cmd" "$infile" >"$tmpfile" | |
| 75 | + # replace original file with the edited one | |
| 76 | + mv "$tmpfile" "$infile" | |
| 77 | +} | |
| 78 | + | |
| 79 | +txt_subst() { | |
| 80 | + local before="$1" | |
| 81 | + local after="$2" | |
| 82 | + local infile="$3" | |
| 83 | + local tmpfile="$infile.swp" | |
| 84 | + | |
| 85 | + sed -e "s/$before/$after/" "$infile" >"$tmpfile" | |
| 86 | + # replace original file with the edited one | |
| 87 | + mv "$tmpfile" "$infile" | |
| 88 | +} | |
| 89 | + | |
| 90 | +txt_delete() { | |
| 91 | + local text="$1" | |
| 92 | + local infile="$2" | |
| 93 | + local tmpfile="$infile.swp" | |
| 94 | + | |
| 95 | + sed -e "/$text/d" "$infile" >"$tmpfile" | |
| 96 | + # replace original file with the edited one | |
| 97 | + mv "$tmpfile" "$infile" | |
| 98 | +} | |
| 99 | + | |
| 65 | 100 | set_var() { |
| 66 | 101 | local name=$1 new=$2 before=$3 |
| 67 | 102 | |
| 68 | 103 | name_re="^($name=|# $name is not set)" |
| 69 | 104 | before_re="^($before=|# $before is not set)" |
| 70 | 105 | if test -n "$before" && grep -Eq "$before_re" "$FN"; then |
| 71 | − sed -ri "/$before_re/a $new" "$FN" | |
| 106 | + txt_append "^$before=" "$new" "$FN" | |
| 107 | + txt_append "^# $before is not set" "$new" "$FN" | |
| 72 | 108 | elif grep -Eq "$name_re" "$FN"; then |
| 73 | − sed -ri "s:$name_re.*:$new:" "$FN" | |
| 109 | + txt_subst "^$name=.*" "$new" "$FN" | |
| 110 | + txt_subst "^# $name is not set" "$new" "$FN" | |
| 74 | 111 | else |
| 75 | 112 | echo "$new" >>"$FN" |
| 76 | 113 | fi |
| @@ -79,7 +116,8 @@ set_var() { · 2 unmodified lines | ||
| 79 | 116 | undef_var() { |
| 80 | 117 | local name=$1 |
| 81 | 118 | |
| 82 | − sed -ri "/^($name=|# $name is not set)/d" "$FN" | |
| 119 | + txt_delete "^$name=" "$FN" | |
| 120 | + txt_delete "^# $name is not set" "$FN" | |
| 83 | 121 | } |
| 84 | 122 | |
| 85 | 123 | if [ "$1" = "--file" ]; then |