You can combine the commands
did -e $dname 6 | did -e $dname 7
as
did -e $dname 6,7
You also have an extra { in the $did = 2 line, which offsets, and therefore prevents the $did = 3 section from working, as $did would have to match both 2 and 3 for that to work, which is both adding and deleting a channel