Better KSH commands

Are there any documents available for checking the execution time taken by ksh commands?

My requirement is to fine tune a set of shell scripts having lot of "echos" and "date"s.
Is there a better replacement for the below code?.

 
echo "ABC process started on `date`"
some code..
echo "ABC process ended on `date`"
echo "BAC process started on `date`"
some code
echo "BAC process ended on `date`"
.......................
.......................
Thus it goes on...

These lines with very less difference is repeated more than 3000 times in my shell script.Please post any method by which I can reduce the execution time.

We don't know what

process command 

is doing. Could you please explain that?

Check this command for capturing time taken to run a process

time

"process commands" are the other lines in my shell script through which I complete my tasks. Its not code.

I guess your initial question was optimization of the script. In that case, without knowing what " process command " is doing, its not possible to provide suggestions on script optimization. Sorry if process optimization is not ur question :slight_smile:

Sorry if I created any confusion here. I will explain my query.

I have these similar lines of code all through my korn shell script(my_script.sh). I have many shell scripts (ABC.sh,BAC.sh,CAC.sh,LKJ.sh,POI.sh and many hundreds) which is run through this parent shell script.
Below is my_script.sh:

echo "ABC.sh started at `date`"
sh ABC.sh
echo "ABC.sh ended at`date`"
echo "BAC.sh started at `date`"
sh BAC.sh 
echo "BAC.sh ended at `date`"
.......................
.......................

Thus it goes on for other files too...

Can I reduce the execution time of my parent script by changing the echo command or date command? I have heard that cat is a better replacement for echo. But will it work in this case?

3000 lines with `date` in them means you create 3000 child processes - one for each date command. This is very expensive timewise.

Probably you could try embedding these multiple commands into a perl script and make use of localtime() instead, so that you dont have to call date command 3000 times or more

If you are using cat, essentially you will have to push that to a file and display from that, am not sure why do you want to do that!!!

Multiple "echo" commands may be replaced by a "here-document".
This makes the script faster.

Example:

echo "Please enter your choice:"
echo "1 - list current directory"
echo "2 - list current users"
echo "3 - log off"

may be replaced with

cat <<!

Please enter your choice
1 - list current directory
2 - list current users
3 - log off
!
courtesy: SHELLdorado - Shell Tips & Tricks (Programmer)

How is this related to your requirement?

You need to display statements with timestamp after each and every process is completed

ksh supports coprocesses.

#!/bin/ksh
# mycoprocess.shl
while read value 
do
date
done
#!/bin/ksh
# your script
./mycoprocess.shl |&

get_date()
{
      print -p 'x'
      read -p dt
      echo $dt
}

call get_date anytime you need a timestamp. You did not post anything else, but if you did the same bactic trick you could modify your coprocess.shl to give multiple responses

BTW - this still leaves one level of child process creation; every date call executes an image. bash has a feature that allows you to create native (i.e., linked into bash) functions in bash written in C, if I recall correctly. That would further improve things.

If you are using ksh93, here is how to avoid forking a process to return the date.

$ printf "ABC.sh started at %(%H:%M:%S)T\n"
ABC.sh started at 07:43:06
printf "ABC.sh started at %(%H:%M:%S)T\n"

or

date "+ABC.sh started at %T"

You can also use the ksh/bash built-in variable SECONDS to keep track run time of each process.

Thanks binlib