简体   繁体   English

alpine 中的双 ambersand 运算符未按预期工作

[英]Double ambersand operator in alpine not working as expected

The following script以下脚本

#!/bin/sh -e


wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub
wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.32-r0/glibc-2.32-r0.apk
apk add glibc-2.32-r0.apk && rm glibc-2.32-r0.apk
sleep 1

run on an alpine:3.16 image succeeds, while there is definitely an error in the apk add glibc-2.32-r0.apk command.alpine:3.16图像上运行成功,而apk add glibc-2.32-r0.apk命令肯定有错误。

/ # ./script.sh 
Connecting to github.com (140.82.121.3:443)
Connecting to objects.githubusercontent.com (185.199.108.133:443)
saving to 'glibc-2.32-r0.apk'
glibc-2.32-r0.apk    100% |**************************************************************************************************************************************************************************************************| 4331k  0:00:00 ETA
'glibc-2.32-r0.apk' saved
(1/1) Installing glibc (2.32-r0)
ERROR: glibc-2.32-r0: trying to overwrite etc/nsswitch.conf owned by alpine-baselayout-data-3.2.0-r23.
1 error; 44 MiB in 20 packages
/ # echo $?
0

Why isn't the && operator in the following line为什么&&运算符不在下一行中

apk add glibc-2.32-r0.apk && rm glibc-2.32-r0.apk

work as expected?按预期工作?

Because the shell can't distinguish between a 1 returned because of a failure and a 1 returned to successfully indicate false , any case where you're branching on a command disables set -e behavior for the duration of that command.因为 shell 无法区分因失败而返回的 1 和成功返回的 1 指示 false ,任何情况下您在命令上分支都会在该命令的持续时间内禁用set -e行为。

Thus, because the && rm glibc-2.32-r0.apk happens conditionally on whether the apk command succeeds, that apk command is "checked", so set -e does not apply for it;因此,因为&& rm glibc-2.32-r0.apk发生在apk命令是否成功,所以 apk 命令被“检查”,所以set -e不适用于它; the sleep at the last line is executed, and its exit status (of 0) is returned for the script as a whole.执行最后一行的sleep ,并为整个脚本返回退出状态(0)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM