fixed script template so that bash can exit when using stdin; bug fixes