我想的inputLineNumber將值設置爲20,如果沒有值由用戶給出由我試圖檢查[[-z「$ inputLineNumber」]],然後通過inputLineNumber = 20設定值。該代碼給出了這條消息./t.sh:[-z:找不到作爲控制檯上的消息。如何解決這個問題?這是我的完整腳本。如何在Unix中輸入錯誤/無效輸入時設置默認值?
#!/bin/sh
cat /dev/null>copy.txt
echo "Please enter the sentence you want to search:"
read "inputVar"
echo "Please enter the name of the file in which you want to search:"
read "inputFileName"
echo "Please enter the number of lines you want to copy:"
read "inputLineNumber"
[[-z "$inputLineNumber"]] || inputLineNumber=20
for N in `grep -n $inputVar $inputFileName | cut -d ":" -f1`
do
LIMIT=`expr $N + $inputLineNumber`
sed -n $N,${LIMIT}p $inputFileName >> copy.txt
echo "-----------------------" >> copy.txt
done
cat copy.txt
從@Kevin建議後改劇本。現在,該錯誤信息./t.sh:在第11行語法錯誤:`$」意外
#!/bin/sh
truncate copy.txt
echo "Please enter the sentence you want to search:"
read inputVar
echo "Please enter the name of the file in which you want to search:"
read inputFileName
echo Please enter the number of lines you want to copy:
read inputLineNumber
[ -z "$inputLineNumber" ] || inputLineNumber=20
for N in $(grep -n $inputVar $inputFileName | cut -d ":" -f1)
do
LIMIT=$((N+inputLineNumber))
sed -n $N,${LIMIT}p $inputFileName >> copy.txt
echo "-----------------------" >> copy.txt
done
cat copy.txt
這不起作用。同樣的錯誤:( – user2889968