Reputation: 7
I looked all over online and can't seem to find anything that addresses what I am trying to do. I am using Bash on an Unbuntu VM. I created the following script
start_code=$(date +%H:%M:%S)
end_code=$(date +%H:%M:%S)
echo $start_code
for i in {1..1000};
do echo $RANDOM >> filename.txt;
done
echo $end_code
The code works fine, but is there any way that I can subtract the variables start_code from end_code??
I have tried this many different ways one being total_code=$(($start_code - $end_code)) but I get errors or nothing returned with everything that I have tried. As I'm brand new to Bash and I'm not even sure if I can do this. Any help would be greatly appreciated.
Upvotes: 0
Views: 285
Reputation: 212198
There are better ways to compute time lapsed. You can do the subtraction with something like:
$ cat a.sh
#!/bin/sh
foo() {
# This function does stuff
sleep ${1-2}
}
start=$(date +%s)
foo 3
end=$(date +%s)
echo "time lapsed: $((end - start))"
time foo 4
$ ./a.sh
time lapsed: 3
real 0m4.006s
user 0m0.001s
sys 0m0.001s
In the above, we first store a time stamp before calling the function foo
that just sleeps for 3 seconds. Then we compute a new time stamp and subtract. Then we do the same using the time
builtin and let the function sleep for 4 seconds.
Upvotes: 1