#unixshellprogramming — Public Fediverse posts
Live and recent posts from across the Fediverse tagged #unixshellprogramming, aggregated by home.social.
-
Here is a "sketch of concept".
I am not satisfied with it, but it may be better than nothing.
(Relying on a DEBUG trap isn't really a good thing here, in my view.)(1) Example transcript:
$ echo hello
hello
Ready(0); E=0.00 s 10:21:42
$ sleep 1.5
Ready(0); E=1.50 s 10:22:06
$
Ready(0); E=1.50 s 10:22:07
$ false | sleep 3.456
Ready(1 0); E=3.46 s 10:22:47
$
Ready(1 0); E=3.46 s 10:22:50
$ :
Ready(0); E=0.00 s 10:22:55
$
Ready(0); E=0.00 s 10:23:01(2) Bash source for the above:
### Print the elapsed time for the previous command before each bash prompt.
### Uses a DEBUG trap and the PROMPT_COMMAND variable.prompt__elapsed_time=""; #Keeping state.
function prompt__print_prompt () {
declare -r s="${PIPESTATUS[*]}";
if [[ "${prompt__elapsed_time}" == "" ]]; then
## The prompt does not change on just pressing Return (no command).
prompt__elapsed_time="$(printf '%s - %s\n' "${EPOCHREALTIME}" "${prompt__start_time}" | bc)";
fi;
## Imitate IBM z/VM's CMS (partly: the latter prints user time and system time):
printf 'Ready(%s); E=%.2f s %s\n' "${s}" "${prompt__elapsed_time}" "$(date +%H:%M:%S)";
}function prompt__set_time () {
if [[ "${BASH_COMMAND}" != "prompt__print_prompt" ]]; then
prompt__elapsed_time="";
prompt__start_time=${EPOCHREALTIME};
fi;
}trap prompt__set_time DEBUG;
PROMPT_COMMAND=prompt__print_prompt; -
Here is a "sketch of concept".
I am not satisfied with it, but it may be better than nothing.
(Relying on a DEBUG trap isn't really a good thing here, in my view.)(1) Example transcript:
$ echo hello
hello
Ready(0); E=0.00 s 10:21:42
$ sleep 1.5
Ready(0); E=1.50 s 10:22:06
$
Ready(0); E=1.50 s 10:22:07
$ false | sleep 3.456
Ready(1 0); E=3.46 s 10:22:47
$
Ready(1 0); E=3.46 s 10:22:50
$ :
Ready(0); E=0.00 s 10:22:55
$
Ready(0); E=0.00 s 10:23:01(2) Bash source for the above:
### Print the elapsed time for the previous command before each bash prompt.
### Uses a DEBUG trap and the PROMPT_COMMAND variable.prompt__elapsed_time=""; #Keeping state.
function prompt__print_prompt () {
declare -r s="${PIPESTATUS[*]}";
if [[ "${prompt__elapsed_time}" == "" ]]; then
## The prompt does not change on just pressing Return (no command).
prompt__elapsed_time="$(printf '%s - %s\n' "${EPOCHREALTIME}" "${prompt__start_time}" | bc)";
fi;
## Imitate IBM z/VM's CMS (partly: the latter prints user time and system time):
printf 'Ready(%s); E=%.2f s %s\n' "${s}" "${prompt__elapsed_time}" "$(date +%H:%M:%S)";
}function prompt__set_time () {
if [[ "${BASH_COMMAND}" != "prompt__print_prompt" ]]; then
prompt__elapsed_time="";
prompt__start_time=${EPOCHREALTIME};
fi;
}trap prompt__set_time DEBUG;
PROMPT_COMMAND=prompt__print_prompt;