Grab exactly one byte from a FIFO, at random intervals
I want to develop a script of the following form:
Code:
#!/bin/bash
# Function 'listen' opens a data stream
# which stores all incoming bytes in
# a buffer, preparing them to be
# grabbed by a following function
# which appears at random
# intervals during the execution of
# the script
listen &
INPID=${!}
# To test if the script is really capable
# of grabbing bytes randomly, the
# following code is looped over:
PROMPT=n
while test ${PROMPT} != 'q' ; do
printf 'y: print next byte\nn: don'"'"'t print next byte\nq: quit\n'
read -n 1 PROMPT
printf '\n'
if test ${PROMPT} = 'y' ; then
# The function 'grab1byte' extracts
# ONE byte from the buffer being fed
# bytes by 'listen', and outputs it
# to stdout
INBYTE=`grab1byte`
echo The input byte is:
printf "${INBYTE}" | xxd -cols 1 | sed 's/^.*: //'
fi
done
kill ${INPID}
The way I've tried to implement this, is by using FIFOs. In one directory, I have 2 FIFOs, which I created with mkfifo; these are named 'INPUT' and 'FIFO'. In fluxbox, I open 2 instances of xterm; in one I run the following script:
Code:
#!/bin/bash
MY_INPUT=INPUT
MY_FIFO=FIFO
# This is 'listen':
(
IFS=
tail -f ${MY_INPUT} | while read -N 1 CHAR ; do
printf "${CHAR}" > ${MY_FIFO}
done
) &
READPID=${!}
PROMPT=n
while test ${PROMPT} != 'q' ; do
printf 'y: print next byte\nn: don'"'"'t print next byte\nq: quit\n'
read -n 1 PROMPT
printf '\n'
if test ${PROMPT} = 'y' ; then
# This is 'grab1byte':
INBYTE=`cat ${MY_FIFO}`
echo The input byte is:
printf "${INBYTE}" | xxd -cols 1 | sed 's/^.*: //'
fi
done
kill ${READPID}
On the other one, I run:
Code:
printf 'Hello, world!' > INPUT
Then, back on the first terminal, I type 'y' to the prompt, to test the bytegrabbing. The problem is: instead of getting only 1 byte, I sometimes get 1, 2, 3, 4, 5 bytes. A typical session looks something like:
Code:
y: print next byte
n: don't print next byte
q: quit
y
The input byte is:
48 H
65 e
y: print next byte
n: don't print next byte
q: quit
y
The input byte is:
6c l
6c l
6f o
2c ,
20
77 w
y: print next byte
n: don't print next byte
q: quit
But, what I want is something like:
Code:
y: print next byte
n: don't print next byte
q: quit
y
The input byte is:
48 H
y: print next byte
n: don't print next byte
q: quit
y
The input byte is:
65 e
y: print next byte
n: don't print next byte
q: quit
The mystery is: Why does FIFO spit out 2 or 4 bytes at a time, if I am only writing ONE byte at each iteration of the loop??:
Code:
IFS=
tail -n 1 -f ${MY_INPUT} | while read -N 1 CHAR ; do
printf "${CHAR}" > ${MY_FIFO}
done
The code seems to work with a 'sleep' delay of 0.2 right after 'printf "${CHAR}" > ${MY_FIFO}'. But... why?
In order for this script to be perfect, I would require it to ONLY use FIFOs: No ugly and slow hard-drive file buffers, please. And also, NO ugly time delays.
Another funny thing is how, when I run in one terminal:
0000000: 48 H
..
0000000: 65 e
..
0000000: 6c l
..
0000000: 6c l
..
0000000: 6f o
..
0000000: 2c ,
..
0000000: 20
..
0000000: 77 w
..
0000000: 6f o
..
0000000: 72 r
..
0000000: 6c l
..
0000000: 64 d
..
0000000: 21 !
..
Which goes to show that ${CHAR} never stores more than 1 byte at any given time. If it did, the output would look more like:
Code:
0000000: 48 H
0000001: 65 e
0000002: 6c l
0000003: 6c l
..
0000000: 6f o
0000001: 2c ,
0000002: 20
..
0000000: 77 w
0000001: 6f o
0000002: 72 r
0000003: 6c l
0000004: 64 d
0000005: 21 !
..
So... my question is... basically: What is the deal with this FIFO glitch? If the problem is not in the loop, then: Where is it?
I posted a question on date intervals about a month back asking about how I could be able to go about a user entering the starting year/month/day and an ending year/month/day and then the script automatically cycling through each day of each month of each year that the user has specified.
I... (7 Replies)
In my server migration requirement, I need to compare if one file on old server is exactly the same as the corresponding file on the new server.
For diff and comm, the inputs need to be sorted. But I do not want to disturb the content of the file and need to find byte-to-byte match.
Please... (4 Replies)
Hi All
Can anyone please suggest me how to remove the last byte from a falt file .This is from the last line's last BYTE.
Please suggest me something.
Thank's and regards
Vinay (1 Reply)
divide input values into specified number (-100 or -200) according to the key (a1 or a2 ....)
For ex: if we give -100 in the command line it would create 100 number intervals (1-100, 100-200, 200-300) untill it covers the value 300 in a1.
Note: It should work the same even with huge numbers... (3 Replies)
Hi
I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue:
I want expect script for random passwords and random commands generation.
please can anyone help me?
Many Thanks in advance (0 Replies)
Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom.
I create a named pipe then:
dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024
but when I cat a file to the fifo that's 1024 random bytes:
cat randomfile.txt >... (7 Replies)
There was an upload recently on generating a pseudo-random file when /dev/random does NOT exist.
This does not need /dev/random, /dev/urandom or $RANDOM either...
(I assume $RANDOM relies on the /dev/random device in some way.)
This code uses hexdump just because I like hexdump for ease of... (2 Replies)
Hi all,
I hope you can help me with the following question:
I have multiple tables like this:
Chr Start End Zygosity Gene
chr1 153233510 153233510 het LOR
chr1 153233615 153233615 hom LOR
chr1 153233701 153233701 hom LOR
chr1 ... (5 Replies)
Hello
I created 3 files by:
dd if=/dev/urandom bs=1024 count=1000000 of=./testfile1
dd if=/dev/urandom bs=1024 count=5000000 of=./testfile2
dd if=/dev/urandom bs=1024 count=10000000 of=./testfile3
Now I want to know how to make a change in a specific byte and/or line of theses files? (2 Replies)
hi all,
I wish to calculate the length between intervals whose are defined by a starting and an end possition. The data looks like this:
1 10
23 30
45 60
70 100...
The desired output should be:
13 # (23-10)
15 # (45-30)
10 # (70-60)...
I donīt know how to operate with different... (2 Replies)