Write a shell script program called “countf.sh” that will count how many files o
ID: 3663550 • Letter: W
Question
Write a shell script program called “countf.sh” that will count how many files or directories recursively. It will either take arguments for specific directories and no argument. If no argument, it will read the current working directory as the starting point. Please also provide –h option to print out how to use your countf.sh program. % ./countf.sh % ./countf.sh dir1 dir2 .... %./countf.sh –h hints: I would write a script function to process a count for each directory and return a number of files to be added up. You should have your linux or linux-like environment, either using virtual machine or have your own linux box Write a shell script program called “countf.sh” that will count how many files or directories recursively. It will either take arguments for specific directories and no argument. If no argument, it will read the current working directory as the starting point. Please also provide –h option to print out how to use your countf.sh program. % ./countf.sh % ./countf.sh dir1 dir2 .... %./countf.sh –h hints: I would write a script function to process a count for each directory and return a number of files to be added up. You should have your linux or linux-like environment, either using virtual machine or have your own linux box Write a shell script program called “countf.sh” that will count how many files or directories recursively. It will either take arguments for specific directories and no argument. If no argument, it will read the current working directory as the starting point. Please also provide –h option to print out how to use your countf.sh program. % ./countf.sh % ./countf.sh dir1 dir2 .... %./countf.sh –h hints: I would write a script function to process a count for each directory and return a number of files to be added up. You should have your linux or linux-like environment, either using virtual machine or have your own linux boxExplanation / Answer
#!/bin/bash #countf.sh #this file counts the number of files and directories in a path recursively #Variables declare -i filecount="0" declare -i dircount="0" for file in /* do if [ -f $file ] then $((filecount++)) elif [ -d $file ] then $((dircount++)) fi done echo The number of files is "$filecount" echo The number of directories is "$dircount" echo $?
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.