快速进入 docker sh 脚本

2024-08-09  本文已影响0人  吉凶以情迁

这是我经过反复调整优化后的脚本

#!/bin/sh

# Function to display containers with numbering
display_containers() {
  containers=$(docker ps --format "{{.ID}} {{.Names}}")
  echo "Please select a container (enter the number):"
  echo "$containers" | awk '{print NR")",$0}'
}

# Display containers
display_containers

# Read user input
read -p "Enter the number of the container: " input

# Get container ID based on user input
container_name=$(docker ps --format "{{.Names}}" | sed "${input}q;d")

# Check if container name is empty (invalid input)
if [ -z "$container_name" ]; then
  echo "Invalid number or container not found"
  exit 1
fi

# Print the selected container name
echo "Selected container: $container_name"

# Enter the specified container's terminal
docker exec -it "$container_name" /bin/sh
image.png

支持填写全名或者序号

上一篇下一篇

猜你喜欢

热点阅读