😡 GPT Sol 5.6 傻逼吧,我一个 6 行的脚本让它修个 BUG 给我改到了 178 行

纳西妲 2026-07-17 14:52 1

^-^ 无语了整个脚本就只是复制新 tls 证书检查是否有更新,有更新就覆盖本地 tls 证书


我自己写的老版本:


        set -e
rclone --config=/etc/rclone.conf copyto r2:tls/key.pem key.pem.new
if ! cmp -s out/nahida.im/key.pem key.pem.new; then
rm -rf out/nahida.im
rclone --config=/etc/rclone.conf copy r2:tls out/nahida.im
fi

ChatGPT 给我的修复版(贼长而且还啥问题都没修复):


        set -euo pipefail

certRoot="/var/lib/acme"
active="$certRoot/${certName}"
generationRoot="$certRoot/.${certName}.generations"

staging="/run/${serviceName}/staging"
validation="/run/${serviceName}/validation"

mkdir -p "$generationRoot"

download_and_validate() {
rm -rf "$staging" "$validation"

mkdir -m 0700 "$staging"
mkdir -m 0700 "$validation"

echo "Downloading certificate files from R2..."

rclone \
--config=/etc/rclone.conf \
--contimeout=30s \
--timeout=2min \
--retries=3 \
--low-level-retries=10 \
--retries-sleep=10s \
--log-level=INFO \
copy \
"r2:tls" \
"$staging" \
|| return 1

for file in key.pem cert.pem chain.pem fullchain.pem; do
if [ ! -s "$staging/$file" ]; then
echo "Missing or empty certificate file: $file" >&2
return 1
fi
done

cat \
"$staging/key.pem" \
"$staging/fullchain.pem" \
> "$staging/full.pem" \
|| return 1

chmod 0640 \
"$staging/key.pem" \
"$staging/cert.pem" \
"$staging/chain.pem" \
"$staging/fullchain.pem" \
"$staging/full.pem"

if ! openssl pkey \
-in "$staging/key.pem" \
-noout
then
echo "Unable to parse key.pem" >&2
return 1
fi

for file in cert.pem chain.pem fullchain.pem; do
if ! openssl x509 \
-in "$staging/$file" \
-noout
then
echo "Unable to parse $file" >&2
return 1
fi
done

if ! openssl x509 \
-in "$staging/cert.pem" \
-noout \
-checkend 86400
then
echo "Downloaded certificate expires in less than 24 hours" >&2
return 1
fi

openssl x509 \
-in "$staging/cert.pem" \
-outform DER \
> "$validation/cert.der" \
|| return 1

openssl x509 \
-in "$staging/fullchain.pem" \
-outform DER \
> "$validation/fullchain-leaf.der" \
|| return 1

if ! cmp -s \
"$validation/cert.der" \
"$validation/fullchain-leaf.der"
then
echo "cert.pem does not match the leaf certificate in fullchain.pem" >&2
return 1
fi

openssl pkey \
-in "$staging/key.pem" \
-pubout \
-outform DER \
> "$validation/key-public.der" \
|| return 1

openssl x509 \
-in "$staging/cert.pem" \
-pubkey \
-noout \
| openssl pkey \
-pubin \
-outform DER \
> "$validation/cert-public.der" \
|| return 1

if ! cmp -s \
"$validation/key-public.der" \
"$validation/cert-public.der"
then
echo "Downloaded private key does not match the certificate" >&2
return 1
fi
}

downloadSucceeded=false

for attempt in 1 2 3; do
echo "Download and validation attempt $attempt/3"

if download_and_validate; then
downloadSucceeded=true
break
fi

if [ "$attempt" -lt 3 ]; then
echo "Download or validation failed; retrying in 15 seconds..." >&2
sleep 15
fi
done

if [ "$downloadSucceeded" != true ]; then
echo "Unable to download a complete and valid certificate set" >&2
exit 1
fi

unchanged=true

for file in key.pem cert.pem chain.pem fullchain.pem; do
if \
[ ! -s "$active/$file" ] \
|| ! cmp -s "$active/$file" "$staging/$file"
then
unchanged=false
break
fi
done

if [ "$unchanged" = true ]; then
echo "Certificate files are unchanged"
exit 0
fi

digest="$(
sha256sum \
"$staging/key.pem" \
"$staging/cert.pem" \
"$staging/chain.pem" \
"$staging/fullchain.pem" \
| sha256sum \
| cut -d ' ' -f 1
)
"


generation="$generationRoot/$digest"

if [ ! -d "$generation" ]; then
temporaryGeneration="$generationRoot/.new-$digest"

rm -rf "$temporaryGeneration"
mkdir -m 0750 "$temporaryGeneration"

for file in key.pem cert.pem chain.pem fullchain.pem full.pem; do
install \
-m 0640 \
"$staging/$file" \
"$temporaryGeneration/$file"
done

mv -T "$temporaryGeneration" "$generation"
fi

temporaryLink="$certRoot/.${certName}.new"
legacyPath=""

rm -f "$temporaryLink"

ln -s \
".${certName}.generations/$digest" \
"$temporaryLink"

if [ -e "$active" ] && [ ! -L "$active" ]; then
legacyPath="$generationRoot/legacy-$(date +%s)-$$"

echo "Preserving existing certificate directory as $legacyPath"

mv -T "$active" "$legacyPath"
fi

if ! mv -Tf "$temporaryLink" "$active"; then
echo "Unable to activate the new certificate generation" >&2

