我只是在學習終端命令,我正在嘗試創建自己的命令,但是我遇到了以下問題
./myFile: line 10: syntax error in conditional expression
./myFile: line 11: syntax error near `then'
./myFile: line 11: ` then'
有我的代碼
#!/bin/bash
echo "please enter name of the folder"
read NAME1
if [[ $NAME1 ]]
then
echo "enter first number"
read NUM1
if [[ $NUM1 ]] && [[ $NUM1 -ge 0]]
then
echo "enter last number"
read NUM2
if [[ $NUM2 ]] && [[ $NUM2 -gt $NUM ]]
then
mkdir $NAME1{$NUM1..$NUM2}
else
echo"please enter last number to continue"
fi
else
echo "please enter first number to continue"
fi
else
echo "please enter name of the folder to continue"
fi
uj5u.com熱心網友回復:
首先,這個運算式[[ $NAME1 ]]是無效的,或者至少,它沒有做你認為它做的事情。我相信您正在嘗試確定用戶是否實際輸入了字串。為此,您應該測驗非空字串 ( [[ -n ${NAME1} ]]) 或測驗長度大于零的字串 ( [[ ${#NAME1} -gt 0 ]])。這同樣適用于測驗$NUM1和$NUM2.
其次,在處理用戶輸入時,應注意避免測驗空字串。這最好通過參考您的變數來實作。例如:[[ "${NUM1}" -gt 0 ]]。
第三,空間在測驗中很重要。[[在 之后和之前總是留一個空格]]。
此外,$NUM(第 15 行)未宣告,因此將評估為空字串。這應該在腳本中較早的地方設定。
此腳本還有許多其他方面可以改進(例如檢查數字輸入、檢查有效檔案夾名稱等。但上述更改應該可以幫助您克服直接錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/474247.html
標籤:重击
