'How to measure execution time in a shell script in Python?
I created this sh script and want to measure time spent on the program:
#!/bin/bash
start=`date +%s`
cd nlu/creator
python3 move_file.py ../../../../../base_data/ ../../resources/kb/
python3 converter.py
python3 transformer.py
cd ../../resources/kb/
find . -name '*.xml' | xargs -I{} rm -rf {}
find . -name '*Object.txt' | xargs -I{} rm -rf {}
end =`date +%s`
runtime=$((end-start))
echo "Building time: ${runtime}"
I execute:
nlu/creator/builder.sh
The error message is:
nlu/creator/builder.sh: line 15: end: command not found
Building time: -1651032434
why does it complain about 'end:command not found'?
Also, why is the time a negative number? I am on a Mac.
Solution 1:[1]
why does it complain?
This is general shell script syntax:
In general, P X Y runs the command P with the two arguments X and Y. Similarily, end = something runs the command end with the two arguments = and something.
Assignment is done by VAR=VALUE, for instance
end=something
If you one day need to execute a command named end=something (i.e. not treat it as assignment, you still can do it by writing
'end=something'
BTW: If you are only interested in knowing the time and are not picky about a particular format of how the time is printed, you could also remove your timing calculation from the script completely, and run the script with the time builtin:
time nlu/creator/builder.sh
Give it a try!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user1934428 |
