Why not make layer name a parameter to the script, thereby eliminating the need for this `grep` step?  Maybe it goes against the unix philosophy somewhat I suppose (of building commands up from several smaller commands).