# 1. 建立目錄
cd $HOME && mkdir -P .mine/bin && cd .mine/bin && touch d-use
gedit d-use # 將下面內容複製到d-use中
#2. 加入環境變數
vim ~/.bashrc
export PATH=.mine/bin:$PATH #將.mine/bin目錄加入環境變數
source ~/.bashrc
# 然後就可以使用d-use 命令
d-use 容器號/容器名 #(不用全部名稱,唯一字首就行)
d-use # 也可以直接使用d-use
#!/bin/bash
# ANSI顏色程式碼
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
SKY_BLUE='\033[0;36m'
NC='\033[0m' # 恢復預設顏色
# 獲取所有容器ID
c_ids=$(docker ps -aq)
c_id_arr=()
c_name_arr=()
c_status_arr=()
id_max_len=0
name_max_len=0
status_max_len=0
idx=0
echo -e "${PURPLE}[Tip] 當前版本命令僅支援對[0-9]編號的容器操作(刪除操作除外)${NC}"
# 遍歷每個容器ID,獲取容器資訊
for id in $c_ids; do
c_name=$(docker inspect --format='{{.Name}}' $id | sed 's/^\///')
c_status=$(docker inspect --format='{{.State.Status}}' $id)
c_id_arr[idx]=$id
c_name_arr[idx]=$c_name
c_status_arr[idx]=$c_status
((idx++))
c_name_len=${#c_name}
c_status_len=${#c_status}
if [ $name_max_len -lt $c_name_len ]; then
name_max_len=$c_name_len
fi
if [ $status_max_len -lt $c_status_len ]; then
status_max_len=$c_status_len
fi
done
choose_num=-1
c_count=0
if [ $# -eq 1 ]; then
arg1=$1
# 遍歷id
for ((i=0; i < idx; i++)); do
val1="${c_id_arr[$i]}"
if [[ "$val1" == "$arg1"* ]]; then
choose_num=$i
((c_count++))
fi
done
if [ $c_count -gt 1 ]; then
echo -e "${GREEN}>${NC} ${RED}存在多個容器與${NC}${YELLOW} [${arg1}]${NC} ${RED}匹配!${NC}"
exit
elif [ $c_count -eq 0 ]; then
# 變數名字
for ((i=0; i < idx; i++)); do
val1="${c_name_arr[$i]}"
if [[ "$val1" == "$arg1"* ]]; then
#echo "$val1=$arg1"
choose_num=$i
((c_count++))
fi
done
if [[ $choose_num -eq -1 ]]; then
echo -e "${GREEN}>${NC} ${RED}選擇的容器[$arg1]不存在!${NC}"
fi
fi
fi
while [ $choose_num -lt 0 ]; do
echo -e "${GREEN}>${NC} 請輸入需要操作的容器編號${SKY_BLUE}(按 [q] 退出,按 [d] 進入容器刪除):${NC}"
# 列印容器列表
for ((i=0; i < idx; i++)); do
id_str="${c_id_arr[i]}"
# name
arr_len=${#c_name_arr[i]}
num=$((name_max_len-$arr_len))
spaces_string=$(printf '%*s' $num '')
name_str="${c_name_arr[i]}$spaces_string"
# status
arr_len=${#c_status_arr[i]}
num=$((status_max_len-$arr_len))
spaces_string=$(printf '%*s' $num '')
status_str="${c_status_arr[i]}$spaces_string"
# final
echo -e "${YELLOW}[$i] $id_str $name_str $status_str${NC}"
done
read -n 1 user_input
echo ""
if [ "$user_input" == "q" ]; then
exit
fi
# 刪除容器操作
if [ "$user_input" == "d" ]; then
echo -e "${GREEN}>${NC} 請選擇需要刪除的容器的編號, 以空格分隔:"
read -r delete_str
IFS=' ' read -r -a delete_array <<< "$delete_str"
# 檢測輸入值是否合法
for item in "${delete_array[@]}"; do
# 判斷編號合法性
if [[ $item =~ ^[0-9]+$ ]]; then
if ((item < 0 || item >= idx)); then
echo -e "${GREEN}>${NC} ${RED}輸入編號${NC} ${YELLOW}[${item}]${NC} ${RED}超出範圍!${NC}"
exit
fi
else
echo -e "${GREEN}>${NC} ${RED}輸入編號${NC} ${YELLOW}[${item}]${NC} ${RED}非數字!${NC}"
exit
fi
done
# 列印待刪除的容器列表
echo -e "${GREEN}>${NC} 你要刪除的容器如下:"
for item in "${delete_array[@]}"; do
echo -e "容器名: ${YELLOW}[${c_name_arr[$item]}]${NC} ID: ${YELLOW}[${c_id_arr[$item]}]${NC}"
done
while true; do
echo -e "${GREEN}>${NC} ${RED}確定刪除以上容器?${NC}, ${SKY_BLUE}按 [y] 確認,按 [q] 取消並退出${NC}"
read -n 1 ret
if [ "$ret" == "q" ]; then
exit
elif [ "$ret" == 'y' ]; then
for item in "${delete_array[@]}"; do
docker stop ${c_name_arr[$item]} && docker rm ${c_name_arr[$item]}
done
echo -e "${GREEN}>${NC} 刪除完成"
exit
fi
echo -e "${GREEN}>${NC} ${RED}輸入非法命令,請重新輸入!${NC}"
done
fi
if ! [[ $user_input =~ ^[0-9]+$ ]]; then
echo -e "${GREEN}>${NC} ${RED}輸入不合法, 請重新輸入!${NC}"
elif [ $user_input -ge 0 ] && [ $user_input -lt $idx ]; then
choose_num=$user_input
break
else
echo -e "${GREEN}>${NC} ${RED}輸入序號不合法,請重新輸入!${NC}"
fi
echo ""
done
choose_id=${c_id_arr[$choose_num]}
choose_name=${c_name_arr[$choose_num]}
choose_status=${c_status_arr[$choose_num]}
while true
do
echo -e "${GREEN}>${NC} 請選擇對 ${GREEN}[$choose_name]${NC} $choose_id 的操作-> ${SKY_BLUE}進入(e) 重啟(r) 關閉(c) 退出(q):${NC}"
read -n 1 c_opt
echo ""
if [ "$c_opt" == "q" ]; then
exit
elif [ "$c_opt" == "e" ]; then
if [ "$choose_status" == "exited" ]; then
docker start $choose_id
fi
docker exec -it $choose_id /bin/bash
break
elif [ "$c_opt" == "r" ]; then
docker restart $choose_id
break
elif [ "$c_opt" == "c" ]; then
docker stop $choose_id
break
else
echo -e "${GREEN}>${NC} ${RED}輸入非法指令,請重新輸入${NC}"
fi
done