代码块
字典是自己准备的,也可以从网上找上传至linux或者直接使用网上词典;
#!/bin/bash
BOOK="/project/sh/dictionary.txt"
function1 (){
read -p "请输入要查找的词:" ENGLISH
ANSWER=`grep -w "$ENGLISH" $BOOK | sed "s/$ENGLISH //p" | head -n 1`
echo -e "\033[35m $ENGLISH 的中文意思是:$ANSWER \033[0m"
}
function2 (){
read -p "请输入要查找的词:" CHINESE
ANS=`grep "$CHINESE" $BOOK | egrep -o "[a-z]+ "`
AN=`grep "$CHINESE" $BOOK | egrep -o "[a-z]+ " | wc -l`
for((i=1;i<=$AN;i++));do
TRANS=`grep "$CHINESE" $BOOK | egrep -o "[a-z]+ " | sed -n "$i p"`
CHINESES=`grep "$CHINESE" $BOOK | egrep -o "[^[:space:]]+" | awk 'NR%2==0' | sed -n "$i p"`
echo -e "\033[35m $CHINESES 对应的英文是:$TRANS \033[0m";
done
}
function3 (){
read -p "要删除的单词:" DELETE
DEL=`sed -n "/\<$DELETE\>/=" $BOOK`
sed -i "$DEL"d $BOOK
echo -e "\033[35m 删除成功!\033[0m"
}
function4 (){
read -p "要添加的单词:" ADD
read -p "请输入中文释义:" ADDS
WORDS="$ADD $ADDS"
echo $WORDS >> $BOOK
echo -e "\033[35m 添加成功!\033[0m"
}
function5 (){
read -p "请输入要修改的单词:" AMEND
read -p "请输入修改后的英文和中文(其间请用空格或换行隔开):" AFTERE AFTERC
TRANSLATE=`grep "\<$AMEND\>" $BOOK | sed "s/$AMEND //p" | head -n1`
sed -i "s/\<$AMEND\>/$AFTERE/g" $BOOK
sed -i "s/\<$TRANSLATE\>/$AFTERC/g" $BOOK
echo -e "\033[35m 修改成功!\033[0m"
}
color1='\033[32m'
echo -e "\033[40;37m 当前词典一共有7986个单词\033[0m"
echo -e "$color1 *************************\033[0m"
echo -e "$color1 |*******1.英译汉********|\033[0m"
echo -e "$color1 |*******2.汉译英********|\033[0m"
echo -e "$color1 |*******3.删除词条******|\033[0m"
echo -e "$color1 |*******4.添加词条******|\033[0m"
echo -e "$color1 |*******5.修改词条******|\033[0m"
echo -e "$color1 |*******6.保存并退出****|\033[0m"
echo -e "$color1 *************************\033[0m"
while true ;do
read -p "请输入序号:" NUM
case $NUM in
1)
function1
;;
2)
function2
;;
3)
function3
;;
4)
function4
;;
5)
function5
;;
6)
echo -e "\033[34m 欢迎再次使用\n \033]0m"
break;
;;
*)
echo -e "\033[33m ***请输入正确的序号*** \033[0m"
esac
done
|