Code Comments
Programming Forum and web based access to our favorite programming groups.> ...
> 1) awk -F: '$1 ~ /'"$USER"'/ {print $5}' /etc/passwd
> ...
Option 1) in combination with a fifo can be used to pass arbitrary
data. I have the following demo script:
_1 #! /usr/bin/bash
_2 NLVAR=$(echo -e line1\\nline2)
_3 FIFONAME=iofifo
_4 [ ! -e $FIFONAME ] && mkfifo $FIFONAME
_5 echo dummy | awk '
_6 BEGIN {
_7 RS = "";
_8 print "clear" > "'$FIFONAME'";
_9 close("'$FIFONAME'");
10 getline var1 < "'$FIFONAME'";
11 close("'$FIFONAME'");
12 print "var1:", var1;
13 }
14 END {print "hello from END via fifo" > fifo;
15 close(fifo);
16 getline < fifo;
17 close(fifo);
18 print "all done via fifo" > fifo;
19 close(fifo);
20 }' fifo="iofifo" &
21 IOVAR=$(< iofifo)
22 echo "$NLVAR" > iofifo
23 IOVAR=$(< iofifo)
24 echo $IOVAR
25 echo 'sync' > iofifo
26 IOVAR=$(< iofifo)
27 echo $IOVAR
28 rm iofifo
Output:
var1: line1 <------------- this
line2 <------------------- and this are var1
hello from END via fifo <- sent out over fifo
all done via fifo <------- also sent out over fifo
Notes:
line 2: variable with embedded newline
line 7: needed to allow reading of embedded-newline variable
line 8: always open write end of fifo first
line 12: print embedded-newline var read over fifo
line 14: send data out over the fifo
line 16: a sync read to allow a subsequent write
line 18: second write
line 21: read from line 8
line 22: write to line 10
line 23: read from line 14
line 25: write to line 16
line 26: read from line 18
A couple of things I've learned: first, the write end of a fifo always
seems to need to be opened before the read end, thats why the first io
statment in the BEGIN block is a dummy print statment at line 8. To
write to two separate & subsequent external reads, you have to put a
read between otherwise the two writes go to the first external read.
ie the writes in lines 14 & 18 are separated by a read at line 16,
otherwise the two writes would go to the read at line 23 and the read
at line 26 would hang waiting for data.
Patrick
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.