Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 意外标记附近的语法错误';其他';_Bash - Fatal编程技术网

Bash 意外标记附近的语法错误';其他';

Bash 意外标记附近的语法错误';其他';,bash,Bash,我已经看了大约30分钟了,似乎找不到其中的错误。它发生在末尾的if/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

我已经看了大约30分钟了,似乎找不到其中的错误。它发生在末尾的if/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']
    。查看此项了解更多详细信息
    我建议你在这里测试你的脚本。我也应该先这样做,而不是手动检查脚本。

    您不能将shell函数称为
    default()
    您可以将它们称为
    default
    。谢谢,这样就解决了问题。现在我来谈谈我的其他问题。正确的形式是一个问题对一个问题,在这个问题上没有比它所涵盖的单个问题更多的代码。另请参见,我建议在请求人工审查之前,先运行代码并修复发现的问题。通读也不会有什么坏处。@CharlesDuffy不能再同意了。现在,如果我在没有参数的情况下运行default,我会遇到一个错误
    /countf.sh:line 35:[:==:预期的一元运算符
    /countf.sh:line 37:[:==:一元运算符应为
    当我运行它时,它完全正常。错误是什么?很抱歉,新的堆栈溢出和学习格式,我在上面编辑了我的评论。哦。很抱歉,我错过了错误。你需要双引号引用变量。我编辑了答案另一个这里没有提到的问题--
    =
    是bash扩展;POSIX只需要支持
    =
    ,而不是
    =