Check the brackets "("+")", braces "{"+"}" and pipes "|" on these lines:

if ($did = 13) { if (%identd = Off) { did -u perstg 13 | .set %ident Off | elseif { %identd = On | did -c perstg 13 | .set %ident On } } }

And

if (%identd = On) { did -c perstg 13 | .identd on %id | elseif { %identd = Off | did -u perstg 13 | .identd off } }