How do I use makefile to test my shell implementation? - c

Here is my current makefile, which does not run test correctly:
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
./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.
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]
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.
I checked the GNU man for make but it didn't explain much in the "testing" section.
Thanks for your help, I really appreciate it.
test_input.txt's output:
./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

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

Related

Run executable right after building (c, cmake, make)

I would like to run my executable right after building it by using a post-build step (add_custom_command).
So far we used a WSL environment on Windows 10, which worked. Now we switch to directly building it in the Windows cmd, which gives us an error...
CMakeLists.txt extract:
# Execute unit tests after build and write output.txt into artifact directory
add_custom_command(TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${PROJECT_NAME} ARGS -v > ${ARTIFACTS}/output.txt
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "run ${PROJECT_NAME} in ${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM
)
Within WSL using commands 'cmake -G "Unix Makefiles" $unit_tests_root' & 'make', CMake generates file: '...\build\Pipeline\CMakeFiles\unit_tests.dir\build.make', which shows:
...
unit_tests: CMakeFiles/unit_tests.dir/link.txt
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/mnt/c/repos/project-source/tests/unit_tests/build/Pipeline/CMakeFiles --progress-num=$(CMAKE_PROGRESS_30) "Linking C executable unit_tests"
$(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/unit_tests.dir/link.txt --verbose=$(VERBOSE)
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --blue --bold "run unit_tests in /mnt/c/repos/project-source/tests/unit_tests"
cd /mnt/c/repos/project-source/tests/unit_tests && /mnt/c/repos/project-source/tests/unit_tests/build/Pipeline/unit_tests -v > /mnt/c/repos/project-source/tests/unit_tests/artifacts/output.txt
# Rule to build all files generated by this target.
CMakeFiles/unit_tests.dir/build: unit_tests
...
This toolchain is working: The unit tests are executed and output is printed to output.txt.
Within cmd using commands 'cmake -G "MinGW Makefiles" %unit_tests_root%' & 'make', CMake generates the same file: '...\build\Pipeline\CMakeFiles\unit_tests.dir\build.make', which shows:
...
unit_tests.exe: CMakeFiles/unit_tests.dir/link.txt
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=C:\repos\project-source\tests\unit_tests\build\Pipeline\CMakeFiles --progress-num=$(CMAKE_PROGRESS_30) "Linking C executable unit_tests.exe"
$(CMAKE_COMMAND) -E cmake_link_script CMakeFiles\unit_tests.dir\link.txt --verbose=$(VERBOSE)
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --blue --bold "run unit_tests in C:/repos/project-source/tests/unit_tests"
cd /d C:\repos\project-source\tests\unit_tests && C:\repos\project-source\tests\unit_tests\build\Pipeline\unit_tests.exe -v > C:/repos/project-source/tests/unit_tests/artifacts/output.txt
# Rule to build all files generated by this target.
CMakeFiles/unit_tests.dir/build: unit_tests.exe
...
Error message in console: (line 555 corresponds to 'cd /d C:\repos\project-source\tests\unit_tests && ...'
...
[100%] Linking C executable unit_tests.exe
run unit_tests in C:/repos/project-source/tests/unit_tests
make[2]: *** [CMakeFiles\unit_tests.dir\build.make:555: unit_tests.exe] Error 13
make[2]: *** Deleting file 'unit_tests.exe'
make[1]: *** [CMakeFiles\Makefile2:94: CMakeFiles/unit_tests.dir/all] Error 2
make: *** [Makefile:102: all] Error 2
...
What's the problem with line 555?
When I comment out the 'add_custom_command(...)' within CMakeLists.txt, the unit_tests.exe builds successfully and I can execute it manually with:
C:\repos\project-source\tests\unit_tests>.\build\Pipeline\unit_tests.exe -v > .\artifacts\output.txt
Somehow the output is not the same as the output I get when build and executed within WSL, but at least it can be executed...
Edit 1, Additional Information: I am calling these commands (cmake, make) within a python script via subprocess.run(): I have the assumption, that this locks the files - still evaluating this...
Edit 2 / Solution, I found the problem: The error actually is returned by the unit_tests.exe itself as it has 13 errors (I missed the fact, that unity exits with the amount for errors). That's why make exits with errors and deletes the executable. Now I prefer calling the executable explicitly in my python script, where I also call the cmake and make commands. #Tsyvarev, sorry for the inconvenience and thanks for your contribution. ;-)

linux - How do I capture all of my compiler's output to a file as well as need to display output

The below program redirects all my output to log file. I need to display all the output along with log file. How can this be achieved?
$ gcc test.c
$ ./a.out > log.txt
$
You could use tee:
$ ./a.out | tee log.txt

nohup for batch command using " for x in find "

I am trying to run a Python script (qTrimHIV.py) on ALL the files ending in .fastq in the current directory. I'd like to append nohup at the beginning so that it does all the files out from a single command, and no matter if I close the terminal or logout.
The individual command:
for x in $(find . -name '*fastq'); do echo $x; python ../qTrimHIV.py -verbose -fastq $x -l 23 -m 20 -w 23 -o ${x%.fastq*}tr -mode 2; done
works well. But when I put nohup in front of the batch command:
nohup for x in $(find . -name '*fastq') ; do echo $x; python ../qTrimHIV.py -verbose -fastq $x -l 23 -m 20 -w 23 -o ${x%.fastq*}tr -mode 2; done
-bash: syntax error near unexpected token `do'
I get the error above.
However, it works well if I put the nohup before the actual command "python ../qTrimHIV.py ", but this is not really what I want, as I want to submit the task for all files at once and have it running until it's done, without having to keep logged in.
I've tried
for x in $(find . -name '*fastq') ; do echo $x; python ../qTrimHIV.py -verbose -fastq $x -l 23 -m 20 -w 23 -o ${x%.fastq*}qtrim -mode 2; done | at now
but it doesn't let me see the progress of the job, and I can't know if it is still running or not.
Can anyone give me any suggestions? Should I use a different command, other than nohup?
you can use:
nohup bash -c 'your command, everything including for'
also, why not to run shell script using nohup:
nohup <your script>

Error Using a Makefile For a Linux Shell Written in C

I created a Linux shell for one of my classes written in C. The shell works perfectly running such commands as ls, pwd, ps, top, cd, etc.
However, using a MakeFile (because that is part of the submission process), when the you use the command "make test" in Terminal I get the error tcsetpgrp: Inappropriate ioctl for device"amongst all of the correct output of my program. Within the Makefile I have test: test.txt | .project4.c with test.txt being the commands, ls, pwd, ps, top, cd separated by newlines instead of commas. What is this error and how do I get rid of it?
Here is my makefile:
PAWPRINT=myname
project4:
clean:
rm -f *.o
nuke:
rm -f *.o project4 *.gz
test: project4
cat test.txt | ./project4
submit.sh:
wget -O submit.sh http://somewebsite.com/submit.sh
chmod +x submit.sh
project4.tar.gz: project4.c test.txt Makefile
mkdir -p dist
cp $^ dist
tar -cvzf $# dist
submit: submit.sh project4.tar.gz
PAWPRINT=$(PAWPRINT) sh submit.sh project4.tar.gz
Here is my test.txt file:
sleep 1000 &
jobs
pwd
ls
echo hello > a.txt
echo world > b.txt
cat a.txt b.txt > c.txt
cat c.txt
My program can run these commands individually outside of the makefile. I designed the program to handles these commands because that is what the assignment was for.
So you're saying, as make sense, that the tcsetpgrp is coming from your program output right? Well this has mothing to do with your MakeFile, but as a result of running the program inside the submission process. Your program is attempting to become a process group and thus is trying to tell its controlling terminal that it's the foreground process. But there is no terminal because you're inside the submission process, so tcsetpgrp() complains (man tcsetpgrp(3) for more information). Generally this is harmless, though annoying.

"no test history available" error when running cal

vogar --benchmark --stream --verbose --mode jvm ArraySortBenchmark.java
But it doesn't execute any benchmark, because "no test history available"
executing mkdir -p /tmp/vogar/573bd257-1b6e-4b91-92dd-1cdc6bdc491b
Actions: 1
skipped Users.louischiffre.projects.testing.ArraySortBenchmark.java
Task 0: prepare target
Task 1: rm /tmp/vogar/573bd257-1b6e-4b91-92dd-1cdc6bdc491b
depends on completed task: prepare target
Task 2: rm /tmp/vogar/run
depends on completed task: prepare target
running prepare target
executing rm -rf /tmp/vogar/run
executing mkdir -p /tmp/vogar/run
executing mkdir -p /tmp/vogar/run/tmp
executing mkdir -p /tmp/vogar/dalvik-cache
executing mkdir -p /tmp/vogar/run/user.home
running rm /tmp/vogar/run
running rm /tmp/vogar/573bd257-1b6e-4b91-92dd-1cdc6bdc491b
executing rm -rf /tmp/vogar/573bd257-1b6e-4b91-92dd-1cdc6bdc491b
executing rm -rf /tmp/vogar/run
parsing outcomes from 0 files
Skips summary:
Users.louischiffre.projects.testing.ArraySortBenchmark.java (no test history available)
Outcomes: 1. Passed: 0, Failed: 0, Skipped: 1. Took 129ms.
Interestingly, dn caliper example
EnumSetContainsBenchmark.java
do not have this problem.

Resources