Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SDKMAN! to install packages from within scripts

Tags:

bash

sh

sdk

sdkman

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?

like image 348
Alexander Weickmann Avatar asked Sep 17 '17 09:09

Alexander Weickmann


People also ask

Where does SDKMAN Install?

The installed SDKs are stored in the SDKMAN! directory which defaults to ~/. sdkman/candidates.

What is SDKMAN java?

SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix based systems. It provides a convenient Command Line Interface (CLI) and API for installing, switching, removing and listing Candidates.


2 Answers

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

like image 66
weHe Avatar answered Sep 22 '22 14:09

weHe


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"  ;;
...
...
like image 35
Powers Avatar answered Sep 22 '22 14:09

Powers