首页 > 解决方案 > 在 bash 中同时运行两个命令

问题描述

代码:

#!/bin/bash
nc ipv4 port echo "hi"

问题:如何让它在连接到听者后立即自动说 Hi?

标签: bash

解决方案


如果您想在命令后保持连接打开,可以使用 fifo

mkfifo fifo_filename

# start nc reading from fifo in background (jobs,kill can be used to monitor)
nc host port <fifo_filename &

# open file descriptor 3 to write to fifo
exec 3>fifo_filename 
echo hi >&3
...
echo bye >&3

# close file descriptor (will close the fifo and background nc will exit)
exec 3>&-

推荐阅读