You can use the same methods as you would use to apply distortion to a node-based material - ie, distort the input coordinates.
For example, change the script to include a DistortFactor and DistortScale as follows :
shader basic_function(
point Pos = P,
float DistortFactor = 0.0,
float DistortScale = 1.0,
output float f = 0)
{
vector distortion = noise("perlin", P*DistortScale)*DistortFactor;
float x = Pos[0]+distortion[0];
float y = Pos[1]+distortion[1];
float z = Pos[2]+distortion[2];
f = (z + 0.5 - floor(z));
}
This will give you two additional inputs on the script node to allow you to adjust the amount of distortion and its scale. The 'noise' function is used to generate distortion based on the point vector (P), scaled by the input DistortScale and multiplied by the input DistortFactor - and added in to the coordinates used to generate the "stripes".
This can produce the following result :
EDIT : As @LarryGritz pointed out, the above solution is generally only of use for static objects - any object using this shader which is moving through the scene (whether location, rotation, or scale) will move through the distortion and the pattern will change as it moves. To address this we need to transform the World coordinates of 'P' into Object coordinates so the OSL code becomes the following :
shader basic_function2(
point Pos = P,
float DistortFactor = 0.0,
float DistortScale = 1.0,
output float f = 0)
{
point Pobj = transform("world","object",Pos);
vector distortion = noise("perlin", Pobj*DistortScale)*DistortFactor;
float x = Pobj[0]+distortion[0];
float y = Pobj[1]+distortion[1];
float z = Pobj[2]+distortion[2];
f = (z + 0.5 - floor(z));
}
Note the use of the 'transform' function to convert from 'world' to 'object' coordinates (similar to the Vector Transform node in node-based materials).