I've been trying to learn scripting from the tutorial online. I believe I
found an error in the script. In part 3, flow control we edit a function
home_space and have the following line
printf "$format" $total_dirs $total_files $total_blocks
$format is defined to take 4 arguments, and since total_blocks=$(du -s
$home_dir) which gives the number of blocks followed by the name of the
directory, it seems that we assume that $total_blocks will resolve into two
arguments which together with $total_dirs and $total_files gives a sum total
of 4 arguments.
However, I found that $total_blocks was being treated as one argument even
though it has two tokens. Without looking at the shell code, I believe that
the assignment of arguments in printf is somehow happening BEFORE the shell
is interpreting the variables. My work around was to do this:
total_blocks=$(du -s $home_dir | sed -e "s/[ \t].*//")
printf "$format" $total_dirs $total_files $total_blocks $home_dir
Can someone confirm and clarify this behavior? The tutorial seems to suggest
that the variables are interpreted by the shell before anything else, which
is counter to the behavior that I'm seeing.
Get latest updates about Open Source Projects, Conferences and News.