我一生都无法弄清楚为什么会失败,所以希望这里有人能对此有所启发:
#!/bin/sh
NAME=Container
if [ -d "/mnt/SSD/docker" ]; then
if [ ! -d "/mnt/SSD/docker/$NAME/data" ]; then
mkdir -p /mnt/SSD/docker/$NAME/data
fi
printf "\nPlease select installation method:\n\n"
while true; do
read -p "1. Re-create existing container
2. Create new container: " CHOICE
case $CHOICE in
1 )
if [ "$(docker ps -a | grep $NAME)" ]; then
IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $NAME)
MAC=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' $NAME)
docker stop $NAME; docker rm $NAME; docker image rm repo/image
break;;
else
printf "\nNo existing container found, going back to menu!\n\n";;
fi
2 )
read -p 'Please enter the IP-address to use: ' IP
read -p 'Please enter the MAC-address to use: ' MAC
break;;
* )
printf "\nIncorrect or no selection made. Please enter 1 or 2.\n\n";;
esac
done
docker run -d \
-e JVM_HEAP=1024m -e TZ=Europe/Stockholm \
--hostname=$NAME \
--ip=$IP \
--mac-address=$MAC \
--name=$NAME --network=macvlan \
--restart=always \
-v /mnt/SSD/docker/$NAME/data:/$NAME/data \
-v /mnt/HDD/Storage:/mnt/HDD/Storage \
repo/image
else
logger "ZFS pool SSD is not mounted; aborting $0!"
fi
binarysta 的答案让我克服了语法错误,但现在 case 语句中既没有使用break也没有使用exit,脚本根本就不再做它应该做的事情了。
选择 1. 如果容器存在(break;;),则应该在 case 语句之后运行命令,但如果容器不存在,则返回到菜单(;;)或退出脚本(exit;;)。
如果我使用break而不是;;无论我选择什么,它都会运行命令,而 ;;将返回到菜单并且永远不会在 case 之后运行命令。
答案1
case
语句中有一些语法错误。
#!/bin/sh
NAME=Container
if [ -d "/mnt/SSD/docker" ]; then
if [ ! -d "/mnt/SSD/docker/$NAME/data" ]; then
mkdir -p /mnt/SSD/docker/$NAME/data
fi
printf "\nPlease select installation method:\n\n"
while true; do
read -p "1. Re-create existing container
2. Create new container: " CHOICE
case $CHOICE in
1 )
if [ "$(docker ps -a | grep $NAME)" ]; then
IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $NAME)
MAC=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' $NAME)
docker stop $NAME; docker rm $NAME; docker image rm repo/image
else
printf "\nNo existing container found, going back to menu!\n\n"
fi
;;
2 )
read -p 'Please enter the IP-address to use: ' IP
read -p 'Please enter the MAC-address to use: ' MAC
;;
* )
printf "\nIncorrect or no selection made. Please enter 1 or 2.\n\n"
;;
esac
done
docker run -d \
-e JVM_HEAP=1024m -e TZ=Europe/Stockholm \
--hostname=$NAME \
--ip=$IP \
--mac-address=$MAC \
--name=$NAME --network=macvlan \
--restart=always \
-v /mnt/SSD/docker/$NAME/data:/$NAME/data \
-v /mnt/HDD/Storage:/mnt/HDD/Storage \
repo/image
else
logger "ZFS pool SSD is not mounted; aborting $0!"
fi
答案2
斯蒙基,;;是为了案件的分离。它们必须处于该级别,而不是在 if 内。那里有一个语法错误。所以在 if 你只做 'break' 和 ;; 中在 case 的末尾(if 之后)
希望有帮助。
#!/bin/sh
NAME=Container
while true; do
read -p "1. Re-create existing container
2. Create new container: " CHOICE
case $CHOICE in
1 )
echo 1 gekozen
if [ "y" = "x" ]
then
echo x
else
echo stop
break
fi
;;
2 )
echo 2 gekozen
read -p 'Please enter the IP-address to use: ' IP
read -p 'Please enter the MAC-address to use: ' MAC
echo IP is $IP
echo MAC is $MAC
break;;
* )
printf "\nIncorrect or no selection made. Please enter 1 or 2.\n\n"
;;
esac
done