將資料寫入檔案和序列埠 - 我可以使用 tee 嗎?

將資料寫入檔案和序列埠 - 我可以使用 tee 嗎?

我試圖從串行端口獲取輸入並將其寫入文件,然後讀取該文件並將其從串行端口發送回主機。一位同事建議使用“tee”命令,但我找不到一個好的範例/圍繞該命令思考。可以用「tee」來做到這一點嗎?似乎 tee 只能執行一個命令,例如將文件定位到不同的位置,但不能定位連接埠然後寫入文檔,然後讀取文檔,然後透過連接埠發送它。或者我只是不理解“tee”命令的基礎知識。

答案1

tee命令將輸入同時寫入標準輸出和檔案。一個簡單的例子是

$ echo "Hi there..."|tee -a hi.txt
Hi there....
$cat hi.txt
Hi there....

在上面的範例中,它在 STDOUT 中呈現文字並將其寫入hi.txt.另一個例子可能是

$cat hi.txt|tee -a final.txt
Hi there.....
$cat final.txt
Hi there....

所以考慮 bash shell,你的例子可能是-

$cat ./serial-port|tee -a <filename>

所以如果串列埠是 20002 那麼上面的指令看起來像

$cat $serial-port|tee -a serial.txt
20002
$cat serial.txt
20002

相關內容