在bash中调用函数时“找不到命令”

时间:2018-08-29 23:28:27

标签: bash shell sh

我正在尝试建立一个简单的脚本来编译我的项目,该项目采用要编译的模块名称,或者如果没有参数,则全部编译它们。

StackOverflow上的其他答案中没有一个对我有用。

尝试以下脚本时出现此错误:

$ ./build.sh updater
Creating the build path: build/0.1.4-master
./build.sh: line 21: updater: command not found

我不喜欢这种语言的歧义,它可能是函数或命令。但是它就是这样啊。如何使它执行功能而不尝试运行名为updater的命令?

#! /bin/sh -e

# release="0.0.0-$(git rev-parse --abbrev-ref HEAD)"
release="$(git describe --tags --abbrev=0)-$(git rev-parse --abbrev-ref HEAD)"
echo "Creating the build path: build/$release"
mkdir -p "build/$release"

while [ "$#" -gt 0 ]; do
param="$1"

case $param in
    s|setup)
        setup
        shift
    ;;
    l|launcher)
        launcher
        shift
    ;;
    u|updater)
        updater
        shift
    ;;
    d|deflector)
        deflector
        shift
    ;;
    # *)
    #     setup
    #     launcher
    #     updater
    #     deflector
    #     shift
    # ;;
esac
done

function setup {
    echo "Compiling executable: build/$release/setup.exe"
    ldc2 "source/setup.d" "source/common.d" -of="build/$release/setup.exe" \
        -O3 -ffast-math -release -g

    echo "Adding icon to executable: build/$release/setup.exe"
    [ -e "build/$release/setup.exe" ] && \
        rcedit "build/$release/setup.exe" --set-icon "icons/icon.ico"
}

function launcher {
    echo "Compiling executable: build/$release/launcher.exe"
    ldc2 "source/launcher.d" "source/common.d" -of="build/$release/launcher.exe" \
        -O3 -ffast-math -release -g

    echo "Adding icon to executable: build/$release/launcher.exe"
    [ -e "build/$release/launcher.exe" ] && \
        rcedit "build/$release/launcher.exe" --set-icon "icons/icon.ico"
}

function updater {
    echo "Compiling executable: build/$release/updater.exe"
    ldc2 "source/updater.d" "source/common.d" -of="build/$release/updater.exe" \
        -O3 -ffast-math -release -g
}

function deflector {
    echo "Compiling executable: build/$release/deflector.exe"
    ldc2 "source/deflector.d" "source/common.d" -of="build/$release/deflector.exe" \
        -O3 -ffast-math -release -g
}

echo "Removing residual object files."
find "build/$release" -name "*.obj" -delete

echo "Copying engines file: build/$release/engines.txt"
cp "engines.txt" "build/$release"
echo "Copying libcurl library: build/$release/libcurl.dll"
cp $(which libcurl.dll) "build/$release"

我也希望帮助为没有参数传递给脚本的情况提供默认情况。我的尝试可以在切换案例的注释框中看到。

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您需要先定义函数 ,然后再从顶级代码中调用它们。

...

updater () { ... }

...

while [ "$#" -gt 0 ]; do
    ...
done