bash - 如何防止Expect输出CRLF?
问题描述
它来自操作系统分配,为 Xv6 添加系统调用(https://github.com/remzi-arpacidusseau/ostep-projects,initial-xv6)。我确保我的代码正确并设置了环境,但就是无法通过测试。跑步diff
回报
1c1
< XV6_TEST_OUTPUT 200000
---
> XV6_TEST_OUTPUT 200000
并且od
我发现答案应该\n
用作换行符,而在我的输出中,它是\r\n
.
我花了相当长的时间来明确这\r
不是 Xv6 中的 printf 或 qemu 引入的。测试是使用 Expect 脚本完成的,这就是我认为可能有问题的地方。
我发现了一个相关的问题,但似乎没有给出可行的解决方案: \r 来自哪里?
这个问题似乎并不常见,我的大多数同学都能通过测试。所以我想知道如何防止\r
输出?
期望脚本:
#! /usr/bin/env expect
proc shutdown {} {
# send command to halt qemu (ctrl-a x)
# https://stackoverflow.com/questions/27050473/how-to-send-ctrl-a-then-d-in-expect
send "\x01"; send "x"
# make sure to wait for it all to stop
# (without this, script was terminating before qemu quit -> bad)
expect eof
}
# turn off timeout (perhaps make this flexible later)
set timeout -1
# build and launch xv6 on qemu
spawn make [lindex $argv 0] -f [lindex $argv 1] qemu-nox
trap {
shutdown
exit 0
} SIGINT
# wait for initial prompt
expect "init: starting sh\r"
expect "$ "
# send command as per command line
send "[lindex $argv 2]\r"
# wait for it to be done
expect "$ "
# shutdown qemu properly (avoid runaways)
shutdown
运行测试的命令:
cd src; ../../tester/run-xv6-command.exp CPUS=1 Makefile.test test_1 | grep XV6_TEST_OUTPUT; cd ..
测试1.c
#include "types.h"
#include "stat.h"
#include "user.h"
int
main(int argc, char *argv[]) {
int x1 = getreadcount();
int x2 = getreadcount();
char buf[100];
(void) read(4, buf, 1);
int x3 = getreadcount();
int i;
for (i = 0; i < 1000; i++) {
(void) read(4, buf, 1);
}
int x4 = getreadcount();
printf(1, "XV6_TEST_OUTPUT %d %d %d\n", x2-x1, x3-x2, x4-x3);
exit();
}
解决方案
推荐阅读
- python - 从源代码和“错误的解释器:没有这样的文件或目录”构建 Python 和 Pip
- python - 为数组中的每个元素 n 快速执行函数 n 次
- flutter - 我可以在 FlutterWeb 上使用 Image.network 吗?
- css - SVG 不随页面大小调整 React 缩放
- excel - 直接在 vba 中使用来自 WorkbookQuery 的数据
- java - 求 n 个斐波那契数的总和
- angular - `(keyup.backspace)` 无法捕获 'shift + backspace'
- apache-spark - 引起:java.lang.IllegalStateException:已经分配了最多8192页
- angular - Angular / DataTables:TypeError:无法通过 2 个 API 调用读取未定义的属性“aDataSort”
- search - 在 ArrayList 中查找共享数据字段值的多个对象的索引值