I am writing a script for competitive programming judging and here is part of the bash script:
#!/bin/bash
TIMEFORMAT=%S
if [ $1 = "-cpp" ]; then
output1=$(g++ -std=c++17 -O2 -Wall -fsanitize=address -lm -s -x c++ b.cpp && ./a.out < input1)
expected1=$(< output1)
time1=$(time g++ -std=c++17 -O2 -Wall -fsanitize=address -lm -s -x c++ b.cpp && ./a.out < input1)
if [[ -z "$expected" ]]; then
echo "Test Case #1 - Passed! Time: ${time1} sec"
elif [ $output1 = $expected1 ]; then
echo "Test Case #1 - Passed."
else
echo "Test Case #1 - Failed (check 'dump1.log' for details)."
fi
fi
The output for the C++ program is 15
. The output of the bash script is:
0.172
Test Case #1 - Passed! Time: 15 sec
It prints the time output above the actual point of concatenation. And in the place of concatenation, it prints the actual C++ output. I am extremely new to bash so I don't know what is going on.
time
(which is your shell's builtintime
function, not/usr/bin/time
) is going to standard error, while yourtime=$(time g++ ...)
is capturing the C++ program's ordinary output. I don't see where$expected
gets defined at all. – steeldriver Jun 08 '21 at 13:38https://shellcheck.net
, a syntax checker, or installshellcheck
locally. Make usingshellcheck
part of your development process. – waltinator Jun 08 '21 at 13:390.172
instead of the defaultreal xxx
/user xxx
/sys xxx
). What you have not done is captured it in thetime1
variable, for the reasons explained in the linked answers. – steeldriver Jun 08 '21 at 14:392>&1 1>/dev/null
worked. Can you explain why I have to append that to the end of the command in the solutions so I can mark it as correct? – VJZ Jun 08 '21 at 15:06