我试图通过翼手龙面板运行自定义鸡蛋,但是,我收到错误“ /entrypoint.sh:第30行:语法错误:文件意外结束”
我的Docker镜像如下;
FROM ubuntu:18.04
MAINTAINER Amelia, <me@amelia.fun>
RUN apt-get update -y
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y dos2unix curl gnupg2 git-core zlib1g-dev libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev libffi-dev yarn build-essential gpg-agent zip unzip software-properties-common git default-jre python3-pip python-minimal python-pip ffmpeg libopus-dev libsodium-dev libpython2.7 libpython2.7-dev wget php7.2 php7.2-common php7.2-cli php7.2-fpm
RUN curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
RUN bash nodesource_setup.sh
RUN apt-get install -y nodejs
RUN rm -rf nodesource_setup.sh
RUN adduser -D -h /home/container container
USER container
ENV USER=container HOME=/home/container
WORKDIR /home/container
COPY ./entrypoint.sh /entrypoint.sh
CMD ["/bin/bash", "/entrypoint.sh"]
和我的entrypoint.sh如下;
#!/bin/bash
cd /home/container
MODIFIED_STARTUP=`eval echo $(echo ${STARTUP_PARAMETERS} | sed -e 's/{{/${/g' -e 's/}}/}/g')`
rm -rf *
git clone ${REPO_PARAMETERS}
cd */
if grep -q 'Java' AppType
then
${STARTUP_PARAMETERS}
if grep -q 'PHP' AppType
then
${STARTUP_PARAMETERS}
elif grep -q 'Python2' AppType
then
[ -f "requirements.txt" ] && pip2 install -r requirements.txt ${STARTUP_PARAMETERS} || ${STARTUP_PARAMETERS}
elif grep -q 'Python3' AppType
then
[ -f "requirements.txt" ] && pip3 install -r requirements.txt ${STARTUP_PARAMETERS} || ${STARTUP_PARAMETERS}
elif grep -q 'NodeJS' AppType
then
npm install
${STARTUP_PARAMETERS}
else
echo "Application not supported"
fi
echo "${MODIFIED_STARTUP}"
Bash文件的长度不超过30行,所以我不确定。
我所使用的指南也可以找到here
答案 0 :(得分:2)
直接的问题是您有两个if
语句,但是其中只有一个用fi
关闭;在我看来,第二个应该是elif
。但是还有许多其他事情对我来说似乎是个坏主意:
cd
命令应该(几乎)始终进行错误测试-例如,如果cd /home/container
由于某种原因失败,则脚本的其余部分(包括rm -rf *
)将在意外的位置运行。现在,一个自我毁灭的Docker环境可能不如一个自我毁灭的真实系统那么重要,但这仍然不是一件好事。我会改用这样的东西:
cd /home/container || {
echo "Error -- can't move to /home/container, something rotten in Denmark." >&2
exit 1
}
类似的评论适用于cd */
。
设置MODIFIED_STARTUP
的下一行是一堆坏主意。我不熟悉$STARTUP_PARAMETERS
中的内容,但总的来说,请使用$( )
而不是反引号(不要将两者混用)。 echo $(somecommand)
几乎是禁止操作的,只需直接运行命令即可。另外,变量引用(以及类似的扩展名,例如$( )
)应该几乎总是用双引号引起来(例外:在赋值的右侧)。 eval
通常很危险,应尽可能避免。我为您提供了$STARTUP_PARAMETERS
外观的示例,我可能可以对此进行整理。
大if ... elif...
等具有多个条件,它们可以完成相同的操作,例如
elif grep -q 'Python2' AppType
then
[ -f "requirements.txt" ] && pip2 install -r requirements.txt ${STARTUP_PARAMETERS} || ${STARTUP_PARAMETERS}
elif grep -q 'Python3' AppType
then
[ -f "requirements.txt" ] && pip3 install -r requirements.txt ${STARTUP_PARAMETERS} || ${STARTUP_PARAMETERS}
根据DRY原则(不要重复自己),最好对所有等效情况进行一次测试,如下所示:
elif grep -q 'Python2' AppType || grep -q 'Python3' AppType
then
[ -f "requirements.txt" ] && pip2 install -r requirements.txt ${STARTUP_PARAMETERS} || ${STARTUP_PARAMETERS}
甚至:
elif grep -q 'Python[23]' AppType
then
[ -f "requirements.txt" ] && pip2 install -r requirements.txt ${STARTUP_PARAMETERS} || ${STARTUP_PARAMETERS}
顺便说一句,使用${STARTUP_PARAMETERS}
不带引号在这里给我敲响了警钟,但这可能是不可避免的-再次,我不知道它的格式。并且&& ... ||
结构并不总是可以安全地替代if then else fi
,因为它可以同时运行两个分支。在此脚本中,如果requirements.txt存在,但pip2 install
命令失败,它将继续运行${STARTUP_PARAMETERS}
。那是故意的吗?如果没有,我会改用正确的if
语句。