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 averageExplanation / 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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.