in reply to While is not working when using variable

When you use single quotes to enclose a string, as you do in the now commented-out first line, symbols such as $mydir are not “interpolated.”   They are not seen as variable-names, but simply literal characters.   To get the behavior that you want, enclose the string in double quotes.

(Yes, some other languages treat single vs. double quotes, with respect to interpolation, in precisely the opposite way.)