我编写了一个 bash 脚本来针对测试套件测试学生编写的 C 程序。由于某种原因,脚本在一段时间后被杀死。我是 bash 脚本的新手,直到现在还没有找到原因。这是脚本。
#!/bin/bash
ulimit -t 1
tests_dir=tests
run_dir=tests
find . -name "*.c" | while read cfile; do rm a.out &> /dev/null; gcc "$cfile" -lm -w &> /dev/null;
if [ ! -f a.out ];
then
echo "$cfile did-not-compile" >> "$run_dir/results.out";
else
find "$tests_dir" -name "in*.txt" | while read testin; do echo "running $testin on $cfile";
rm test.out &> /dev/null;
rm space_less_testout &> /dev/null;
LD_PRELOAD=../../EasySandbox/EasySandbox.so ./a.out < $testin | grep -v "entering SECCOMP mode" &> test.out;
if [ -e test.out ]; then
testout=${testin/in/out}
tr -d '\n' < $testout | tr -d ' ' > space_less_testout
echo -e '\n' >> space_less_testout
if diff -qwB "$testout" test.out &> /dev/null
then
# if no difference then takes true brance (based on return value)
echo "$cfile ;passed-on-test; $testin" >> "$run_dir/results.out"; echo "passed-on-test $testin";
elif diff -qB space_less_testout test.out &> /dev/null
then
# or no difference with new-line removed should-be-output (just a formatting error)
echo "$cfile ;passed-on-test; $testin" >> "$run_dir/results.out"; echo "passed-on-test $testin";
else
echo "$cfile ;failed-on-test; $testin" >> "$run_dir/results.out"; echo "failed-on-test $testin";
fi
fi
done;
fi
done;
Run Code Online (Sandbox Code Playgroud)
Flo*_*sch 15
ulimit -t 1将脚本的 CPU 时间限制为 1 秒。当脚本耗尽其所有 CPU 时间时,它就会被杀死。
要限制脚本中仅一个命令的 CPU 时间,您可以使用括号在具有自己限制的子 shell 中启动它,例如
(ulimit -t 1; LD_PRELOAD=../../EasySandbox/EasySandbox.so ./a.out < $testin)
Run Code Online (Sandbox Code Playgroud)