create read/write environment using named pipes

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 09:27:26

问题


I am using RedHat EL 4. I am using Bash 3.00.15.

I am writing SystemVerilog and I want to emulate stdin and stdout. I can only use files as the normal stdin and stdout is not supported in the environment. I would like to use named pipes to emulate stdin and stdout.

I understand how to create a to_sv and from_sv file using mkpipe, and how to open them and use them in SystemVerilog.

By using "cat > to_sv" I can output strings to the SystemVerilog simulation. But that also outputs what I'm typing in the shell.

I would like, if possible, a single shell where it acts almost like a UART terminal. Whatever I type goes directly out to "to_sv", and whatever is written to "from_sv" gets printed out.

If I am going about this completely wrong, then by all means suggest the correct way! Thank you so much,

Nachum Kanovsky


回答1:


Edit: You can output to a named pipe and read from an other one in the same terminal. You can also disable keys to be echoed to the terminal using stty -echo.

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

Whit this command everything you write goes to /tmp/to and is not echoed and everything written to /tmp/from will be echoed.

Update: I have found a way to send every chars inputed to the /tmp/to one at a time. Instead of cat > /tmp/to use this command:

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



回答2:


You probably want to use exec as in:

exec > to_sv
exec < from_sv

See sections 19.1. and 19.2. in the Advanced Bash-Scripting Guide - I/O Redirection




回答3:


Instead of cat /tmp/from & you may use tail -f /tmp/from & (at least here on Mac OS X 10.6.7 this prevented a deadlock if I echo more than once to /tmp/from).

Based on Lynch's code:

# 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


来源:https://stackoverflow.com/questions/6605477/create-read-write-environment-using-named-pipes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!