You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bash脚本自动化submit50提交CS50P作业失败求助

解决CS50P批量提交作业时submit50失败的问题

问题情况

完成CS50P课程作业时,手动用submit50提交单次耗时6-8分钟,于是编写Bash脚本批量提交已完成作业,但脚本在git commit阶段失败。对比日志发现:

  • 脚本生成的提交路径包含./,例如cs50/problems/2022/python/./einstein
  • 手动提交的路径无此冗余部分,例如cs50/problems/2022/python/einstein

怀疑是路径中的./导致submit50识别分支出错,提交失败。

原脚本代码

#!/bin/bash

cd /home/pilks/cs50/python

for d in ./*;
do cd $d && for d2 in ./*;
do cd $d2 && yes yes | submit50 cs50/problems/2022/python/$d2 --log-level info;
cd ..
done
cd ..
done

失败日志片段

Connecting...
Authenticating...
Verifying...
(INFO) git clone --bare --single-branch ssh://git@ssh.github.com:443/me50/[user].git --branch cs50/problems/2022/python/./einstein
...
(INFO) git commit -m 'automated commit by submit50 [lang=en_GB:en]' --allow-empty
Sorry, something's wrong, please try again...
Submission cancelled.

手动成功日志片段

$ submit50 cs50/problems/2022/python/einstein --log-level info
...
(INFO) git commit -m 'automated commit by submit50 [lang=en_GB:en]' --allow-empty
(INFO) git push origin cs50/problems/2022/python/einstein
...
Go to https://submit.cs50.io/... to see your results.

问题原因

脚本中$d2变量的取值是./einstein这类带当前目录前缀的路径,直接拼接到提交路径后,生成的分支路径包含./,submit50无法正确识别该分支,导致提交失败。

解决方案

需要移除$d2中的./前缀,有两种常用方法:

方法1:使用参数扩展去除前缀

修改后的脚本:

#!/bin/bash

# 切换到目标目录,失败则退出脚本
cd /home/pilks/cs50/python || exit

for d in ./*; do
    # 进入子目录,失败则跳过当前循环
    cd "$d" || continue
    for d2 in ./*; do
        # 去除d2开头的./前缀
        problem_name="${d2#./}"
        cd "$d2" || continue
        yes yes | submit50 "cs50/problems/2022/python/$problem_name" --log-level info
        cd ..
    done
    cd ..
done

方法2:使用basename命令提取目录名

修改后的脚本:

#!/bin/bash

cd /home/pilks/cs50/python || exit

for d in ./*; do
    cd "$d" || continue
    for d2 in ./*; do
        # 提取目录的基本名称(去掉路径前缀)
        problem_name=$(basename "$d2")
        cd "$d2" || continue
        yes yes | submit50 "cs50/problems/2022/python/$problem_name" --log-level info
        cd ..
    done
    cd ..
done

说明

修改后,提交路径会变成和手动提交一致的cs50/problems/2022/python/einstein,submit50能正确识别分支,完成提交。同时脚本中添加了|| exit和|| continue,避免目录切换失败后执行后续错误代码。

内容的提问来源于stack exchange,提问作者pilks7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 04:35:55