脚本有待优化,由于时间问题,暂时先搁置优化,后期会慢慢再次优化到可以以每个容器或者images来操作!
#! /bin/bash
##start,stop,delete the docker containers
##written by zhdya_20170914
list=`docker ps -a |awk '{print $2}'| grep -v 'ID'`
echo "============================================================================"
echo -e "pls check the follow list of container: \n$list"
read -p "pls choose an action which you want!<1.start 2.stop 3.rm 4.rmi > " act
echo "============================================================================"
echo -e "stop\nstart\nrm\nrmi" > /tmp/docker.txt
##judge if input the words or not!
if [ -z $act ]
then
echo "you type was wrong,pls just input "start"."stop"."rm"."rmi"."
exit
fi
##judge if input a wrong words!!
if grep -wq $act /tmp/docker.txt
then
case $act in
start)
docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)
echo "already start all of docker containers,pls checking it.."
;;
stop)
docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)
echo "already stop all of docker containers,pls checking it.."
;;
rm)
docker rm $(docker ps -a | awk '{ print $1}' | tail -n +2)
echo "already remove all of docker containers,pls checking it.."
;;
*)
docker rmi $(docker images | awk '{print $3}' |tail -n +2)
echo "already remove all of docker images,pls checking it.."
esac
else
echo "you type was wrong,pls just input "start"."stop"."rm"."rmi"."
fi
复制