简体   繁体   English

如何使用makefile测试我的Shell实现?

[英]How do I use makefile to test my shell implementation?

Here is my current makefile, which does not run test correctly: 这是我当前的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

My program tests for newline to know when a command has been entered. 我的程序测试换行符,以知道何时输入了命令。 This is the output of my program when I compile it myself manually: 这是我自己手动编译程序时的输出:

$ 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...

Here is the output when I run make test: 这是我运行make test时的输出:

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

Also, I have to hit ctrl+D every time for a new line to execute during make test. 另外,我必须每次按ctrl + D才能在make测试期间执行新行。

I'm trying to write this makefile for my class so that I can submit my assignment, my professor did not explain at all how to use a makefile besides the basic ./a.out [input command] 我正在尝试为我的班级编写该makefile,以便我可以提交作业,我的教授除了基本的./a.out [input command]之外,根本没有解释如何使用makefile。

He never explained how to use a makefile in the case that your program is running on a continuous loop like a shell is, waiting for the user to press [enter] or new line for the command to be parsed. 在您的程序正在像shell这样的连续循环上运行,等待用户按下[enter]或换行以解析命令的情况下,他从未解释过如何使用makefile。

I checked the GNU man for make but it didn't explain much in the "testing" section. 我检查了GNU男人的牌子,但在“测试”部分没有做太多解释。

Thanks for your help, I really appreciate it. 感谢您的帮助,我非常感谢。

test_input.txt's output: 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: 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

It looks like you're trying to supply input to your program. 您似乎正在尝试向程序提供输入。 You can't do this with make (directly) as make simply executes each line with /bin/sh -c COMMAND . 您不能使用make直接执行此操作,因为make只需使用/bin/sh -c COMMAND执行每一行。

What you can do is 你能做的是

test: shell2
    ./shell2 < test_input.txt

to redirect input to the file test_input.txt , which would contain the commands you want. 将输入重定向到文件test_input.txt ,其中将包含所需的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM