2011-07-07 34 views
0

我正在使用RedHat EL 4.我使用Bash 3.00.15。使用命名管道創建讀/寫環境

我在寫SystemVerilog,我想模擬stdin和stdout。我只能使用文件,因爲環境中不支持標準stdin和stdout。我想用命名管道來模擬stdin和stdout。

我明白如何使用mkpipe創建to_sv和from_sv文件,以及如何打開它們並在SystemVerilog中使用它們。

通過使用「cat> to_sv」,我可以輸出字符串到SystemVerilog仿真。但是,這也會輸出我在shell中輸入的內容。

我想,如果可能的話,它會像一個UART終端一樣運行一個shell。無論我輸入什麼,都直接輸出到「to_sv」,並且任何寫入「from_sv」的內容都會打印出來。

如果我正在談論這個完全錯誤的話,那麼一定建議正確的方法!謝謝你這麼多,

Nachum Kanovsky

回答

0

你可能想使用exec爲:

exec > to_sv 
exec < from_sv 

見第19.1。19.2。在Advanced Bash-Scripting Guide - I/O Redirection

+0

我剛纔試了一下速度非常快。它似乎還沒有爲我工作。我試過的是'exec> to_sv&',然後'exec to_sv作業也退出。 – nachum

2

編輯:你可以輸出到一個命名管道,並從另一個終端讀取。您還可以使用stty -echo禁用鍵以回顯到終端。

mkfifo /tmp/from 
mkfifo /tmp/to 
stty -echo 
cat /tmp/from & cat > /tmp/to 

白衣你寫的這個命令一切都將/tmp/to並沒有呼應,一切都寫入/tmp/from將呼應。

更新:我發現一種方法可以將每個輸入的字符一次發送到/ tmp /中。取而代之的cat > /tmp/to用這個命令:

while IFS= read -n1 c; 
do 
    if [ -z "$c" ]; then 
     printf "\n" >> /tmp/to; 
    fi; 
    printf "%s" "$c" >> /tmp/to; 
done 
+0

如果可能的話,我希望在單個shell中指示兩個方向。像UART終端一樣工作。我輸入的內容不應該回顯。我相信在cat>/tmp/a解決方案中,角色都會回顯。 – nachum

+0

@ user832745我更新了我的答案。起初我不明白你需要什麼。可能是因爲我從來沒有使用過真正的終端,只有終端仿真器。 [UART](http://en.wikipedia。org/wiki/Universal_asynchronous_receiver/transmitter)對我來說並沒有多大意義,可能是因爲我的軟件背景不止是電子/硬件背景。現在我希望我的回答能夠符合你所描述的內容 – Lynch

+0

這非常有幫助!謝謝。它正在工作。角色可以無緩衝發送嗎?目前我在終端中輸入的內容是/ tmp/to,只有在按下回車鍵後纔會發送這些字母。我怎樣才能讓每個按鍵都沒有被緩衝? – nachum

0

相反的cat /tmp/from &你可以使用tail -f /tmp/from &(至少在這裏在Mac OS X 10.6.7這防止了僵局,如果我echo不止一次/tmp/from)。

基於證券代碼:

# terminal window 1 
(
rm -f /tmp/from /tmp/to 
mkfifo /tmp/from 
mkfifo /tmp/to 
stty -echo 
#cat -u /tmp/from & 
tail -f /tmp/from & 
bgpid=$! 
trap "kill -TERM ${bgpid}; stty echo; exit" 1 2 3 13 15 
while IFS= read -n1 c; 
do 
    if [ -z "$c" ]; then 
    printf "\n" >> /tmp/to 
    fi; 
    printf "%s" "$c" >> /tmp/to 
done 
) 

# terminal window 2 
(
tail -f /tmp/to & 
bgpid=$! 
trap "kill -TERM ${bgpid}; stty echo; exit" 1 2 3 13 15 
wait 
) 

# terminal window 3 
echo "hello from /tmp/from" > /tmp/from