声明-A返回无效选项使用Apple M1上的Bash版本5

发布于 2025-02-02 19:12:02 字数 1146 浏览 2 评论 0原文

我已经阅读了有关Stackoverflow的类似文章,但是没有解决我的特定问题,我真的很感谢任何帮助,因为我真的在这本书上失去了理智。

  1. 我有一个使用Shebang #!/usr/bin/env bash的脚本,我已经使用Echo在脚本内检查了bash的版本,它的确是bash版本5我通过Homebrew下载的 - 我读到我不需要使用路径/opt/homebrew/bin/bash 它是我的bash所在的位置[苹果M1 CPU] Shebang粘贴在第1点。
  2. 我的VSC终端正在使用ZSH。
  3. 由于某种原因,即使声明-a应由4后bash版本支持,但我返回以下错误: 声明:-a:无效选项声明:用法:声明[-affirtx] [-p] [-p] [name [= value] ...]
  4. 我的脚本如下:
#!/usr/bin/env bash

echo "BASH_VERSION=$BASH_VERSION"

# save current branches in packages as `composer install` may change them to master
SCRIPTPATH="$( cd "$( /usr/bin/dirname "$0" )" && pwd )"

DIR=`pwd`

declare -A BRANCHES

DIR=`pwd`

for d in */; do

    cd "$DIR/$d"
    BRANCH=`git rev-parse --abbrev-ref HEAD`

    if [ "${BRANCH}" != "master" ]; then
        BRANCHES[$d]="${BRANCH}"
    fi

done

返回的bash版本:

BASH_VERSION=5.1.16(1)-release

有建议吗?请让我知道您需要哪些其他细节。另外,也许是宣布关联数组的替代语法,也许我在初始化之前无法声明它们?

我也对此有一个问题。该脚本位于Docker目录内 - 如果Docker打算将虚拟Linux用于安装和实例 - 那么为什么我需要对我的Shell脚本进行某些MACOS支持的更改?

I have read similar articles on stackoverflow but none addressing my specific issue, I would really appreciate any help as I am really losing my mind on this one.

  1. I have a script that is using the shebang #!/usr/bin/env bash, I have checked the version of bash inside of the script by using echo and it is indeed bash version 5 that I downloaded via HomeBrew - I read that I do not need to use the path /opt/homebrew/bin/bash which is where my bash is situated [apple M1 cpu] because of the shebang pasted in point 1.
  2. My vsc terminal is using zsh.
  3. For some reason even though declare -A should be supported by bash versions after 4, I am returned the following error:
    declare: -A: invalid option declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
  4. My script is as follows:
#!/usr/bin/env bash

echo "BASH_VERSION=$BASH_VERSION"

# save current branches in packages as `composer install` may change them to master
SCRIPTPATH="$( cd "$( /usr/bin/dirname "$0" )" && pwd )"

DIR=`pwd`

declare -A BRANCHES

DIR=`pwd`

for d in */; do

    cd "$DIR/$d"
    BRANCH=`git rev-parse --abbrev-ref HEAD`

    if [ "${BRANCH}" != "master" ]; then
        BRANCHES[$d]="${BRANCH}"
    fi

done

Returned bash version:

BASH_VERSION=5.1.16(1)-release

Any advice? Please let me know what other details you require. Also, perhaps an alternative syntax to declaring associative arrays, perhaps I cannot declare them before initializing?

I have a subsequent question regarding this as well. This script sits inside of a docker directory - if docker is meant to use the virtual Linux for installations and instances - why then would I need to make certain macOS supported changes to my shell scripts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

横笛休吹塞上声 2025-02-09 19:12:02

您能否确保在本地Env-VAR路径中,所需的路径/opt/homebrew/bin位于/bin/的前面? Shebang #!/usr/bin/env bash只要求它通过路径定义的目录进行检查时,将其定位为第一个爆炸。

can you ensure that in your local env-var PATH, the desired path /opt/homebrew/bin is situated in front of /bin/? The Shebang #!/usr/bin/env bash simply asks it to locate the first bang executable when it checks through directories defined by PATH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文