arthas icon indicating copy to clipboard operation
arthas copied to clipboard

Update Dockerfile-No-Jdk

Open anjia0532 opened this issue 5 years ago • 3 comments

fixed when $MIRROR is false but allways use mirror addr

anjia0532 avatar May 29 '19 10:05 anjia0532

没看懂这个,为什么加了个 x ?

hengyunabc avatar May 30 '19 03:05 hengyunabc

一般写bash脚本的一个常用写法

export MIRROR=false
if [ $MIRROR ];  then echo 'true' ; fi

打印的还是true 一般需要判断字符串

export MIRROR=false
if [ "$MIRROR" = "false" ];  then echo "false" ; else echo "true" ; fi
>> false

export MIRROR=true
if [ "$MIRROR" = "false" ];  then echo "false" ; else echo "true" ; fi
>> true

说明符合预期

但是如果MIRROR是空值,就很尴尬。

export MIRROR=true
echo $MIRROR
>> true
unset MIRROR
echo $MIRROR
>> 空
if [ "$MIRROR" = "false" ];  then echo "false" ; else echo "true" ; fi
>> true

if [ "$MIRROR" = "true" ];  then echo "true" ; else echo "false" ; fi
>> false

而加上x,或者别的字母,起码能保证,是两个有值的字符串在比对

export MIRROR=true
“x$MIRROR” = "xtrue" -> xtrue = xtrue -> true
unset MIRROR
“x$MIRROR” = "xtrue" -> x = xtrue -> false

虽然上述步骤,可以通过调整分支语句的顺序,来得到预期效果。但是容易导致意料之外的结果,比如,把 true/false一颠倒,走的都是else,这种问题很隐蔽。很容易成坑。

另外还有种更好理解的一种写法,允许MIRROR是true/y/yes/1 都是使用镜像源(但是在alpine里默认的/bin/sh不支持)

MIRROR=false
if [[ "$MIRROR" =~ ^(true|y|yes|1)$ ]]; then
    echo "$MIRROR is in the list"
else
    echo "$MIRROR is not in the list"
fi

anjia0532 avatar May 30 '19 08:05 anjia0532

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Jun 12 '20 07:06 CLAassistant