linux - Bash: Iteration through directories acts weird -


#!/bin/bash dir in "/home/$user/sessions/out/*/";     size=$(stat -c %s $dir/log)     if [ 1 -ne 0 ];         echo $size.     fi done 

started using bash today. trying check size of log file in directories in home/sessions/out. if 1!=0 (always, test), file size of log should printed. 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 period after each file size, 1 period. file sizes appended variable size before reaches if clause? confused.

the problem pathname expansion (expansion of '*') happens in stat invocation, not in loop declaration, because path in loop quoted, path in stat invocation not.

i.e. 1 iteration of loop happening, dir having value of /home/$user/sessions/out/*/. being expanded in stat invocation, supplying paths matching /home/$user/sessions/out/*/log stat.

the solution unquote path in loop declaration , quote in stat invocation.

like this:

#!/bin/bash dir in /home/$user/sessions/out/*/;     size=$(stat -c %s "$dir/log")     if [ 1 -ne 0 ];         echo $size.     fi done 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -