【发布时间】:2018-08-28 04:02:53
【问题描述】:
我想要的是类似于下面这个 bash shell 脚本的东西:
壳牌重击
#!/bin/bash
# shell timer
# Note: Do not measure time precisely because there is loss in calculations and other commands
# For a human being is something almost imperceptible, fortunately.
# ------------------------------------------------- -----------------------------
s=00
m=00
h=00
key=""
function _screen() {
clear
# Shows the elapsed time on the terminal screen and plays to the time.txt file always updating
printf "%02d:%02d:%02d" $h $m $s > ~/time.txt
echo ":: 'p' to pause, 'c' to continue and 's' to exit ::"
}
function _time() {
_screen
sleep 1
s=$((s+1))
[ $s -eq 60 ] && m=$((m+1)) && s=00
[ $m -eq 60 ] && h=$((h+1)) && m=00
}
function _pause() {
while :
do
_screen
sleep 1
read key
[ "$key" = "c" ] && clear && break
done
}
function _main() {
# Put the terminal in special character interpretation mode
stty -echo -icanon min 0
while :
do
[ "$key" = "s" ] && break
[ "$key" = "p" ] && _pause
_time
read key
done
# Restores the default mode
stty sane
exit 0
}
_main
也许最明显的是将其转换为 Tcl/Tk。我什至尝试过,但我仍然没有成功。见:
Shell Tclsh
#!/usr/bin/env tclsh
# shell timer
# Note: Do not measure time precisely because there is loss in calculations and other commands
# For a human being is something almost imperceptible, fortunately.
# ------------------------------------------------- -----------------------------
set s 00
set m 00
set h 00
puts -nonewline ""
flush stdout
set key [gets stdin]
proc _screen{ } {
clear
set archive [open [pwd]/time.txt w]
# Shows the elapsed time on the terminal screen and plays to the time.txt file always updating
puts $archive "%02d:%02d:%02d" $h $m $s"
puts -nonewline ":: 'p' to pause, 'c' to continue and 's' to exit ::"
}
proc _time{ } {
_screen
after 1000
s=[expr s+1]
if { $s -eq 60 } { m=[expr m+1] } { s=00 }
if { $m -eq 60 } { h=[expr h+1] } { m=00 }
}
proc _pause{ } {
while { 1 }
{
_screen
after 1000
$argv key
if { "$key" = "c" } { break }
}
}
proc _main{ } {
# Put the terminal in special character interpretation mode
stty -echo -icanon min 0
while { 1 }
{
if { "$key" = "s" } { break }
if { "$key" = "p" } { _pause }
_time
$argv key
}
# Restores the default mode
stty sane
close $archive
exit 0
}
after 1000 _main
我仍然致力于并为此努力与引用的示例相同 - bash 脚本。但不排除您可以推广的改进和建议。
我的想法是这样的:
如果这里有人知道并想分享这个想法,请随意。
【问题讨论】: