kdave / btrfsmaintenance

Scripts for btrfs maintenance tasks like periodic scrub, balance, trim or defrag on selected mountpoints or directories.
GNU General Public License v2.0
900 stars 79 forks source link

Protect strings on subshell/stat calls #48

Closed comio closed 6 years ago

comio commented 6 years ago

All scripts create subshells and compare the resulting output string with construct like this:

if [ $(stat ... "$P") != "btrfs" ]; then
...
fi

this construct doesn't protect from empty strings obtaining a syntax error.

To solve, we must use "eval" in order to solve escaping and protect the result:

if [ "$(eval stat ... \"$P\")" != "btrfs" ]; then
...
fi
kdave commented 6 years ago

I'd rather avoid using eval, in this case a temporary variable can do the job, like

fst=$(stat ...)
if [ "$fst" = "btrfs" ]; ...
kdave commented 6 years ago

Applied, thanks. I've squashed the 2 commits and updated the changelog.