Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Bash script, filter.sh, which prints those files/directories that have t

ID: 3542021 • Letter: W

Question

Write a Bash script, filter.sh, which prints those files/directories that have the size greater than the average file size of the directory. Assuming the directory /my/dir has five files/directories with size in parentheses " a(100) b(10) c(100) d(100) e(20)", your program "filter.sh /my/dir" will list "a c d" since the size of each of the three files/directories "a c d" is greater than the average file size of 66. Use three functions: main, average, and filter, where main calls average and filter, and average computes the average file size of a directory and filter filters out those that have less than the average

Write a Bash script, filter.sh, which prints those files/directories that have the size greater than the average file size of the directory. Assuming the directory /my/dir has five files/directories with size in parentheses " a(100) b(10) c(100) d(100) e(20)", your program "filter.sh /my/dir" will list "a c d" since the size of each of the three files/directories "a c d" is greater than the average file size of 66. Use three functions: main, average, and filter, where main calls average and filter, and average computes the average file size of a directory and filter filters out those that have less than the average

Explanation / Answer

#!/bin/bash

function average()

{

n=0;

i=0;

for entry in $(ls);do

k=$(du -s "$entry" | awk '{print $1}');

let n=n+1;

i=$(($i+$k));

done;

avg=$(( $i/$n))

}

function filter()

{

out="";

for entry in $(ls);do

k=$(du -s "$entry" | awk '{print $1}');

if [ $k -gt $avg ];then

out="$out $entry";

fi;

done;

}

function main()

{

average;

filter;

echo $out

}

main

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote