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 reporttimeandreporttime sleep 1orreporttime trail -f /etc/issueinterrupted with^Cwith 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 -lprints 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:34zshaszsh'stimereports resources used by a process once that process terminates. To report resources of a process that runs azshfunction, you'd need totimea subshell that runs that function. Here, you could replacetime "$@"withtime ("$@")for that. Or you can use the approach using$epochtimein my other answer. – Stéphane Chazelas Jan 03 '21 at 13:38