How to use SDKMAN! to install packages from within scripts

4.2k views Asked by At

Having SDKMAN! installed (http://sdkman.io/), I can install packages from the command line using for example:

sdk install java 8u144-zulu

However, when I try to do the same thing from within a script "my-installer.sh", I get error message: "sdk: command not found".

my-installer.sh:

#!/bin/bash
sdk install java 8u144-zulu

What am I doing wrong?

2

There are 2 answers

0
weHe On BEST ANSWER

you need to source in ~/.sdkman/bin/sdkman-init.sh, like in

#!/bin/bash
. /home/alexw/.sdkman/bin/sdkman-init.sh
sdk install java 8u144-zulu

'sdk' is a bash function declared in sdkman-init.sh, and your first line (#!...) starts a new(!) shell.

hope that helps! weHe

0
Powers On

Here's how to source sdkman-init.sh more generically:

source "$HOME/.sdkman/bin/sdkman-init.sh"

This is similar to the code that SDKMAN adds to your .zshrc file:

#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="/Users/powers/.sdkman"
[[ -s "/Users/powers/.sdkman/bin/sdkman-init.sh" ]] && source "/Users/powers/.sdkman/bin/sdkman-init.sh"

Type which sdk if you'd like to see the function that's being sourced:

sdk () {
    COMMAND="$1"
    QUALIFIER="$2"
    case "$COMMAND" in
        (l) COMMAND="list"  ;;
        (ls) COMMAND="list"  ;;
        (v) COMMAND="version"  ;;
        (u) COMMAND="use"  ;;
...
...