I am using How to define and load your own shell function in zsh to convert my zsh script to shell function.
The script I am trying to convert is:
#! /bin/zsh -
trap true INT QUIT
zmodload zsh/datetime
TIMEFMT='Total duration: %*E'
strftime -s start 'Start Time: %d/%m/%y %I:%M%P'
{
duration=$(
exec 4>&2 2>&1 1>&3 3>&-
time "$@" 2>&4 4>&-
)
} 3>&1
ret=$?
strftime -s end 'End Time: %d/%m/%y %I:%M%P'
echo -e
print -rlu2 $start $end $duration
return $ret
So, I removed the shebang and placed the script in my fpath
.
The problem is, it is no more showing the $duration
:
% reporttime tail -f ~/.xsession-errors
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
^C
Start Time: 03/01/21 03:15pm
End Time: 03/01/21 03:15pm
What can i do?
autoload reporttime
andreporttime sleep 1
orreporttime trail -f /etc/issue
interrupted with^C
with zsh 5.8. In which way is it not working for you? With what version ofzsh
? For(...)
, take the original script, add a(
line at the top and a)
line at the bottom. – Stéphane Chazelas Jan 03 '21 at 13:28(...)
was so that you can use the original script (the one that ends withexit
) asis without worrying about scope as a subshell runs in separate, child process inzsh
.print -l
prints its arguments one per line. See how I've added a''
here to first print an empty line. – Stéphane Chazelas Jan 03 '21 at 13:34zsh
aszsh
'stime
reports resources used by a process once that process terminates. To report resources of a process that runs azsh
function, you'd need totime
a subshell that runs that function. Here, you could replacetime "$@"
withtime ("$@")
for that. Or you can use the approach using$epochtime
in my other answer. – Stéphane Chazelas Jan 03 '21 at 13:38