Blame


1 10382cde 2022-01-23 op #!/bin/sh
2 10382cde 2022-01-23 op #
3 10382cde 2022-01-23 op # Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 10382cde 2022-01-23 op #
5 10382cde 2022-01-23 op # Permission to use, copy, modify, and distribute this software for any
6 10382cde 2022-01-23 op # purpose with or without fee is hereby granted, provided that the above
7 10382cde 2022-01-23 op # copyright notice and this permission notice appear in all copies.
8 10382cde 2022-01-23 op #
9 10382cde 2022-01-23 op # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 10382cde 2022-01-23 op # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 10382cde 2022-01-23 op # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 10382cde 2022-01-23 op # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 10382cde 2022-01-23 op # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 10382cde 2022-01-23 op # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 10382cde 2022-01-23 op # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 10382cde 2022-01-23 op #
17 10382cde 2022-01-23 op # subst -- filter that replaces key=values pairs
18 10382cde 2022-01-23 op #
19 10382cde 2022-01-23 op # usage: subst key1=val1 [... keyn=valn] [files...]
20 10382cde 2022-01-23 op #
21 10382cde 2022-01-23 op # Use `--' before the first file if it may contain an = in its name.
22 10382cde 2022-01-23 op # If no files are given, read and substitute from standard input.
23 10382cde 2022-01-23 op
24 10382cde 2022-01-23 op args=''
25 10382cde 2022-01-23 op
26 10382cde 2022-01-23 op for arg; do
27 10382cde 2022-01-23 op if [ "$arg" = '--' ]; then
28 10382cde 2022-01-23 op break
29 10382cde 2022-01-23 op fi
30 10382cde 2022-01-23 op
31 10382cde 2022-01-23 op key="$(printf '%s\n' $arg | cut -s -d '=' -f1)"
32 10382cde 2022-01-23 op if [ -z "$key" ]; then
33 10382cde 2022-01-23 op break
34 10382cde 2022-01-23 op fi
35 10382cde 2022-01-23 op val="$(printf '%s\n' $arg | cut -d '=' -f 2-)"
36 10382cde 2022-01-23 op shift
37 10382cde 2022-01-23 op
38 10382cde 2022-01-23 op args="$args -e s/$key/$val/g "
39 10382cde 2022-01-23 op done
40 10382cde 2022-01-23 op
41 10382cde 2022-01-23 op exec sed $args "$@"