我正在嘗試為用戶輸入構建回圈,直到我得到一個特定的輸入,例如我想在輸入 = 4 時停止回圈并列印 siiiiii 但問題是程式卡在回圈中我如何設定一個新的值回圈輸入?
#!/bin/bash
value=4
echo Enter the number:
read $input
while [ $input != $value ]
do
echo "The input must be between 1 and 4"
read input2
input = $input2
done
echo siiiiiiiiiiiiiiiiiii
uj5u.com熱心網友回復:
#!/bin/bash
value=4
echo Enter the number:
while read input; do
if [ "$input" = "$value" ]; then break; fi
echo "The input must be between 1 and 4" >&2
done
echo siiiiiiiiiiiiiiiiiii
你也可以寫:
while read input && [ "$input" != "$value" ]; do
echo "The input must be between 1 and 4" >&2
done
您可能更喜歡使用-eqand-ne因為您正在進行整數比較,因為這會給您額外的錯誤訊息。這些錯誤訊息是否有用是一個設計決策:
while read input && ! [ "$input" -eq "$value" ]; do
echo "The input must be between 1 and 4" >&2
done
您的原始代碼的 4 個主要問題是:未能參考變數、錯誤的賦值嘗試(您input = $input2不能在. 可能還有其他一些小問題,但那些跳出來了。=$read $inputwhile read varname
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/483711.html
上一篇:為一組值運行函式
