0

I am trying to set up a simple script to compile my project that takes the name of the module(s) to compile, or if no arguments, compile them all.

None of the other answers on StackOverflow have worked for me.

When I try the below script I get this error:

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

I don't like the ambiguity of the language, it could be a function or a command. But it is what it is. How can I make it execute the function and not try to run a command called 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"

I would also like help making a default case for when there are no arguments passed to the script. My attempt can be seen in the commented block in the switch case.

Appreciate any help.

Jacob Birkett
  • 1,927
  • 3
  • 24
  • 49
  • Functions *define* commands. They *are* commands in every meaningful way. If you want to explicitly call an external command and bypass any like-named function, invoke `command foo` instead of `foo`. – Charles Duffy Aug 29 '18 at 23:37

1 Answers1

1

You need to define the functions before they are called from top-level code.

...

updater () { ... }

...

while [ "$#" -gt 0 ]; do
    ...
done
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Oh. Now I just feel dumb. To be fair to myself, this isn't an issue in most other languages. – Jacob Birkett Aug 29 '18 at 23:33
  • What about the default case? – Jacob Birkett Aug 29 '18 at 23:35
  • The default case would be covered by [How can I check if no arguments are given?](https://stackoverflow.com/questions/34925224/how-can-i-check-if-no-arguments-are-given); it already has a distinct question in the knowledge base. – Charles Duffy Aug 29 '18 at 23:36
  • Languages where it isn't an issue tend not to allow code outside a function. (C, for instance, starts by calling `main`, not executing arbitrary code in a particular file.) – chepner Aug 30 '18 at 02:12