0

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.

VJZ
  • 119
  • 2
  • 1
    The output from `time` (which is your shell's builtin `time` function, not `/usr/bin/time`) is going to standard error, while your `time=$(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:38
  • Always paste your script into `https://shellcheck.net`, a syntax checker, or install `shellcheck` locally. Make using `shellcheck` part of your development process. – waltinator Jun 08 '21 at 13:39
  • ... (cont.) see for example [How to capture stderr of a bash keyword (e.g. time)?](https://unix.stackexchange.com/questions/429547/how-to-capture-stderr-of-a-bash-keyword-e-g-time) and more generally [How to measure time of program execution and store that inside a variable](https://unix.stackexchange.com/questions/12068/how-to-measure-time-of-program-execution-and-store-that-inside-a-variable) – steeldriver Jun 08 '21 at 13:42
  • I changed it so that I don't need /usr/bin/time anymore, is there still a method of formatting built in time? – VJZ Jun 08 '21 at 14:08
  • @VJZGamingHD you *have* successfully formatted the builtin time (hence why you get `0.172` instead of the default `real xxx` / `user xxx` / `sys xxx`). What you have *not* done is captured it in the `time1` variable, for the reasons explained in the linked answers. – steeldriver Jun 08 '21 at 14:39
  • @steeldriver For some reason doing `2>&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

1 Answers1

-3

By simply calling time, you're making use of the bash builtin. Check this with type -a time.

To use the time program, use the full path, /usr/bin/time.

Read man time bash.

waltinator
  • 4,439
  • 1
  • 16
  • 21
  • 1
    Why is `man time` relevant here? That gives the manual for the binary `/usr/bin/time` (or whatever is installed), not for the builtin. Did you mean "read `help time`"? And how is this answering the OP's question about the output they are seeing? – terdon Jun 08 '21 at 13:48