When I cut worktops to fit existing walls (same would apply with shelves etc) I make a template with .25mm plastic which is stiff enough to keeps its shape, but easy to cut. It costs something like 50p for a 52" x 26" sheet.The speed increase more than covers the cost, and I get a near perfect fit first time. I cut it into smaller pieces if needed, and stick the pieces together with masking tape. I learned about templating from watching granite installers.
This method copes with corners, too