ROT13 Conversion Script

Discussion and support for all Linux distributions and Unix flavours (FreeBSD, OpenBSD, etc).
Post Reply
Kasyx
Registered User
Posts: 139
Joined: 13 Dec 2006, 02:00

ROT13 Conversion Script

Post by Kasyx »

Whilst bored out of my mind over the weekend, I decided to write a bash script that would convert user input to/from ROT13.

For those of you unaware of what ROT13 actually is, it is a blindingly basic (think teenage-girl-diary) encryption method which basically consists of rotating each character 13 letters over in the alphabet (i.e. "a" would become "n", "b" would become "o", etc.)

Why would I waste my time doing this? Because ROT13 is used more often than you think. Oh, and because I have no life. Anyhow, without further ado, my ROT13 Conversion Script:

Code: Select all

#!/bin/bash
#ROT13 Script
#Converts text to/from rot13 based on arguments parsed from the command line
#Kasyx
#20080516

args={"$@"}
echo "`echo $@ | tr A-Za-z N-ZA-Mn-za-m`"

exit;
What this basically does is parse arguments sent to the script as the input variable, and then pipes it to "tr" (a translation package) which basically translates "a-n" to "m-z" and vice-versa, effectively converting to/from ROT13. (Credit for the discovery of "tr" goes to my good friend Calvin, who asked me to mention his awesomeness).

Thus, to use this script you would type something like ./rot13.sh Kasyx is awesome

However, because I am lazy, I want to just type rot13 Kasyx is awesome right into the command line and have it convert for me, so I pay a visit to my bashrc file (differs in location from distro to distro - I use Gentoo, so /etc/bash/bashrc) and add an alias to my rot13 script. Due to being exceptionally anal, all my scripts sit in /usr/local/bin/, thus my alias line would be the following:

Code: Select all

alias rot13='/usr/local/bin/rot13.sh'
Be sure to set the correct permissions on the script, or else it is unlikely to work...

Code: Select all

chmod -x rot13.sh
At this point, you could do an env-update (or just log out and log back in again) to reload the bashrc file, and behold!

Code: Select all

kasyx@hephaestus ~ $ rot13 Kasyx is awesome
Xnflk vf njrfbzr
Post Reply