# 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
标签:脚本,容器,arr,name,NC,echo,status,docker,id
From: https://www.cnblogs.com/aliving/p/18322212