简体   繁体   English

.bashrc 末尾的 'source' 语句和 'function' 声明相互干扰

[英]'source' statement and 'function' declaration at end of .bashrc interfere with one another

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.唉,从脚本返回后,“cd”没有改变。 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.所以...我在 .bashrc 的中间创建了与 function 相同的代码。 When I re-enter the Bash shell, the function is not defined or visible.当我重新输入 Bash shell 时,function 未定义或可见。 So... I placed the function at the end of.bashrc and -- voila.所以...我将 function 放在 .bashrc 的末尾,然后 -- 瞧。 -- 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!!! ).我不得不在 SDKMan 的 init 代码 in.bashrc之后移动它(忽略#THIS 必须在 SDKMAN 工作的文件末尾的警告!!! )。 Not surprisingly, 'sdk' no longer works.毫不奇怪,“sdk”不再有效。

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...???是否有“正确的方法”在.bashrc 中包含 function 以便其他脚本(如 SDKMan 的)可以保留在最后,无论出于什么原因,它必须在那里......???

I uninstalled then reinstalled SDKMan and the functions are now working as is SDKMan.我卸载然后重新安装了SDKMan ,这些功能现在可以像 SDKMan 一样工作。

The conditional they add is odd.他们添加的条件很奇怪。 It reminds me of the shortcuts in Perl.它让我想起了 Perl 中的快捷方式。

Here is the code added to.bashrc:这是添加到 .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:如果他们使用了他们刚刚在上面定义的 var,它会更短:

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. .bashrc 中的位置无关紧要。 Thanks.谢谢。 Wiimm: Thanks for the tip.威姆:谢谢你的提示。 Mark: For good measure, I've exported the functions.马克:为了更好的衡量,我已经导出了函数。 Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM