in reply to Error in script

Try removing the single quotes from '$dir'.