-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-exec
More file actions
executable file
·37 lines (30 loc) · 998 Bytes
/
Copy pathdocker-exec
File metadata and controls
executable file
·37 lines (30 loc) · 998 Bytes
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
30
31
32
33
34
35
36
37
#!/usr/bin/env bash
errorTooManyArguments="Input Error: too many arguments given." #1
errorDockerNameNotGiven="Input Error: container name not given." #2
errorCommandNotGiven="Input Error: command not given." #3
errorDockerNameNotFound="Docker Error: container name not found." #4
errorDockerNameNotSpecific="Docker Error: found multiple containers containing given name, be more specific." #5
#input check
if [ $# -eq 0 ]; then
echo $errorDockerNameNotGiven
exit 2
elif [ $# -eq 1 ]; then
echo $errorDockerNameNotGiven
exit 3
elif [ $# -gt 2 ]; then
echo $errorTooManyArguments
exit 1
fi
#grab conatiner id
containerId=$(docker ps -qf name=${1})
#check if container name exists
if [ -z $(echo -e ${containerId} | tr -d "[:space:]") ]; then
echo $errorDockerNameNotFound
exit 4
fi
#check if container name was too general
if [ $(echo ${containerId} | grep -o " " | wc -l) -gt 1 ]; then
echo $errorDockerNameNotSpecific
exit 5
fi
docker exec -it $containerId sh -c "${2}"