我正在嘗試制作一個 bash 腳本來檢查一個單詞是否是我的家庭作業的回文,但我無法讓我的 while 回圈正常作業。腳本應該繼續運行并請求回文,直到用戶提供回文。有沒有更有經驗的人可以幫我解決這個問題,也許可以解釋一下我做錯了什么。
#!/bin/bash
if [ $# -le 2 ]
then
echo "Enter the word"
read input
fi
echo $input > temp
reverse=`rev temp`
echo $reverse
while [ ! $input==$reverse ]
do echo "Not a palindrome"
read input
done
echo "it is a palindrome"
rm temp
uj5u.com熱心網友回復:
這樣的事情應該做:
#!/bin/bash
set -euo pipefail;
while true; do
echo -n "Enter the word: ";
IFS=$'\n\t ' read -d$'\n' -r inputword restofsentence;
if [[ -n "${inputword}" && -z "${restofsentence}" ]] ; then
reverse="$( rev <( echo "${inputword}" ) )";
if [[ "${reverse}" == "${inputword}" ]] ; then
echo "it is a palindrome";
exit 0;
else
echo "Not a palindrome";
echo "";
echo "";
fi;
fi;
done;
$#在您的示例中,我不確定(cli 引數的數量)提供了什么功能,因為您沒有使用 cli 引數;所以我把那部分省略了。
至于它的作用:
set -euo pipefail;
有效啟用“嚴格模式”。讓 BASH 不再是一種糟糕的腳本語言。它在這個腳本中并沒有太大的影響,但是默認情況下總是在每個腳本中都有它是一個很好的做法。
while true; do
開始一個回圈并永遠重復它。
echo -n "Enter the word: ";
該-n抑制在句末通常換行符。
IFS=$'\n\t '
顯式設定內部欄位分隔符。這意味著 bash 對單詞的解釋是輸入在換行符 ( \n)、制表符 ( \t) 和空格 (' ')上“分開” ;通常這是默認值,但IFS在每個腳本的基礎上手動設定或為每個read類似命令手動設定總是好的做法。
read -d$'\n' -r inputword restofsentence;
讀取用戶輸入直至分隔符換行符 ( -d$'\n'),即回車鍵。不允許將反斜杠轉義-r用于多行輸入等內容。輸入行被拆分成基于較早的單詞IFS;之后第一個詞進入inputword,潛在的第二/第三/...-詞進入restofsentence。
if [[ -n "${inputword}" && -z "${restofsentence}" ]] ; then
我們期望inputword有一個值(非空)并且我們期望restofsentence是空的。如果不是這種情況,我們只需讓while-loop 重復并再次請求輸入。這樣我們就不會處理空輸入(因為inputword會是空的),也不會處理多詞輸入(因為restofsentence不會是空的)。
reverse="$( rev <( echo "${inputword}" ) )";
我們將其回顯inputword到一個虛擬檔案中,并將其傳遞給rev程式,這echo是反向值。我們將其存盤在變數中reverse。
我們沒有使用真正的檔案(它存盤在檔案系統中,而不是僅存盤在記憶體中)這一事實使我們不必以后清理它。
if [[ "${reverse}" == "${inputword}" ]] ; then
我們比較inputword和reverse是否相同,如果它們相同,那么我們正在處理回文。
exit 0;
一旦我們有一個成功的回文,我們就退出(退出代碼0表明沒有錯誤)。
程式(特別是while-loop)不斷重復,直到找到成功的回文。
uj5u.com熱心網友回復:
試試這個:
#!/bin/bash
while :; do
read -p "Enter a word: "
if [[ ${REPLY} == "$(rev <(printf %s "${REPLY}"))" ]]; then
echo "It is a palindrome."
break
fi
echo "Not a palindrome."
done
或者,它可以像這樣更簡單地完成,但這不是正確的,因為理論上“這是一個回文”。即使回圈中斷不是由正確的輸入而是其他錯誤引起的,仍將被發送。
#!/bin/bash
while read -p "Enter a word: "; [[ ${REPLY} != "$(rev <(printf %s "${REPLY}"))" ]]; do
echo "Not a palindrome."
done
echo "It is a palindrome"
順便說一下,它使用Process Substitution。
可以創建一個函式來反轉字串,這應該是微不足道的,但它是可選的范圍。
uj5u.com熱心網友回復:
首先讓它作業。
您想檢查一個引數。給出引數后,將其分配給輸入。參考$input避免特殊字符和空格的問題。在周圍添加空格==以使其比較并在回圈中再次計算反向。我還使用$(rev temp)了比 backtics 更好的方法。
#!/bin/bash
if [ $# -ne 1 ]
then
echo "Enter the word"
read input
else
input="$1"
fi
echo "$input" > temp
reverse=$(rev temp)
echo "$reverse"
while [ ! "$input" == "$reverse" ]
do
echo "Not a palindrome"
read input
echo "$input" > temp
reverse=$(rev temp)
done
echo "it is a palindrome"
rm temp
您可以避免這樣的 tmp 檔案:
#!/bin/bash
if [ $# -ne 1 ]
then
echo "Enter the word"
read input
else
input="$1"
fi
while [ ! "$input" == "$(rev <<< "${input}")" ]
do
echo "Not a palindrome"
read input
done
echo "it is a palindrome"
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/398287.html
下一篇:回圈遍歷串列。安卓
