summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-10-23 08:07:54 -0300
committerEuAndreh <eu@euandre.org>2024-10-23 08:07:54 -0300
commit6979748ca768d6945f2c2c17770deb422210cfce (patch)
tree0ed4fa75655d2222d384223f2400018349123487 /src
parentsrc/pre: Add "-n" to prevent ":" output (diff)
downloadeut-6979748ca768d6945f2c2c17770deb422210cfce.tar.gz
eut-6979748ca768d6945f2c2c17770deb422210cfce.tar.xz
src/shesc: Migrate over from dotfiles
Diffstat (limited to 'src')
-rwxr-xr-xsrc/shesc85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/shesc b/src/shesc
new file mode 100755
index 0000000..b7a4a1b
--- /dev/null
+++ b/src/shesc
@@ -0,0 +1,85 @@
+#!/bin/sh
+set -eu
+
+
+usage() {
+ cat <<-'EOF'
+ Usage:
+ shesc [CONTENT...]
+ shesc -h
+ EOF
+}
+
+help() {
+ cat <<-'EOF'
+
+
+ Options:
+ -h, --help show this message
+
+ CONTENT a literal string to be escaped
+
+
+ Convert data to/from POSIX sh double quote string escaping. If
+ CONTENT is not given, get data from STDIN.
+
+
+ Examples:
+
+ Escape sh control characters in a string:
+
+ $ printf 'content="%s"\n' "$(shesc '"string" $(dollar "`cmd`") \ ')"
+ content="\"string\" \$(dollar \"\`cmd\`\") \\ "
+
+
+ Escape the content from a file:
+
+ $ shesc < file.conf
+ EOF
+}
+
+
+for flag in "$@"; do
+ case "$flag" in
+ (--)
+ break
+ ;;
+ (--help)
+ usage
+ help
+ exit
+ ;;
+ (*)
+ ;;
+ esac
+done
+
+while getopts 'h' flag; do
+ case "$flag" in
+ (h)
+ usage
+ help
+ exit
+ ;;
+ (*)
+ usage >&2
+ exit 2
+ ;;
+ esac
+done
+shift $((OPTIND - 1))
+
+
+
+esc() {
+ sed 's|\([\\`"$]\)|\\\1|g'
+}
+
+
+if [ $# = 0 ]; then
+ esc
+else
+ for s in "$@"; do
+ printf '%s\n' "$s" | esc
+ done
+fi