The problem is that pathname expansion (expansion of '*') happens in the stat
invocation, not in the loop declaration, because the path in the loop is quoted, but the path in stat
invocation is not.
I.e. only one iteration of the loop is happening, with dir
having value of /home/$USER/sessions/out/*/
. That is then being expanded in stat invocation, supplying all the paths matching /home/$USER/sessions/out/*/log
to stat
.
The solution is to unquote the path in the loop declaration and quote it in the stat
invocation.
Like this:
#!/bin/bash
for dir in /home/$USER/sessions/out/*/; do
size=$(stat -c %s "$dir/log")
if [ 1 -ne 0 ]; then
echo $size.
fi
done