Mic*_*aël 10 shell scripting process zsh
我有一个程序P希望收到“你好”并输出“为什么?” 在提供功能之前。此功能被其他程序使用,这些程序不知道以“Hello”开始对话是一种常见的礼貌。因此,我想为P这样的工作编写一个包装器(zsh 语法):
coproc P
print -p Hello # Send Hello to P
read -pr line # Read what P has to say
[[ "$line" = "Why?" ]] && Replace current process with the coprocess.
echo Could not get P's attention.
Run Code Online (Sandbox Code Playgroud)
在部分(类似)中使用cat或 会导致不必要的缓冲。我有哪些选择?ddReplace...cat <&p &; exec cat >&p
Ven*_*atC -1
能够使用 perl 测试以下代码以避免缓冲,尝试这是否适合您
P 的样本版本
$ cat /tmp/P
#!/bin/bash
read input
if [[ $input = "Hello" ]]
then
echo "Why?"
else
exit 1
fi
echo "Got Hello from client, working ..."
sleep 10
echo "Need to read some input"
read x
echo "Got: $x"
Run Code Online (Sandbox Code Playgroud)
包装程序
$ cat /tmp/wrapper
#!/usr/bin/zsh
coproc /tmp/P
print -p Hello # Send Hello to P
read -pr line # Read what P has to say
if [[ "$line" = "Why?" ]]; then
perl -e '$|=1;print $_ while(<>);' <& p &
perl -e '$|=1;print $_ while(<>);' >& p
else
echo "Could not get P's attention."
fi
Run Code Online (Sandbox Code Playgroud)
测试运行
Run Code Online (Sandbox Code Playgroud)$ /tmp/wrapper Got Hello from client, working ... Need to read some input hi there P! <== Typed in at teminal Got: hi there P!