【问题标题】:How can I run shell script in Windows command line?如何在 Windows 命令行中运行 shell 脚本?
【发布时间】:2019-06-07 00:58:06
【问题描述】:

我有一个用于为 Kubernetes 集群配置容器映像的 shell 脚本,如何从 Windows 10 PowerShell 运行 shell 脚本?

我有一个用于为 Kubernetes 集群配置容器映像的 shell 脚本,如何从 Windows 10 PowerShell 运行 shell 脚本?我有 Docker,并且我的 Windows 上已经安装了 Google CloudSDK。

TAG=2.2

export IMAGE_PROMETHEUS="marketplace.gcr.io/google/prometheus:${TAG}"
export IMAGE_ALERTMANAGER="marketplace.gcr.io/google/prometheus/alertmanager:${TAG}"
export IMAGE_KUBE_STATE_METRICS="marketplace.gcr.io/google/prometheus/kubestatemetrics:${TAG}"
export IMAGE_NODE_EXPORTER="marketplace.gcr.io/google/prometheus/nodeexporter:${TAG}"
export IMAGE_GRAFANA="marketplace.gcr.io/google/prometheus/grafana:${TAG}"
export IMAGE_PROMETHEUS_INIT="marketplace.gcr.io/google/prometheus/debian9:${TAG}"

    for a in "IMAGE_PROMETHEUS" \
             "IMAGE_ALERTMANAGER" \
             "IMAGE_KUBE_STATE_METRICS" \
             "IMAGE_NODE_EXPORTER" \
             "IMAGE_GRAFANA" \
             "IMAGE_PROMETHEUS_INIT"; do
      repo=$(echo ${!i} | cut -d: -f1);
      digest=$(docker pull ${!i} | sed -n -e 's/Digest: //p');
      export $i="$repo@$digest";
      env | grep $i;
    done

【问题讨论】:

标签: windows shell


【解决方案1】:

请注意标签windowsshell(POSIX shell)是互斥的。您在上面发布的既不是 windows 也不是 POSIX shell,而是 bash(或另一个支持 变量间接 的高级 shell(例如 ${!i})。

您无法从PowerShell 运行脚本。 (它不知道如何解释语法)。运行它的唯一方法是在 bash for windows(或更一般地使用 WSL)中。

为什么?每种“脚本”语言都需要和解释器。您的问题中写的内容似乎是为 bash 编写的,其中可以调用实用程序 sedgrep 以及 docker pull。检查您是否安装了 bash for windows 或使用 WSL 安装的 Linux 发行版。

如果没有提供 bash 解释器的附加软件,cmd.exePowerShell 无法解释脚本。

【讨论】:

    猜你喜欢
    • 2015-11-02
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多