rm -f "$temporaryLink"

if [ -n "$legacyPath" ] && [ -e "$legacyPath" ]; then
mv -T "$legacyPath" "$active"
fi

exit 1
fi

echo "Activated certificate generation: $generation"

事实证明还是自己写的好,后面我直接自己修了:


        set -euo pipefail
rclone \
--config=/etc/rclone.conf \
--contimeout=30s \
--timeout=2m \
--retries=5 \
--low-level-retries=10 \
--retries-sleep=15s \
copy "r2:tls" new
for file in chain.pem fullchain.pem full.pem key.pem; do
if [ ! -s "new/$file" ]; then
echo "Missing or empty certificate file: $file" >&2
rm -rf new
exit 1
fi
done

mkdir -p /var/lib/acme/generations
gen="/var/lib/acme/generations/$(sha256sum new/full.pem | cut -d ' ' -f 1)"
if [ -d "$gen" ]; then
rm -rf new
else
mv new "$gen"
ln -sfT "$gen" "/var/lib/acme/${cert}"
for g in /var/lib/acme/generations/*; do
[ -e "$g" ] || continue
[ "$g" = "$gen" ] || rm -rf -- "$g"
done
fi

echo "Certificate for ${cert} updated successfully"
最新回复 (22)
  • buspotato 07-17 14:53
    1

    哈哈哈

  • leic-山水 07-17 14:53
    2

    来,把6行和178贴出来,大家分析分析

  • Tokin 07-17 14:54
    3

    GPT:不好意思,我觉得你需要

  • xikk 07-17 14:54
    4


    一样 前天做差不多的一个软件 现在越改问题越多 Tokens越用越多。。

  • wujiong 07-17 14:55
    5

    这种任务还不如自己手写

  • sudy 07-17 14:57
    6

    想到这个笑话,GPT在提前帮你避免bug ^-^


  • 泪水打湿麻辣烫 07-17 14:57
    7

    我换个deepseek不到一分钟给我跑好了,5.6sol愣是跑了几十分钟

  • 纳西妲 楼主 07-17 14:58
    8

    @buspotato #1 发布于2026/7/17 14:53:29

    哈哈哈


    @leic-山水 #2 发布于2026/7/17 14:53:59

    来,把6行和178贴出来,大家分析分析


    @Tokin #3 发布于2026/7/17 14:54:09

    GPT:不好意思,我觉得你需要


    @xikk #4 发布于2026/7/17 14:54:22


    一样 前天做差不多的一个软件 现在越改问题越多 Tokens越用越多。。


    @wujiong #5 发布于2026/7/17 14:55:00

    这种任务还不如自己手写


    @sudy #6 发布于2026/7/17 14:57:17

    想到这个笑话,GPT在提前帮你避免bug ^-^



    @泪水打湿麻辣烫 #7 发布于2026/7/17 14:57:21

    我换个deepseek不到一分钟给我跑好了,5.6sol愣是跑了几十分钟


    已更新代码

  • joe001 07-17 14:58
    9

    不至于吧5.6Sol,我个人感觉和Opus4.8已经差距不大了

  • ZaNgVVB 07-17 14:58
    10

    想的多也怕,想的不多也怕

  • whipjamboree 07-17 14:58
    11

    这可不是 ponytail, 是 ponyhead 了

  • wujiong 07-17 14:58
    12

    @纳西妲 #8 这么多if干啥

  • 纳西妲 楼主 07-17 14:59
    13

    @wujiong #12 发布于2026/7/17 14:58:48

    @纳西妲 #8 这么多if干啥


    不知道啊 GPT 给我写了一堆 if

  • huggchai 07-17 15:00
    14

    过度思考,试一下kimi k3吧

  • katorly 07-17 15:01
    15
    set -euo pipefail

    这个一看就是gpt喜欢写的参数,而且不同时期的gpt写的这个set后面的参数还不一样

  • 老衲推车 07-17 15:04
    16

    给你规避潜在风险了

  • 纳西妲 楼主 07-17 15:05
    17

    @katorly #15 发布于2026/7/17 15:01:18,编辑于2026/7/17 15:01:33


    set -euo pipefail

    这个一看就是gpt喜欢写的参数,而且不同时期的gpt写的这个set后面的参数还不一样


    set -e 是一条命令失败整个脚本失败

    -u 是用未定义变量直接报错

    -o pipefail 是 a | b 这种命令一个失败全失败(Cloudflare 就因为没加这个宕机过一次)


    我感觉这几个参数还好吧,一般人写脚本估计也会加这几个

  • 纳西妲 楼主 07-17 15:08
    18

    @老衲推车 #16 发布于2026/7/17 15:04:13

    给你规避潜在风险了


    规避个 P,temporaryLink 它先给我删除后再 ln -s 重新创建,本来一行 ln -sf 直接就是原子操作的愣是写成了非原子操作,写了那么长一串检测啥用都没有,还顺带藏了个 BUG ^-^

  • katorly 07-17 15:08
    19

    @纳西妲 #17 gpt-5.x 之前,gpt只写set -e来着

  • 胖达任 07-17 15:09
    20



    最近越发觉得有点智障了

  • 冠希陈 07-17 15:13
    21

    5.6:“不好意思咯,我以为你喜欢~”

  • Paimic 07-17 16:01
    22

    提高……额,鲁棒性?

* 帖子来源NodeSeek
返回