I write a shell script and I want to get execution time of a command but I also redirect output of command to a file. Also, I need to get exit code of this specific command to use it in if statement. Here is what I have tried:
...
TIMEFORMAT=%R
log_file() {
echo "$PROJECT_DIR/logs/$1_$(date +%Y%m%d_%H%M%S).log"
}
CODE_TIME=$(time sh -c "ipython -c "%run ./foo.ipynb" > $(log_file "log_file_name") 2>&1")
CODE_RESULT=$?
echo "Code_Result: $CODE_RESULT"
if [ "$CODE_RESULT" -eq 0 ]
...
According to documentation of time, it returns the exit code of the command but I also execute an echo command in function. So, anyone has an idea about how can I get exit code of ipython command? Thanks.
$SECONDSvariable, something simple like:start=$SECONDS; run-command; CODE_RESULT=$?; end=$SECONDS; duration=$((end-start))SECONDS=0; run-command; CODE_RESULT=$?; echo $SECONDS.$SECONDSvariable. So, I searched for an alternative andTIMEFORMAT=%Rworked for me to get seconds.