我想運行nc通過&,然後手動從/ proc文件系統提供數據在標準輸入時,只要我想。所以問題是:bash運行與&符號終止程序
,如果我跑nc 127.0.0.1 1234 &
程序在後臺運行,我可以在標準輸入任何我想要寫。但是,如果我創建test.sh並添加
#!/bin/bash
nc 127.0.0.1 1234 &
sleep 20
它連接到1234並立即終止(甚至不等待20秒)。爲什麼?我懷疑它是從某處寫入的stdin。
我想運行nc通過&,然後手動從/ proc文件系統提供數據在標準輸入時,只要我想。所以問題是:bash運行與&符號終止程序
,如果我跑nc 127.0.0.1 1234 &
程序在後臺運行,我可以在標準輸入任何我想要寫。但是,如果我創建test.sh並添加
#!/bin/bash
nc 127.0.0.1 1234 &
sleep 20
它連接到1234並立即終止(甚至不等待20秒)。爲什麼?我懷疑它是從某處寫入的stdin。
有趣的問題的管道製造。
bash的手冊頁指出:
If a command is followed by a & and job control is not active, the
default standard input for the command is the empty file /dev/null.
Otherwise, the invoked command inherits the file descriptors of the
calling shell as modified by redirections.
如果調用nc 127.0.0.1 1234 < /dev/null
一個shell腳本(作業控制)之外,將導致相同的。
你可以改變你的bash腳本這樣的,使其工作:
#!/bin/bash
nc 127.0.0.1 1234 < /dev/stdin &
sleep 20
如果我的目的是正確的,你想手動將數據提交給nc,然後發送給客戶端。
您可以使用命名管道來達到此目的。
cat /tmp/f | ./parser.sh 2>&1 | nc -lvk 127.0.0.1 1234 > /tmp/f
其中/tmp/f
是使用mkfifo /tmp/f
不管你想要喂nc
可以回聲版在parser.sh
感謝給另一種解決方案,但更重要的是我想了解爲什麼NC在我的情況 –
嘗試標誌'-lk'終止其允許它一收到一個連接就立即終止,並等待其他連接@JaniBaramidze –
謝謝你的好解釋! :) –