简体   繁体   English

在Bash脚本中模拟TAB键按下以自动完成

[英]Emulate TAB key press in Bash script to autocomplete

I have simple Bash script running command in Kubernetes, however I need autocomplete in one command to continue: 我在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" ;

and this is where I need autocompletion as the pod name is different in each kubectl environment. 这就是我需要自动完成的地方,因为每个kubectl环境中的pod名称都不同。

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

for instance: 例如:

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

Autocomplete works when I am typing it manually, but I want Bash to autocomplete it in the script. 我手动输入时自动完成功能正常,但我希望Bash在脚本中自动完成它。 Is it possible? 可能吗?

Using interactive features of a shell in a script should be your last resort. 在脚本中使用shell的交互式功能应该是最后的选择。

We can do without: Let's first get all pod names in the namespace; 我们可以不用:让我们首先获取命名空间中的所有pod名称; then filter for your desired pod name. 然后过滤所需的广告连播名称。

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

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

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