Created
March 25, 2012 02:58
-
-
Save rsvp/2190964 to your computer and use it in GitHub Desktop.
quotel.sh : quote lines using prefix and suffix strings. By default, those are double quotes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# bash 4.1.5(1) Linux Ubuntu 10.04 Date : 2012-03-23 | |
# | |
# _______________| quotel : quote lines using prefix and suffix strings. | |
# | |
# Usage: quotel [filename] [prefix string] [suffix string] | |
# # stdin and double quotes are defaults. | |
# # Use '~' to represent an empty string. | |
# | |
# Dependencies: sed | |
# CHANGE LOG LATEST version available: https://bitbucket.org/rsvp/gists/src | |
# | |
# 2012-03-23 First version of a common idiom. | |
# _____ PREAMBLE_v2: settings, variables, and error handling. | |
# | |
LC_ALL=POSIX | |
# locale means "ASCII, US English, no special rules, | |
# output per ISO and RFC standards." | |
# Esp. use ASCII encoding for glob and sorting characters. | |
shopt -s extglob | |
# ^set extended glob for pattern matching. | |
set -e | |
# ^errors checked: immediate exit if a command has non-zero status. | |
set -u | |
# ^unassigned variables shall be errors. | |
# Example of default VARIABLE ASSIGNMENT: arg1=${1:-'foo'} | |
fname=${1:-'-'} | |
prefix=${2:-'"'} | |
suffix=${3:-'"'} | |
program=${0##*/} # similar to using basename | |
# memf=$( mktemp /dev/shm/88_${program}_tmp.XXXXXXXXXX ) | |
cleanup () { | |
# Delete temporary files, then optionally exit given status. | |
local status=${1:-'0'} | |
# rm -f $memf | |
[ $status = '-1' ] || exit $status # thus -1 prevents exit. | |
} #-------------------------------------------------------------------- | |
warn () { | |
# Message with basename to stderr. Usage: warn "message" | |
echo -e "\n !! ${program}: $1 " >&2 | |
} #-------------------------------------------------------------------- | |
die () { | |
# Exit with status of most recent command or custom status, after | |
# cleanup and warn. Usage: command || die "message" [status] | |
local status=${2:-"$?"} | |
cleanup -1 && warn "$1" && exit $status | |
} #-------------------------------------------------------------------- | |
trap "die 'SIG disruption, but cleanup finished.' 114" 1 2 3 15 | |
# Cleanup after INTERRUPT: 1=SIGHUP, 2=SIGINT, 3=SIGQUIT, 15=SIGTERM | |
# | |
# _______________ :: BEGIN Script :::::::::::::::::::::::::::::::::::::::: | |
# If either prefix or suffix was an empty string '' | |
# our default variable assignment would be triggered, so... | |
if [ "$prefix" = '~' ] ; then | |
prefix='' | |
fi | |
if [ "$suffix" = '~' ] ; then | |
suffix='' | |
fi | |
# ... we let ~ represent the empty string. | |
# It cannot be used anyways due to the way we use sed below. | |
# Take the whole line and substitute in prefix and suffix strings. | |
sed -e "s~^.*$~${prefix}&${suffix}~" "$fname" 2> /dev/null \ | |
|| die "tilde ~ forbidden in prefix and suffix strings." 113 | |
cleanup | |
# _______________ EOS :: END of Script :::::::::::::::::::::::::::::::::::::::: | |
# vim: set fileencoding=utf-8 ff=unix tw=78 ai syn=sh : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment