2015-01-20 100 views
-1

我已經看了大約30分鐘了,現在似乎無法找到這個錯誤。它發生在我最後的if/else塊中。在意外標記'else'附近的語法錯誤

default() 
{ 
for file in /* 
do 
     if [ -f $file ]; then 
       ((filecount++)) 
     elif [ -d $file ]; then 
       ((dircount++)) 
     fi 
done 
echo The number of files is "$filecount" 
echo The number of directories is "$dircount" 
} 
specific() 
{ 
for file in $param 
do 
     if [ -f $file ]; then 
       ((filecount++)) 
     elif [ -d $file ]; then 
       ((dircount++)) 
     fi 
done 
echo The number of files is "$filecount" 
echo The number of directories is "$dircount" 
} 
#Variables 
declare -a param=$1 
declare -i filecount="0" 
declare -i dircount="0" 
#Action 
if [ $param=='-h' ]; then 
     echo To use this program, enter a directory path after $0 or leave it blank to use current directory. 
elif [ $param=='' ]; then 
     default() 
else 
     specific() 
fi 
exit 0 

這裏是錯誤代碼。任何幫助表示讚賞。

./countf.sh: line 44: syntax error near unexpected token `else' 
./countf.sh: line 44: `else' 
+1

您不會將shell函數作爲default()調用,您稱它們爲「default」。 – 2015-01-20 21:54:01

+0

謝謝你解決了這個問題。現在談談我的其他問題。 – 2015-01-20 21:59:49

+0

這裏的正確形式是一個問題的一個問題,並且在該問題中沒有比它涵蓋的單個問題更多的代碼。另請參閱http://stackoverflow.com/help/mcve – 2015-01-20 22:24:09

回答

0

我只檢查你的語法,發現這些錯誤。

  1. 函數調用。正如@Etan Reisner所說。
  2. 您需要比較運算符周圍的空格。像[ $param == '-h' ];
  3. 你需要雙引號你的變量。使用[ "$param" == '-h' ]而不是[ $param == '-h' ]。檢查這個更多細節。 Double quote to prevent globbing and word splitting

我建議你在這裏測試你的腳本。 http://www.shellcheck.net/我也應該先做,而不是手動檢查你的腳本。

+0

現在我得到一個錯誤,如果我運行默認沒有參數'./countf.sh:行35:[:==:一元運算符預計 '和'./countf.sh:行37:[:==:一元運算符預期 ' – 2015-01-20 22:12:13

+0

當我運行它時,它是完全正常的。什麼是錯誤? – qqibrow 2015-01-20 22:15:18

+0

對不起,新的堆棧溢出和學習格式,我編輯我的評論以上 – 2015-01-20 22:16:46

相關問題