是否有一种方法可以使用命令leave和命令say (MacOS)来通知内置语音系统?
我可以在命令行上执行echo hello | say。
leave输出Time to leave!,当它出现时,如何将该输出管道传输到say?
当我发出这个命令的时候,它只是悬着。(没有创建休假流程)
# one minute from now
leave +0001
Alarm set for Fri Jun 3 15:55:05 CEST 2022. (pid 37692)
pgrep leave
37692
# triggers: Time to leave!
# one minute from now piped to say
# this one is just hanging there.... (no leave process created)
leave +0001 | sayman离开
LEAVE(1) BSD General Commands Manual LEAVE(1)
NAME
leave -- remind you when you have to leave
SYNOPSIS
leave [[+]hhmm]
DESCRIPTION
The leave utility waits until the specified time, then reminds you that you have to leave. You are reminded 5 minutes and 1 minute before the actual time, at
the time, and every minute thereafter. When you log off, leave exits just before it would have printed the next message.
The following options are available:
hhmm The time of day is in the form hhmm where hh is a time in hours (on a 12 or 24 hour clock), and mm are minutes. All times are converted to a 12 hour
clock, and assumed to be in the next 12 hours.
+ If the time is preceded by `+', the alarm will go off in hours and minutes from the current time.
If no argument is given, leave prompts with "When do you have to leave?". A reply of newline causes leave to exit, otherwise the reply is assumed to be a
time. This form is suitable for inclusion in a .login or .profile.
To get rid of leave you should either log off or use `kill -s KILL' giving its process id.
SEE ALSO
calendar(1)
HISTORY
The leave command appeared in 3.0BSD.
BSD April 28, 1995 BSD谢谢!
发布于 2022-06-03 13:54:16
听起来leave每天写一行输出。如果say试图一次读取所有stdin (或者以其他方式进行任何类型的读取,而不是在第一个换行符处停止一个字符的读取),则缓冲区将永远不够满,无法在合理的时间内完成其读取。
bash read内置程序进行这些(低效的)单字符实时读取,因此能够更恰当地从管道中获取内容(只要leave覆盖libc的默认缓冲行为,即在输出不直接到TTY时从行缓冲切换到完全缓冲;但如果它不这样做,则应该向操作系统供应商报告)。
为say的每一行输出运行一个新的leave副本
leave | while IFS= read -r line; do say <<<"$line"; done要等待leave有一些输出,只运行say一次,然后退出:
leave | { IFS= read -r line; say <<<"$line"; }所有这些都可以放在背景中,如果你选择的话。例如:
{ leave | { IFS= read -r line; say <<<"$line"; }
} </dev/null >leave-say.log 2>&1 & disown -h "$!"...will相当于在后台使用nohup运行上述代码,将任何错误写入leave-say.log而不是nohup.out。
发布于 2022-06-03 14:22:49
leave所做的是分叉一个后台进程,给出STDOUT上留下的消息。完成分叉后,子进程从其父进程继承STDOUT。当父进程死亡时,子进程在管道中保持下一个进程的STDIN打开。因此,
leave +0002 | sed 's/o/OOOO/g'似乎要一直挂到孩子的进程结束。
正确的解决方案是将管道的其余部分作为背景:
leave +0002 | sed 's/o/OOOO/g' &从而产生:
Just OOOOne mOOOOre minute!
Time tOOOO leave!或者,在你的情况下:
leave +0002 |say &杀死子进程将停止say表单的“挂起”。子程序的PID在消息Alarm set for xx.xx. (pid 3311636)中给出。您可以通过从另一个窗口杀死孩子来验证这一点。
并不是说,如果你不离开,孩子的进程就不会消亡,而是会时不时地唠叨。这意味着say将“
<代码>H 117如“离开时间”(来自子程序的输出)<代码>H 218<代码>H 119挂起另一分钟<代码>H 220<代码>H 121表示“离开时间”(从子代码输出)H 222f 223/code>>
(最后两行重复,直到孩子被杀)
这与say的Macos手册也是一致的,该手册指出:
如果输入是TTY,或者没有指定文本,则键入的输入文本将逐行
。
https://stackoverflow.com/questions/72490476
复制相似问题