1
+ #! /bin/bash
2
+
3
+ # **
4
+ # docker exec Shortcut
5
+ # Version 0.0.1
6
+ #
7
+ # Make quick adjustments via Docker exec without having to type the entire
8
+ # "docker exec -it CONTAINERNAME /bin/bash" command into the CLI when needed.
9
+ #
10
+ # Documentation: https://github.com/disisto/docker-exec-shortcut
11
+ #
12
+ #
13
+ # Licensed under MIT (https://github.com/disisto/docker-exec-shortcut/blob/main/LICENSE)
14
+ #
15
+ # Copyright (c) 2023 Roberto Di Sisto
16
+ #
17
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
18
+ # of this software and associated documentation files (the "Software"), to deal
19
+ # in the Software without restriction, including without limitation the rights
20
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21
+ # copies of the Software, and to permit persons to whom the Software is
22
+ # furnished to do so, subject to the following conditions:
23
+ #
24
+ # The above copyright notice and this permission notice shall be included in all
25
+ # copies or substantial portions of the Software.
26
+ #
27
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33
+ # SOFTWARE.
34
+ # #/
35
+
36
+ # Create a list of running Docker containers
37
+ docker_ps_output=$( docker ps --format " {{.Names}}" )
38
+
39
+ # Check if a container name was provided as an argument
40
+ if [ $# -eq 1 ]; then
41
+ selected_container=" $1 "
42
+ if echo " $docker_ps_output " | grep -wq " $selected_container " ; then
43
+ echo " Selected container: $selected_container "
44
+ docker exec -it " $selected_container " /bin/bash
45
+ else
46
+ echo " The specified container '$selected_container ' is not in the list of running containers."
47
+ fi
48
+ else
49
+ # Enumerate the containers in the list
50
+ container_list=($docker_ps_output )
51
+ num=1
52
+ for container_name in " ${container_list[@]} " ; do
53
+ echo " $num . $container_name "
54
+ num=$(( num+ 1 ))
55
+ done
56
+
57
+ # Read the selected number from user input
58
+ read -p " Choose a number to execute the corresponding container: " choice
59
+
60
+ # Ensure the choice is a valid number
61
+ if [[ " $choice " =~ ^[0-9]+$ ]] && [ " $choice " -gt 0 ] && [ " $choice " -le ${# container_list[@]} ]; then
62
+ selected_container=" ${container_list[$((choice-1))]} "
63
+ echo " Selected container: $selected_container "
64
+ docker exec -it " $selected_container " /bin/bash
65
+ else
66
+ echo " Invalid choice."
67
+ fi
68
+ fi
0 commit comments