【问题标题】:Emulate TAB key press in Bash script to autocomplete在 Bash 脚本中模拟 TAB 键以自动完成
【发布时间】:2019-06-20 11:03:14
【问题描述】:

我在 Kubernetes 中有简单的 Bash 脚本运行命令,但是我需要在一个命令中自动完成才能继续:

#!/bin/bash
DATE=$(date +%Y-%m-%d_%H:%M:%S)
printf "Available Kubectl contexts:\n\n"
kubectl config get-contexts -o=name | sort -n
printf "%s\n"
echo -ne "Select Kubectl context: "; read KUBE_CONTEXT
for I in $KUBE_CONTEXT ; do
    kubectl config use-context $KUBE_CONTEXT
done
echo -ne "Path to file containing Job ID list: "; read -e JOB_ID_LIST
printf "%s\n"
echo "Setting port forwarding to Prometheus POD in $KUBE_CONTEXT" ;

这是我需要自动补全的地方,因为每个 kubectl 环境中的 pod 名称都不同。

kubectl port-forward -n prometheus prometheus-prometheus-RANDOM_TEXT-RANDOM_TEXT 20001:9090

例如:

kubectl port-forward -n prometheus prometheus-prometheus-6465c4df4c-4dvf7 8080:9090 &

自动完成在我手动输入时有效,但我希望 Bash 在脚本中自动完成它。有可能吗?

【问题讨论】:

标签: bash tab-completion


【解决方案1】:

在脚本中使用 shell 的交互功能应该是你最后的手段。

我们可以不用: 我们首先获取命名空间中的所有 pod 名称;然后过滤您想要的 pod 名称。

podName=$(kubectl get pods -n prometheus -o name | grep "^pod/prometheus-prometheus-" | cut -d/ -f2)

【讨论】:

    猜你喜欢
    • 2012-12-20
    • 1970-01-01
    • 2012-03-09
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 2020-11-25
    • 2018-10-18
    相关资源
    最近更新 更多