Skip to content

Instantly share code, notes, and snippets.

@ernstki
Last active July 22, 2019 19:04
Show Gist options
  • Save ernstki/ed7f283b9d5fd7057a1a4bba3f3d256c to your computer and use it in GitHub Desktop.
Save ernstki/ed7f283b9d5fd7057a1a4bba3f3d256c to your computer and use it in GitHub Desktop.
recase - convert input (filename or standard input) to all upper-/lower-case
#!/bin/bash
#
# Re-case the input to all upper- / lower-case based on the name of the symlink
# that points to this script
#
# Author: Kevin Ernst <ernstki -at- mail.uc.edu>
# Date: 27 June 2018
#
ME=$(basename "$BASH_SOURCE")
INFILE=
# if a filename argument is given, feed it to tr's stdin
[ -f "$1" ] && INFILE="<'$1'"
case $ME in
u*)
# e.g., 'uppercase' or 'ucase'
# translate lower-case to upper-case
TRANS="'a-z' 'A-Z'" ;;
l*|d*)
# e.g., 'downcase' or 'lowercase'
# do a lower-case translation instead
TRANS="'A-Z' 'a-z'" ;;
*)
echo >&2
echo "OH NOES!" >&2
echo "========" >&2
echo >&2
echo "You called this script by a weird name I don't understand!" >&2
echo >&2
echo "Try creating a symlink with a name like 'lcase' or 'ucase'." >&2
echo >&2
exit 1 ;;
esac
eval "tr $TRANS $INFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment