I have a few alias' on my Dreadnaught embedded Linux system, some of them have a reasonably long list of jobs to do. I'm trying to improve the efficiency of these task, and trying to time how long each permutation took, so I can confirm which is the quickest permutation.
I've built my scripts up in parts, and found each part worked... but not when they were put together. I'll put the attempts at the end of the question so they don't flood the question out....
However, after hours trying to figure out why it appeared to work in parts but not as a whole, I've discovered that The command substitution is evaluated when you define the alias, not when you execute the it.
How can I create an alias that times itself for several tasks (calls to other alias', calls to system).
I tried using functions instead; equally unsuccessfully.
I tried time, but it doesn't like getting alias' passed to it; and my alias' call many other alias' and system calls, so it's not practical to unpack them.
Is there any way to create a bash alias (or other method that can be called from bash?) that times itself and/or other processes?
Update - possible solution, but looking for any better suggestions?
As I often find, writing out a question on SO makes me carefully explain it, which somehow triggers my head into thinking about alternatives I've not thought of previously. I've just written a bash script that calls the tasks - because it's not using alias' for the time records, it's calling the time functions directly:
#!/bin/bash
echo "== Task 1 Start =="
timerstart1=$(eval 'date +%s')
TimerStartstr1=$(eval 'date -d@$((timerstart1))')
echo "Task 1 started at $timerstart1 = $TimerStartstr1"
myTask1
timerend1=$(eval 'date +%s')
TimerEndstr1=$(eval 'date -d@$timerend1')
echo "Task 1 completed at $timerend1 = $TimerEndstr1"
duration1=$(($timerend1 - $timerstart1))
echo "=========== Task 1 Complete, took $duration1 =================="
echo "== Task 2 Start =="
timerstart2=$(eval 'date +%s')
TimerStartstr2=$(eval 'date -d@$((timerstart2))')
echo "Task 2 started at $timerstart2 = $TimerStartstr2"
MyTask2
timerend2=$(eval 'date +%s')
TimerEndstr2=$(eval 'date -d@$timerend2')
echo "Task 2 completed at $timerend2 = $TimerEndstr2"
duration2=$(($timerend2 - $timerstart2))
echo "=========== Task 2 Complete, took $duration2 =================="
echo "Speed improvement = $(($duration1 - $duration2))"
And, for some bonus schadenfreude: my "improved" Task2 actually took longer than the original - 83 seconds vs 70 seconds!
Here's the construction of the job in bits that (mostly) seemed to work on their own:
[user@Dreadnaught log]$ echo ==Start==; timerstart=$(eval 'date +%s'); TimerStartstr=$(eval 'date -d@$((timerstart))'); echo Started at \ \ $timerstart = $TimerStartstr;
==Start==
Started at 1710780061 = Mon Mar 18 16:41:01 GMT 2024
[user@Dreadnaught log]$ timerend=$(eval 'date +%s'); TimerEndstr=$(eval 'date -d@$timerend'); echo Completed at $timerend = $TimerEndstr;
Completed at 1710780075 = Mon Mar 18 16:41:15 GMT 2024
[user@Dreadnaught log]$ echo =========== TestTimerTask Complete, took $(($timerend - $timerstart)) ==================
=========== TestTimerTask Complete, took 14 ==================
[user@Dreadnaught log]$ alias timertest="echo ==Start==; timerstart=$(eval 'date +%s'); TimerStartstr=$(eval 'date -d@$((timerstart))'); echo Started at \ \ $timerstart = $TimerStartstr; read -p 'Waiting for input' >/dev/tty; timerend=$(eval 'date +%s'); TimerEndstr=$(eval 'date -d@$timerend'); echo Completed at $timerend = $TimerEndstr; echo =========== TestTimerTask Complete, took $(($timerend - $timerstart)) =================="
[user@Dreadnaught log]$ timertest
==Start==
-ash: Mar: not found
Started at 1710780061 = Mon Mar 18 16:41:01 GMT 2024
Waiting for input
-ash: Mar: not found
Completed at 1710780075 = Mon Mar 18 16:41:15 GMT 2024
=========== TestTimerTask Complete, took 14 ==================
[user@Dreadnaught log]$ timertest
==Start==
-ash: Mar: not found
Started at 1710780061 = Mon Mar 18 16:41:01 GMT 2024
Waiting for input
-ash: Mar: not found
Completed at 1710780075 = Mon Mar 18 16:41:15 GMT 2024
=========== TestTimerTask Complete, took 14 ==================
[user@Dreadnaught log]$ alias timertest="echo ==Start==; timerstart=$(eval 'date +%s'); TimerStartstr=$(eval 'date -d@$((timerstart))'); echo Started at \ \ $timerstart = $TimerStartstr; sleep 2; timerend=$(eval 'date +%s'); TimerEndstr=$(eval 'date -d@$timerend'); echo Completed at $timerend = $TimerEndstr; echo =========== TestTimerTask Complete, took $(($timerend - $timerstart)) =================="
[user@Dreadnaught log]$ timertest
==Start==
-ash: Mar: not found
Started at 1710780100 = Mon Mar 18 16:41:01 GMT 2024
-ash: Mar: not found
Completed at 1710780101 = Mon Mar 18 16:41:15 GMT 2024
=========== TestTimerTask Complete, took 1 ==================
[user@Dreadnaught log]$
[user@Dreadnaught log]$ alias timerstart="f(){ timerstart=$(eval 'date +%s'); TimerStartstr=$(eval 'date -d@$((timerstart))'); echo Started at \ \ $timerstart = $TimerStartstr; unset -f f; };f"
[user@Dreadnaught log]$ alias timerend="f(){ timerend=$(eval 'date +%s'); TimerEndstr=$(eval 'date -d@$((timerend))'); echo Finished at \ \ $timerend = $TimerEndstr; unset -f f; };f; echo =========== TestTimerTask Complete, took $(($timerend - $timerstart)) =================="
[user@Dreadnaught log]$ alias timertest="timerstart; read -p 'Waiting for input' >/dev/tty; timerend"
[user@Dreadnaught log]$ timertest
-ash: Mar: not found
Started at 1710780677 = Mon Mar 18 16:41:01 GMT 2024
Waiting for input
-ash: Mar: not found
Finished at 1710780677 = Mon Mar 18 16:41:15 GMT 2024
=========== TestTimerTask Complete, took 0 ==================
[user@Dreadnaught log]$
It sounds like you're using aliases when you should be using functions and have a few other issues such as calling
evalfor no apparent reason. I THINK this is what you're trying to do: