Sample data and how to produce them

Sometimes a programmer needs to test/debug one application by feeding it with data, random or not. In a situation like this, there are some utilities one can use to produce as many data as she needs.

Let’s start with a simple example of feeding our test application (we will call it “test_data”) with zeroes, say 100 bytes with a value of zero.

head –bytes=100 /dev/zero | ./test_data

The above command has two parts; the first one (before the ‘|’ delimiter), ‘head‘, reads /dev/zero device, which provides us with zero-valued bytes as a stream, up to a count of 100 bytes and sends them to the second one, ‘test_data‘, as input. If we wanted more, say 100 KBytes, we would replace the parameter of ‘bytes’ argument with “100k”, as follows:

head –bytes=100k /dev/zero | ./test_data

Now, let’s say we want random data. It’s as simple as replacing ‘/dev/zero’ device with ‘/dev/random‘:

head –bytes=100k /dev/random | ./test_data

With the above command combination, we produce 100 KBytes of random data as input to our test_data application.

And what if we wanted some specific data, like “testing with sample data”, repeated 40 times?

yes “testing with sample data” | head -40 | ./test_data

The ‘yes‘ command just outputs its input continuously; ‘head’ keeps only the first 40 lines; ‘test_data’ works with these 40 lines of the sample string.

Programming life can be very simple (and entertaining) some times!

Visit The Light of the LAMP blog for more…