脚本在elif [$ 1 =“ - h”]附近出现“意外错误”,语法正确

时间:2013-01-06 20:38:00

标签: bash parameters

尝试在终端中运行此命令时:

gerard@yoda ~$ Sources/Scripts/ultimate.sh -u -typecount -std /usr/bin py
Sources/Scripts/ultimate.sh: line 28: syntax error near unexpected token `elif'
Sources/Scripts/ultimate.sh: line 28: `elif [ $1 = "-h" ];'

这是脚本的来源:

#!/bin/bash

## $1 = Mode(u/h) ; $2 = Command(?) ; $3+ = Params

if [ $1 = "-u" ];
    if [ $2 = "-typecount" ];
    then
        if [ $3 = "-std" ];
        then
            find $4 -maxdepth 1 -iname *.$5 | wc -l
        elif [ $3 = "-ext" ];
        then
            find $5 -maxdepth $4 -iname *.$6 | wc -l
        else
            echo 'use option -std for single-dir'
            echo 'or option -def for recusive with custom depth'
            echo 'filetype should be defined  type  instead of .type'
        fi
    elif [ $2 = "-prep" ];
    then
        sed -i 's/^/$3/' $4
    elif [ $2 = "-app" ];
    then
        sed -i 's/$/$3/' $4
    else
        echo 'No command specified/Command not found'
    fi
elif [ $1 = "-h" ];
then
    if [ $2 = "-typecount" ];
    then
        echo 'for -std: [location] [type]'
        echo 'for -ext: [depth] [location] [type]'
    elif [ $2 = "-prep" ];
    then
        echo '[text] [file]'
    elif [$2 = "-app" ];
    then
        echo '[text] [file]'
    else
        echo 'No command specified/Command not found'
else
    echo 'No mode specified/Wrong mode'
fi

语法对我来说是正确的,并且脚本的更简单版本,具有相同的elif构造工作正常。我做错了什么? (解析所需函数后脚本需要退出)

1 个答案:

答案 0 :(得分:3)

在顶部,您在第一个then之后没有if个关键字。

if [ $1 = "-u" ];
**then**
    if [ $2 = "-typecount" ];

你也错过了靠近底部的fi

    else
        echo 'No command specified/Command not found'
    **fi**
else