gpt4 book ai didi

c - 如何使用 makefile 来测试我的 shell 实现?

转载 作者:行者123 更新时间:2023-11-28 21:32:19 25 4
gpt4 key购买 nike

这是我当前的 makefile,它没有正确运行测试:

shell2: shell2.o
shell2.o: shell2.c

clean:
rm -f *.o

test: shell2
./shell2
pwd
./shell2
cd ..
./shell2
jobs
./shell2
sleep 100 &
jobs
./shell2
exit

我的程序测试换行符以了解何时输入命令。这是我的输出自己手动编译时的程序:

$ pwd
/students/8/[redacted]/[redacted]/Shell2
$ cd ..
$ jobs
Jobs:
$ sleep 1000 &
To the background: 20203
$ jobs
Jobs:
20203
$ jobs
Jobs:
20203
$ killall sleep
sleep(17014): Operation not permitted
sleep(17305): Operation not permitted
sleep(17433): Operation not permitted
sleep(19741): Operation not permitted
sleep(19841): Operation not permitted
sleep(20041): Operation not permitted
sleep(20183): Operation not permitted
$ jobs
Jobs:
$ exit
now exiting...

这是我运行 make test 时的输出:

make test
./shell2
$ pwd
/students/8/[redacted]/[redacted]/Shell2
./shell2
$ cd ..
./shell2
$ jobs
make: jobs: Command not found
make: *** [test] Error 127

此外,我每次都必须按 ctrl+D 才能在 make test 期间执行新行。

我正在尝试为我的类(class)编写这个 makefile,以便我可以提交我的作业,我的教授除了基本的之外根本没有解释如何使用 makefile./a.out [输入命令]

他从未解释过在您的程序像 shell 一样连续循环运行的情况下如何使用 makefile,等待用户按 [enter] 或换行以解析命令。

我检查了 GNU man for make,但它在“测试”部分没有太多解释。

谢谢你的帮助,我真的很感激。

test_input.txt 的输出:

./shell2 < test_input.txt
"Sending command: pwd"
/students/8/[redacted]/[redacted]/Shell2
"Sending command: cd .."
"Sending command: pwd"
/students/8/[redacted]/[redacted]
"Sending command: jobs"
$ $ $ $ $ $ $ $ Jobs:
$
"Sending command: sleep 1000 &"
$ $ To the background: 27199
"jobs"
$ $ Jobs:
27199
$
"Sending command: killall sleep"
$ $ $ $ Jobs:
"Sending command: jobs"
$ $ now exiting...
"exit"

test_input.txt:

echo "Sending command: pwd"
pwd
echo "Sending command: cd .."
cd ..
echo "Sending command: pwd"
pwd
echo "Sending command: jobs"
jobs

echo "Sending command: sleep 1000 &"
sleep 1000 &
echo "jobs"
jobs

echo "Sending command: killall sleep"
killall sleep
echo "Sending command: jobs"
jobs
echo "exit"
exit

最佳答案

看起来您正在尝试为您的程序提供输入。您不能使用 make(直接)执行此操作,因为 make 只是使用 /bin/sh -c COMMAND 执行每一行。

你能做的是

test: shell2
./shell2 < test_input.txt

将输入重定向到文件 test_input.txt,其中将包含您想要的命令。

关于c - 如何使用 makefile 来测试我的 shell 实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15080605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com