0

At first I created a script to 'find' a file and switch to that directory. Alas, upon returning from the script, the 'cd' was unchanged. Directory changes within a script are local to that script. I forgot. Sue me.

So... I created that same code as a function in the middle of .bashrc. When I re-enter the Bash shell, the function is not defined or visible. So... I placed the function at the end of .bashrc and -- voila! -- it worked. Here is the function:

function goto {

    if [[ "$1" == "" ]]
    then
        echo "[ERROR] $0 requires a filename as input."
        echo "[INFO]  Usage: $0 <filename> finds file and changes to that directory."
    else
        echo "[INFO] Looking for file: $1"
        declare -x -a full_filepath=$(find . -name "$1")
        if [[ "${full_filepath[0]}" == "" ]]
        then
            echo "[ERROR] Unable to find requested file $1. Exiting..."
        else
            local filepath=${full_filepath[0]%/*}
            local filename=${full_filepath[0]##*/}
            echo "[INFO] Switching to $filepath to locate $filename..."
            cd $filepath
        fi
    fi
}

Now here's the problem. I had to move it after SDKMan's init code in .bashrc (ignoring the warning that #THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!). Not surprisingly, 'sdk' no longer works.

Is there a "right way" to include a function in .bashrc so that other scripts like SDKMan's can remain at the end, for whatever-in-gods-name reason it must be there...???

  • Put the function definition before that comment. – Barmar Apr 16 '20 at 04:06
  • I tried that. The presence of the 'goto' function seems to cause .bashrc to skip the SDKMan init commands. The 'goto' function (yes, I regret the name) works, but the 'sdk' command is not found. – Corba the Geek Apr 16 '20 at 04:17
  • Put `set -x` at the beginning so you can see the commands as they're executing. – Barmar Apr 16 '20 at 04:19
  • A function definition shouldn't have any effect on the rest of the script. Are you sure you didn't make some other change to the script? – Barmar Apr 16 '20 at 04:20
  • 1
    The only reason the sdk stuff needs to be at the end is so that updating the package can find it to replace it. The position shouldn't matter for normal functioning. – Barmar Apr 16 '20 at 04:21
  • Guessing: Let's pretend for a second that SDKMan is doing an exec. In that case, you'll get a new shell and you won't keep your function. Try to export your function using `export -f goto`. So declare goto, export it, then run SDKMan (or whatever that is). – Mark Apr 16 '20 at 04:23
  • Offtopic: Quote `cd $filepath` so that it will work with directories containing spaces ⇒ `cd "$filepath"` – Wiimm Apr 16 '20 at 09:58

1 Answers1

0

I uninstalled then reinstalled SDKMan and the functions are now working as is SDKMan.

The conditional they add is odd. It reminds me of the shortcuts in Perl.

Here is the code added to .bashrc:

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

This works just as well:

if [[ -s "/home/peter/.sdkman/bin/sdkman-init-sh" ]]; then source "/home/peter/.sdkman/bin/sdkman-init-sh"; fi

but it's a few characters longer, I guess. And if they had used the var they just defined above it, it would be even shorter:

if [[ -s "$SDKMAN_DIR/bin/sdkman-init-sh" ]]; then source "$SDKMAN_DIR/bin/sdkman-init-sh"; fi

Barmar: You were right. Location in .bashrc doesn't matter. Thanks. Wiimm: Thanks for the tip. Mark: For good measure, I've exported the functions. Thanks.