0

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

3 Answers 3

2

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
5
  • What I would like is both directions within a single shell if possible. Like a UART terminal would work. What I type shouldn't be echoed back. I believe with the cat > /tmp/a solution, the characters are all echoed back.
    – nachum
    Jul 7, 2011 at 6:34
  • @user832745 I updated my answer. At first I did not understand correctly what you needed. May be because I have never used a real terminal, only terminal emulator. The UART thing did not mean much to me either, may be because I have a software background more than an electronical/hardware background. Now I hope my answer does what you describe.
    – Lynch
    Jul 7, 2011 at 16:17
  • That was very helpful! Thank you. It is working now. Can the characters be sent unbuffered? Currently what I type in the terminal, which goes to /tmp/to, the letters are only sent once I press enter. How can I get each keypress to go without being buffered?
    – nachum
    Jul 7, 2011 at 20:16
  • Its hard to find a way of sending every keys one at a time. cat, dd seam to wait for the \n even if you manage to read only one char at a time using read -n1 c; echo "$c", it does not handle spaces or newlines. Ill check again see if I can find. May be a curse software? or a terminal like minicom will do.
    – Lynch
    Jul 7, 2011 at 20:59
  • I have updated my solution to send every chars one at a time to the output.
    – Lynch
    Jul 7, 2011 at 21:27
0

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

1
  • I have just tried it very quickly. It doesn't yet seem to be working for me. What I tried is 'exec > to_sv &' and then 'exec < from_sv &'. I then went to another shell and typed 'cat to_sv'. This just exited, and caused the exec > to_sv job from the other shell to exit also.
    – nachum
    Jul 7, 2011 at 6:30
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.