在意外标记'else'附近的语法错误
我已经看了大约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'
我只检查你的语法,发现这些错误。
- 函数调用。正如@Etan Reisner所说。
- 您需要比较运算符周围的空格。像
[ $param == '-h' ]
; - 你需要双引号你的变量。使用
[ "$param" == '-h' ]
而不是[ $param == '-h' ]
。检查这个更多细节。 Double quote to prevent globbing and word splitting
我建议你在这里测试你的脚本。 http://www.shellcheck.net/我也应该先做,而不是手动检查你的脚本。
现在我得到一个错误,如果我运行默认没有参数'./countf.sh:行35:[:==:一元运算符预计 '和'./countf.sh:行37:[:==:一元运算符预期 ' – 2015-01-20 22:12:13
当我运行它时,它是完全正常的。什么是错误? – qqibrow 2015-01-20 22:15:18
对不起,新的堆栈溢出和学习格式,我编辑我的评论以上 – 2015-01-20 22:16:46
您不会将shell函数作为default()调用,您称它们为“default”。 – 2015-01-20 21:54:01
谢谢你解决了这个问题。现在谈谈我的其他问题。 – 2015-01-20 21:59:49
这里的正确形式是一个问题的一个问题,并且在该问题中没有比它涵盖的单个问题更多的代码。另请参阅http://stackoverflow.com/help/mcve – 2015-01-20 22:24:09