Fixes the profiles-plugin

An error was introduced with 7f75bb9, which uses special vars. This is the
fix.
Merge pull request #3299 from apoxa/fix-profiles

Fix for the profile plugin.
Refactor similar functions into common sections
Fix take function

Before the modification, the `take` function will try to cd
into the created directory even if mkdir throws an error.

With this modification, if mkdir fails to create the directory,
cd won't run and the take function will return the mkdir exit
code.