重定向应用程序(java)的输入,但仍允许BASH中的stdin

|| 我有点困惑,我昨天做了这个工作,但是它几乎停止了重定向标准输入,几乎是不可思议的。
set -m
mkfifo inputfifo
mkfifo inputfifo_helper
((while true; do cat inputfifo; done) > inputfifo_helper)&
trap \"rm -f inputfifo inputfifo_helper java.pid; kill $!\" EXIT

exec 3<&0
(cat <&3 > inputfifo)&

NOW=$(date +\"%b-%d-%y-%T\")

if ! [ -d \"logs\" ]; then
    mkdir logs
fi

if [ -f \"server.log\" ]; then
    mv server.log logs/server-$NOW.log
fi
java <inputfifo_helper -jar $SERVER_FILE & echo $! > java.pid && fg
运行得很好,我可以将内容回显到inputfifo,应用程序将其获取,也可以直接在其控制台中键入内容。它甚至可以通过屏幕工作。绝对没有代码方面的变化,但是重定向的stdin已停止工作。我尝试将文件描述符更改为9,甚至127,但均未修复。 我忘记了什么吗?是否有特定原因导致它坏了并且不再起作用? (我使用此方法不是将输入发送到屏幕本身,因为我是分开打开屏幕的,并且除非接收到至少一次附加,否则它拒绝接收输入,我不知道这是错误还是意)     
已邀请:
如果您可以将Java程序置于后台,则可以尝试从控制终端
/dev/tty
进行读取,并使用while-read循环将其写入inputfifo。
# ...
java <inputfifo_helper -jar $SERVER_FILE & echo $! > java.pid

while IFS=\"\" read -e -r -d $\'\\n\' -p \'input> \' line; do
  printf \'%s\\n\' \"${line}\"
done </dev/tty >inputfifo
    
这是预感..但fd 0可能还附有其他内容吗? 在我的Linux上,我看到了
$ ls -l /dev/fd/
total 0
lrwx------ 1 nhed nhed 64 Mar 24 19:15 0 -> /dev/pts/2
lrwx------ 1 nhed nhed 64 Mar 24 19:15 1 -> /dev/pts/2
lrwx------ 1 nhed nhed 64 Mar 24 19:15 2 -> /dev/pts/2
lr-x------ 1 nhed nhed 64 Mar 24 19:15 3 -> /proc/6338/fd
但是在随后的所有ls中,fd3指向的proc#是不同的-我不知道这是什么(也许与我的提示命令有关),但是采用了fd 3,请尝试使用fds#5-9 (并在脚本顶部添加ѭ4进行诊断)     
运行给定代码的简化版本会显示I / O错误消息:
cat: stdin: Input/output error
一个快速的解决方案是将该命令的stderr重定向到/ dev / null。 在Mac OS X / FreeBSD上,您也可以尝试使用\“ cat -u \”禁用输出缓冲(从而避免出现cat输出缓冲问题)。
rm -v inputfifo inputfifo_helper
mkfifo inputfifo inputfifo_helper

(
((while true; do cat inputfifo; done) > inputfifo_helper) &
# use of \"exec cat\" terminates the cat process automatically after command completion
#((while true; do exec cat inputfifo; done) > inputfifo_helper) &
pid1=$!
exec 3<&0  # save stdin to fd 3
# following command prints: \"cat: stdin: Input/output error\"
#(exec cat <&3 >inputfifo) &
(exec cat <&3 >inputfifo 2>/dev/null) &
pid2=$!
# instead of: java <inputfifo_helper ...
(exec cat <inputfifo_helper) &
pid3=$!
echo $pid1,$pid2,$pid3   
lsof -p $pid1,$pid2,$pid3
echo hello world > inputfifo
)


# show pids of cat commands
ps -U $(id -u) -axco pid,command | grep cat | nl    # using ps on Mac OS X
    
尝试使用单个fifo并将内容回显到r / w文件描述符。 使用ASCII NUL字符终止输入(行),以便 读取命令将继续读取,直到NULL字节(或EOF)为止。
rm -v inputfifo 
mkfifo inputfifo
(
exec 0>&-
exec 3<>inputfifo   # open fd 3 for reading and writing
echo \"hello world 1\" >&3
echo \"hello world 2\" >&3
printf \'%s\\n\\000\' \"hello world 3\" >&3
# replaces: java <inputfifo_helper ...
cat < <(IFS=\"\" read -r -d \'\' <&3 lines && printf \'%s\' \"$lines\")
)
    

要回复问题请先登录注册