#!/bin/bash
# 使用单码替换(单一字母替换法)来进行加密.
key=ETAOINSHRDLUBCFGJMQPVWZYXK
# "key"是一个乱序的字母表.
# 修改"key"就会修改加密的结果.
if [ $# -gt 2 ];then
echo "Usage:`basename $0` [-e|-d] [filename]"
echo "Use the -h or --help option for the detail."
exit 0
fi
# 'cat "$@"' 结构既可以从stdin获得输入, 也可以从文件中获得输入.
# 如果使用stdin, 那么要想结束输入就使用 Control-D.
# 否则就要在命令行上指定文件名.
if [ -z $1 ];then
cat "$@" | tr "a-z" "A-Z" | tr "A-Z" "$key"
# | 转化为大写 | 加密
elif [ $# -eq 1 ];then
if [ $1 == "-h" ] || [ $1 == "--help" ];then
cat <<-End-Of-Message
Usage:`basename $0` [-e|-d] [filename]
既可以从stdin获得输入, 也可以从文件中获得输入.
如果使用stdin, 那么要想结束输入就使用 Control-D.
否则就要在命令行上指定文件名.
-e 加密(缺省值)
-d 解密
End-Of-Message
elif [ $1 == "-d" ];then
shift
cat "$@" | tr "$key" "A-Z"
# | 解密
elif [ $1 == "-e" ];then
shift
cat "$@" | tr "a-z" "A-Z" | tr "A-Z" "$key"
elif [[ $1 == -* ]];then
echo "Usage:`basename $0` [-e|-d] [filename]"
echo "Use the -h or --help option for the detail."
else
cat "$@" | tr "a-z" "A-Z" | tr "A-Z" "$key"
fi
else
if [ $1 == "-e" ] && [[ $2 != -* ]];then
shift
cat "$@" | tr "a-z" "A-Z" | tr "A-Z" "$key"
elif [ $1 == "-d" ] && [[ $2 != -* ]];then
shift
cat "$@" | tr "$key" "A-Z"
else
echo "Usage:`basename $0` [-e|-d] [filename]"
echo "Use the -h or --help option for the detail."
fi
fi
# 小写, 大写, 或混合大小写, 都可以正常加密.
# 但是传递进来的非字母字符将不会起任何变化.
exit 0