'time' does NOT work on a function in 'dash'.

Hi guys and gals...

I am writing a piece of code that is dash compliant and came across this error.
I have put it in the OSX section as that is what I am using.
I have no idea what the 'dash' version is but was installed about 6 months ago.

MBP, OSX 10.12.6, default terminal running dash on top of default bash.

#!/usr/local/bin/dash

delayme()
{
	echo "Sleep for 1 second."
	sleep 1
}

echo "Test the function."
delayme

echo "Test the timer."
time delayme

echo ""
echo "Test a real command."
time sleep 1

Result...

Last login: Mon Sep 11 19:10:20 on console
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./dash_function.sh
Test the function.
Sleep for 1 second.
Test the timer.
delayme: No such file or directory
        0.00 real         0.00 user         0.00 sys

Test a real command.
        1.00 real         0.00 user         0.00 sys
AMIGA:amiga~/Desktop/Code/Shell> _

I have no idea if this error also exists with Linux or other UNIX like flavours.
Does anyone know the reason why this error occurs using 'time' on the _function_ as the code works perfectly in 'bash'.
I can't find anything about it on the WWW.

TIA.
Bazza...

probably cannot 'time' a function in dash - dunno.....

mute@zbox:~$ type time
time is a shell keyword
mute@zbox:~$ dash
$ type time
time is /usr/bin/time

Look at dash manual. It doesn't provide a time. bash has a built-in time, so it can time things like functions. Since dash doesn't provide it, you're using an external program called time . It cannot call shell functions.

2 Likes

Hi neutronscott...

Thanks, excellent reply.

I also tested the external command and it does not work in 'bash' either.

It never occurred to me that the external command would not work on a function.

Again thanks.

Bazza.

Hi.

This seems to be not time-critical, so you could write a nonce-script, like so on file z7:

#!/usr/bin/env dash
#!/usr/local/bin/dash

cat >delay.sh  <<EOF
#!/usr/bin/env dash
delayme()
{
        echo "Sleep for 2 second."
        sleep 2
}
delayme
EOF
chmod +x delay.sh

echo "Test the function."
# delayme
./delay.sh

echo "Test the timer."
# time ./delayme
time ./delay.sh

echo ""
echo "Test a real command."
time sleep 1

producing:

$ ./z7
Test the function.
Sleep for 2 second.
Test the timer.
Sleep for 2 second.
        2.01 real         0.00 user         0.00 sys

Test a real command.
        1.01 real         0.00 user         0.00 sys

On a system like:

OS, ker|rel, machine: Apple/BSD, Darwin 16.7.0, x86_64
Distribution        : macOS 10.12.6 (16G29), Sierra
dash - ( local: /usr/local/bin/dash, 2017-09-12 )

Best wishes ... cheers, drl