All pastes #1084928 Raw Edit

Miscellany

public text v1 · immutable
#1084928 ·published 2008-07-28 05:44 UTC
rendered paste body
#!/bin/sh
#
# Rot13 shell script
#
# Copyright (c) 2005 Lorance Stinson <LoranceStinson+rot13@gmail.com>

# These is used by sed to rot13 a file.
rot13from='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
rot13to='nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'

#**************************************************************************
# Help text.
#**************************************************************************
usage(){
    cat<<EOHELP
Usage: `basename $0` [FILE|WORD(s)]
An enhancement of the classic Unix rot13 utility.
Rotates a file, words or STDIN by 13 characters.
Use to (en/de)crypt secret messsages.
EOHELP
    exit 1
}

#**************************************************************************
# Rot some text using the Unix utility tr.
#**************************************************************************
rot13tr(){
    echo $1 | tr '[a-zA-Z]' '[n-za-mN-ZA-M]'
}

#**************************************************************************
# Process the command line arguments.
#**************************************************************************

if [ "$#" -gt 0 ] ; then
    if [ -f $1 -a -r $1 ] ; then
        # Rot13 the file.
        sed -e "y/$rot13from/$rot13to/" < $1
    elif [ `expr "$1" : "-*-h"` -ne 0 ] ; then
        # Print the help text.
        usage
    else
        # Rot13 all the arguments.
        rot13tr "$@"
    fi
else
    # No arguments, act like a filter.
    IFS=""
    while read line ; do
        rot13tr $line
    done
fi