aboutsummaryrefslogtreecommitdiff
path: root/debian/Makefile.replace.sh
blob: d47d92bf4d92be9258f72c02403aa0e8ea71dcc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# from http://mywiki.wooledge.org/BashFAQ/021

string_rep()
{
	# initialize vars
	in=$1
	unset out

	# SEARCH must not be empty
	test -n "$2" || return

	while true; do
		# break loop if SEARCH is no longer in "$in"
		case "$in" in
			*"$2"*) : ;;
			*) break;;
		esac

		# append everything in "$in", up to the first instance of SEARCH, and REP, to "$out"
		out=$out${in%%"$2"*}$3
		# remove everything up to and including the first instance of SEARCH from "$in"
		in=${in#*"$2"}
	done

	# append whatever is left in "$in" after the last instance of SEARCH to out, and print
	printf '%s%s\n' "$out" "$in"
}