【发布时间】:2022-01-27 03:51:33
【问题描述】:
我正在使用:
/usr/local/bin # cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.12.7
PRETTY_NAME="Alpine Linux v3.12"
使用 /bin/ash shell
我有一个检查状态的脚本:
#!/bin/sh
full=$(DOCUMENT_URI=/api/$SERVICE_PREFIX/healthz SCRIPT_FILENAME=/app/src/index.php REQUEST_METHOD=GET cgi-fcgi -bind -connect /run/php-fpm/php.sock)
before=${full%$'\r\n\r\n'*}
after=${full#*$'\r\n\r\n'}
stat=$(echo $before | sed 's/.*Status: //' | cut -d " " -f 1)
sub="Status:"
echo "head is: $before"
echo "tail is: $after"
echo "message is: $full"
echo "status is: $stat"
echo "substring is: $sub"
echo "message is: ${sub} ${stat}"
if [[ "$before" == *"$sub"* ]] && [[ "$stat" != "200"]]; then
exit 1
fi
if [ "$after" != "OK" ]; then
exit 1
fi
exit
当我执行脚本时,我得到正确的响应,但 if 语句不起作用:
/usr/local/bin # ./liveness_probe1.sh
head is: Status: 300 Multiple Choices
X-Powered-By: PHP/7.4.16
Content-type: text/html; charset=UTF-8
tail is: OK
message is: Status: 300 Multiple Choices
X-Powered-By: PHP/7.4.16
Content-type: text/html; charset=UTF-8
OK
status is: 300
substring is: Status:
message is: Status: 300
/usr/local/bin # echo $?
0
当我只执行一个命令时,我会得到不同的响应:
/usr/local/bin # DOCUMENT_URI=/api/$SERVICE_PREFIX/healthz SCRIPT_FILENAME=/app/src/index.php REQUEST_METHOD=GET cgi-fcgi -bind -connect /run/php-fpm/php.so
ck
Status: 300 Multiple Choices
X-Powered-By: PHP/7.4.16
Content-type: text/html; charset=UTF-8
OK
这个脚本有错误吗?
【问题讨论】: