OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Summing Up

With our knowledge of the for command, we will now apply the final improvements to our sys_info_page script. Currently, the report_home_space function looks like this:



report_home_space () {

if [[ $(id -u) -eq 0 ]]; then cat <<- _EOF_

<H2>Home Space Utilization (All Users)</H2>

<PRE>$(du -sh /home/*)</PRE>

_EOF_

else

cat <<- _EOF_

<H2>Home Space Utilization ($USER)</H2>

<PRE>$(du -sh $HOME)</PRE>

_EOF_

fi return

}

report_home_space () {

if [[ $(id -u) -eq 0 ]]; then cat <<- _EOF_

<H2>Home Space Utilization (All Users)</H2>

<PRE>$(du -sh /home/*)</PRE>

_EOF_

else

cat <<- _EOF_

<H2>Home Space Utilization ($USER)</H2>

<PRE>$(du -sh $HOME)</PRE>

_EOF_

fi return

}


Next, we will rewrite it to provide more detail for each user’s home directory, and include the total number of files and subdirectories in each:



report_home_space () {

report_home_space () {


local format="%8s%10s%10s\n"

local i dir_list total_files total_dirs total_size user_name


if [[ $(id -u) -eq 0 ]]; then dir_list=/home/* user_name="All Users"

else

dir_list=$HOME user_name=$USER

fi


echo "<H2>Home Space Utilization ($user_name)</H2>" for i in $dir_list; do

total_files=$(find $i -type f | wc -l) total_dirs=$(find $i -type d | wc -l) total_size=$(du -sh $i | cut -f 1)


echo "<H3>$i</H3>" echo "<PRE>"

printf "$format" "Dirs" "Files" "Size" printf "$format" "----" "-----" "----"

printf "$format" $total_dirs $total_files $total_size echo "</PRE>"

done return

}

local format="%8s%10s%10s\n"

local i dir_list total_files total_dirs total_size user_name


if [[ $(id -u) -eq 0 ]]; then dir_list=/home/* user_name="All Users"

else

dir_list=$HOME user_name=$USER

fi


echo "<H2>Home Space Utilization ($user_name)</H2>" for i in $dir_list; do

total_files=$(find $i -type f | wc -l) total_dirs=$(find $i -type d | wc -l) total_size=$(du -sh $i | cut -f 1)


echo "<H3>$i</H3>" echo "<PRE>"

printf "$format" "Dirs" "Files" "Size" printf "$format" "----" "-----" "----"

printf "$format" $total_dirs $total_files $total_size echo "</PRE>"

done return

}


This rewrite applies much of what we have learned so far. We still test for the superuser, but instead of performing the complete set of actions as part of the if, we set some vari- ables used later in a for loop. We have added several local variables to the function and made use of printf to format some of the output.


Top OS Cloud Computing at OnWorks: