I have read many tutorials on the internet about the usage of the 'tr' command. However, I am not able to understand how to encrypt an email address with a shell script开发者_JS百科 shift the characters using rot13. Can any one give a link or an example?
Not sure exactly how you want to use this, but here's a basic example to get you started:
echo 'fooman@example.com' | tr 'A-Za-z' 'N-ZA-Mn-za-m'
To make it easier, you can alias the tr
command in your .bashrc
file thusly:
alias rot13="tr 'A-Za-z' 'N-ZA-Mn-za-m'"
Now you can just call:
echo 'fooman@example.com' | rot13
A perfect task for tr
, indeed. This should do what you want:
tr 'A-Za-z' 'N-ZA-Mn-za-m'
Each character in the first set will be replaced with the corresponding character in the second set. E.g. A replaced with N, B replaced with O, etc.. And then the same for the lower case letters. All other characters will be passed through unchanged.
Note the lack of [
and ]
where you normally might expect them. This is because tr
treats square brackets literally, not as range expressions. So, for example, tr -d '[A-Z]'
will delete capital letters and square brackets. If you wanted to keep your brackets, use tr -d 'A-Z'
:
$ echo "foo BAR [baz]" | tr -d '[A-Z]'
foo baz
$ echo "foo BAR [baz]" | tr -d 'A-Z'
foo [baz]
Same for character classes. E.g. tr -d '[[:lower:]]'
is probably an error, and should be tr -d '[:lower:]'
.
However, in lucky situations like this one, you can get away with including the brackets anyway! For example, tr "[a-z]" "[A-Z]"
accidentally works because the square brackets in the first set are replaced by identical square brackets from the second set, but really this is a bad habit to get into. Use tr "a-z" "A-Z"
instead.
Ruby(1.9+)
$ ruby -ne 'print $_.tr( "A-Za-z", "N-ZA-Mn-za-m") ' file
Python
$ echo "test" | python -c 'import sys; print sys.stdin.read().encode("rot13")'
to simultaneously do ROT13 (for letters) and ROT5 (for numbers):
tr 'A-Za-z0-9' 'N-ZA-Mn-za-m5-90-4'
usage:
echo test | tr 'A-Za-z0-9' 'N-ZA-Mn-za-m5-90-4'
alias definition for your ~/.bashrc
in case you need it more often:
alias rot="tr 'A-Za-z0-9' 'N-ZA-Mn-za-m5-90-4'"
(accurately rot135 or rot18)
# Reciprocal Transformation(s)
# rot13
tr 'A-Za-z' 'N-ZA-Mn-za-m' <<< 'user@domain.com'
# rot13.5 (rot18)
tr 'A-Za-z0-9' 'N-ZA-Mn-za-m5-90-4' <<< 'user123@domain.com'
# rot47
tr '\!-~' 'P-~\!-O' <<< 'user123@domain.com'
# rot13 -- SED anyone
echo 'user@domain.com' | sed y/NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/
Shell Script
#!/bin/bash
# Purpose: Rotate 13 characters (a reciprocal transformation)
# ./rot13.sh 'A String to look Ciphered'
tr 'A-Za-z' 'N-ZA-Mn-za-m' <<< "$1"
exit $?
You can execute it inside a shell script
#!/bin/bash
echo 'rot13@rot.com' | tr 'A-Za-z' 'N-ZA-Mn-za-m'
echo 'rot13@rot.com' | rot13
Make the file executable by running:
sudo chmod +x file_name
To ouput the answer run:
./file_name
example:
./try.sh
精彩评论