feat: 支持通过命令行参数指定Docker镜像关键字

- 修改脚本以接受命令行参数作为搜索关键词
- 如果没有提供参数,脚本将提示用户输入搜索关键词
- 根据镜像名称查找匹配的容器
- 如果只找到一个匹配的容器,直接进入该容器的交互式终端
- 如果找到多个匹配的容器,显示匹配的容器列表并提示用户选择一个容器ID
This commit is contained in:
outmanwt 2024-06-13 14:10:33 +08:00 committed by GitHub
parent 2a7548edaa
commit d0edba552a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,7 +1,13 @@
#!/bin/bash #!/bin/bash
# 提示用户输入搜索关键词 # 检查是否提供了命令行参数
read -p "请输入搜索的Docker镜像关键字: " keyword if [ $# -eq 0 ]; then
# 如果没有提供参数,提示用户输入搜索关键词
read -p "请输入搜索的Docker镜像关键字: " keyword
else
# 如果提供了参数,使用第一个参数作为搜索关键词
keyword=$1
fi
# 查找匹配关键词的容器 # 查找匹配关键词的容器
containers=$(docker ps --format "{{.ID}} {{.Image}} {{.Names}}" | grep "$keyword") containers=$(docker ps --format "{{.ID}} {{.Image}} {{.Names}}" | grep "$keyword")
@ -37,4 +43,3 @@ else
# 进入选定的容器 # 进入选定的容器
docker exec -it $container_id /bin/bash docker exec -it $container_id /bin/bash
fi fi