Your main problem is, as pointed out by @wigy, that you're not quoting your variable. This means that a file name with whitespace will be treated as separate files. The general rule of thumb is that you should always quote your variables unless you have a very good reason not to.
That said, here's a simplified version of your script:
#!/usr/bin/env bash shopt -s nullglob for f in /tank/test/*txt do [[ -n $(getfattr -n user.comment "$f" 2>/dev/null) ]] || setfattr -n user.comment -v "$(md5sum "$f")" "$f" done
Using shell globs, as you did, is generally simpler and cleaner but here's how to do it with find
:
find /tank/test/ -type f -name '*.txt' -print0 | while IFS= read -r -d '' f do [[ -n $(getfattr -n user.comment "$f" 2>/dev/null) ]] || setfattr -n user.comment -v "$(md5sum "$f")" "$f" done