简书链接:快速进入dockersh脚本
文章字数:58,阅读全文大约需要1分钟
这是我经过反复调整优化后的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/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

支持填写全名或者序号