Chương trình mã hoá chữ
#!/bin/bash
echo "bat dau"
set -e
doanmatd=""
ham_dc() {
local madao=$1 #madao ma dung de dao chu
local doanma=$2
echo "ma dao chuong trinh lam $madao"
echo "doan ma chuong tri la $doanma"
local name=0
local kt=''
local sokt=$(echo -n "$madao" | wc -m) # sokt la so ki tu cua ma
echo "so kyntu $sokt"
local stht=0
for ((i=1; i<=sokt; i++))
do
stht=$(echo -n "$madao" | cut -c${i})
declare "var$i=$stht"
done
for ((j=1; j<=sokt; j++))
do
name="var${j}"
echo "biem name $name"
kt=$(echo "$doanma" | cut -c${!name})
doanmatd=$(echo "$doanmatd" | sed "s/$/$kt/g")
echo "da hoan thanh ct doi chu :: $doanmatd"
done
}
ham_tc() {
local doanchu=$(sed ':a;N;$!ba;s/ /~/g;s/\n/?/g;s/\t/%/g' file1.txt)
#~ dau cach
#? dau xuong dong
#% dau tab
cat file1.txt
echo "doan chu $doanchu"
local chu=""
local doanchuchia=$(echo "$doanchu" | sed 's/.\{4\}/&*/g')
echo "doan chu da chia $doanchuchia"
local sochu=$(echo -n "$doanchuchia" | tr -cd '*' | wc -m)
for ((j=1;j<=sochu; j++))
do
chu=$(echo "$doanchuchia" | cut -d '*' -f${j})
echo "$chu" >> file2.txt
done
}
ham_cat_chu() {
echo "ham cat chu khoi dong"
local doanma2
local motchu=""
doanma2=$(cat file2.txt)
local sodong=0
sodong=$(wc -l < file2.txt)
echo "$sodong day la so dong"
for ((h=1;h<=sodong; h++))
do
motchu=$(sed -n "${h}p" file2.txt)
ham_dc $madao2 $motchu
echo "$doanmatd" >> file3.txt
doanmatd=""
done
}
ham_ghep_chu() {
local doanchu2=$(sed ':a;N;$!ba;s/\n//g' file3.txt)
echo "$doanchu2" > file4.txt
sed 's/~/ /g;s/?/\n/g;s/%/\t/g' file4.txt > file_dao.txt
}
doanma3=$1
madao2=$2
rm file1.txt 2> /dev/null || echo "file1 khong ton tai"
touch file1.txt && echo "tao thanh cong file1"
rm file2.txt 2> /dev/null || echo "file2 khong ton tai"
touch file2.txt && echo "tao thanh cong file2.txt"
rm file3.txt 2> /dev/null || echo "file2 khong ton tai"
touch file3.txt && echo "tao thanh cong file2.txt"
rm file4.txt 2> /dev/null || echo "file2 khong ton tai"
touch file4.txt && echo "tao thanh cong file2.txt"
rm file_dao.txt 2> /dev/null || echo "file2 khong ton tai"
touch file_dao.txt && echo "tao thanh cong file2.txt"
echo "chuong trinh bat dau chay"
cat "$doanma3"
cat "$doanma3" > file1.txt
ham_tc
ham_cat_chu
ham_ghep_chu
rm file1.txt
rm file2.txt
rm file3.txt
rm file4.txt