Question

#!/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

Started using bash today. I am trying to check the size of log file in all directories in home/sessions/out. If 1!=0 (always, as test), the file size of log should be printed. What I am getting is:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 57344001 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 966904.

I expect a period after each file size, I actually get one period. Are all file sizes appended to variable size before it reaches the if clause? I am confused.

Was it helpful?

Solution

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

OTHER TIPS

Your whole program can be simplified down to just one line

stat -c %s /home/$USER/sessions/out/*/log

Not quite sure why you have a test condition of 1 != 0, which always evaluates to true in there. But, the following loop will do it for you:

for LOGFILE in `find $LOGPATH -type f -iname 'log'`
do
   echo `stat -c %s $LOGFILE`'.'
done

I used the variable LOGPATH where you would use '/home/$USER/sessions/out.' If this is in a script file, it would be wise to use a variable like that, at the top of the script, so that it could easily be changed later if need be.

The '-type f' is there in case there are directories with the name log, that you might not want to parse. If that is not the case, then you can omit that.

I used the find command, because you have much more finely grained control over it. You can set search depth, file type, file size, and so on. To get the period at the end, simply single quote a period right at the end of the echo statement.

If you needed the filename, and then the size, just change the echo statement, like so:

echo "$LOGFILE" `stat -c %s $LOGFILE`'.'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top