logoISU  

CS469/569 - Linux and Unix Administration and Networking

Spring 2022

Displaying ./code/02-10/h5/d.sh

#!/bin/bash

# (2 points)
# Use du to give a byte total of a particular path, then print out the total
# in the SI unit human readable formats.
# Example output:
# > ./d.sh /net/vm
# Total:  39873039000 bytes / 38938514 KB / 38025 MB / 37 GB

# Get the path from the first command line argument, if it is not provided, use
# the current working directory (.):


# need to not just use . but use $1 if it exists
      
# Use du and cut to get the size in bytes for the path given, assign the result
# to a variable:

num=$(du .) # but make sure it's giving bytes.
num=$(echo $num | cut -f 1 -d " ")
let total=$(($num+10))  
echo $total
# you can do /1000 or *1000


echo -n "Total: ";

siunits=( "bytes" "KB" "MB" "GB" "TB" "PB" )

# Setup a index variable for the siunits array defined above:
let i=1

# While the total is > 0 loop:
while (( total > 0 )); do
    echo "$i, $total";
    echo $siunits{[$i]};  # fix this
    let i++;
    let total=$(( total / 2))
  # if the si index is > 0 output a "/" separator:

  # Print the current total with the siunit indexed by the index variable

  # divide the total by 1024.

  # increment the index variable.

done

# print a new-line after exiting the loop