一个简单的shell程序: app.sh

#!/bin/bash

action=$1
version=$2
app_name=shuqi-public-${version}

load()
{
  file_name=shuqi-public-${version}.tar
  docker load -i data/${file_name}
}

clean()
{
  old_container=`docker container ls | grep ${app_name}`
  if [ -n "${old_container}" ]; then
    echo "程序运行中,请先停止程序"
    exit;
  fi
  docker rm ${app_name}  
}

start()
{
  old_container=`docker container ls | grep ${app_name}`
  if [ -n "${old_container}" ]; then
    echo "程序已经在运行,请先停止"
    exit;
  fi
  ip=`ip a | grep -v docker0 |grep inet|grep -v 127.0.0.1| grep -v inet6|awk '{print $2}'|tr -d "addr:" | awk -F '/' '{print $1}'`
  account_url=${ip}
  echo "启动程序${app_name}..."
  docker run --privileged --name shuqi-public-${version} -dit -e account_url=${account_url} -p80:80 -p3306 shuqi-public:${version} /usr/sbin/init  && docker exec -it shuqi-public-${version} sh /root/config.sh && docker exec -it shuqi-public-${version} sh /root/init.sh
  echo "启动中..."
  sleep 100
  echo "启动完成"

}

stop()
{
  old_container=`docker container ls | grep ${app_name}`
  if [ ! -n "${old_container}" ]; then
    echo "程序不存在,请先启动程序"
    exit;
  fi
  docker stop ${app_name}
}

restart()
{
  old_container=`docker container ls | grep ${app_name}`
  if [ ! -n "${old_container}" ]; then
    echo "程序不存在,请先启动程序"
    exit;
  fi
  docker restart ${app_name}
}

if [ ! "${action}" ] ;then
    echo "参数是 start、stop、restart"
    exit;
fi

if [ ! -n "${version}" ]; then
  echo "请输入版本号"
  exit;
fi

echo "程序名: ${app_name}"
echo "操作: ${action}"
echo "版本: ${version}"

if [ "${action}" = "start" ] ;then
  start
elif [ "${action}" = "stop" ] ;then
  stop
elif [ "${action}" = "restart" ] ;then
  restart
elif [ "${action}" = "load" ] ;then
  load
elif [ "${action}" = "clean" ]; then
  clean
else
  echo "command error"
fi

progress()
{
  printf "\e[?25l" 
  i=0;
  str=""
  arr=("|" "/" "-" "\\")
  while [ $i -le 100 ]
  do
    let index=i%4
    let indexcolor=i%8
    let color=30+indexcolor
    #打印进度
    printf "\r\e[0;$color;1m[%-100s][%d%%]%c\e[0m" "$str" "$i" "${arr[$index]}"
    sleep 0.1
    let i++
    str+='='
  done
  #显示光标
  wait
  printf "\e[?25h""\n" 
}

progress