以下是一段shell代碼,主要是為了簡化trans指令,并且可以選擇將輸出內容輸入到檔案中
read word
content=$(trans zh:en $word)
echo “$content”
read option
if [ $option='y' ];then
result=${content//m/}
result=${result//[0-9]/}
result=${result//[/}
result=${result//]/}
echo "$result" >> ~/Documents/trans_history
echo -e "\n" >> ~/Documents/trans_history
exit 0
else
exit 0
fi
終端輸出如下

輸入到檔案中有亂碼,以下是過濾掉部分亂碼字符后的結果。仍有一個亂碼字符^[。

我感到困惑的是^[是一個字符,無論是過濾^還是[都不行。且該字符在neovim、nano等編輯器中存在,也可以通過編輯器的搜索功能搜索^字符查到,但用普通文本編輯器打開并不存在
此外,還想請教shell代碼中正則運算式的或符號的使用。我已嘗試了result=${result//[ | ]/}、result=${result//[|]/}、result=${result//[\|]/}、result=${result//[\\|]/}等,結果均不正確
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/25901.html
標籤:新手樂園
上一篇:求大神幫忙,編輯一個程式
下一篇:為什么我的答案對了卻wa
