Question

In: Computer Science

write a bash shell script using the for-loop construct that counts the number of files and...

write a bash shell script using the for-loop construct that counts the number of files and directories looping through the files in the directory name provided by the user on a prompt by the script and prints "TOO many files" is the count of files is larger than a random number it generates between 20 and 40

Solutions

Expert Solution

Explanation:

Steps to be followed

1)Create a script by name forloop.sh which consists of a for-loop construct that counts the number of files and directories.

2)Take the directory name as input from the user in the following manner:

sh forloop.sh <directoryname>

where directoryname is the input that will be entered by the user.

3)Declare two variables filecount and dircount to hold the value of the no of files and directories.

4)Declare a variable LOCATION which will store the input entered by the user.

5)Loop through the directory name provided in Step 2.The below command will recursively iterate through the files in the given directory and will increment the count of files and directories present respectively.

for item in $LOCATION/* $LOCATION/.*   

do   

if [ -f "$item" ]

then   

FILECOUNT=$[$FILECOUNT+1]   

elif [ -d "$item" ]
  
  
then   

DIRCOUNT=$[$DIRCOUNT+1]   

fi

6)Make use of the command shuf -i 20-40 -n 1 to generate random numbers between 20 and 40 and store it in a variable randomno.

7)Check whether the no of files present in the directory provided in the Step 2 is greater than the random no generated.If the above condition is true print TOO Many Files

Commands to check for the condition in the Step 7 is:

if [ $FILECOUNT -gt $randomno ]
then
echo "TOO Many Files"
exit   
fi

8)Display a proper message to the user providing information about the usage of the shell script by using the following commands

  
if [ "$#" -lt "1" ]

then   

echo "Usage: ./forloop.sh <directory>"   

exit 0   

fi

9)Also we can display the no of file count,dir count and random number count using the following commands

echo "Random Number : $randomno"   

echo "File Count" : $FILECOUNT   

echo "DIR COUNT" : $DIRCOUNT

NOTE : Above commands are only for informative purpose.They are optional.

10)I have created a test directory with around 32 files in order to test the shells script

--------------------------------------

Content of forloop.sh

LOCATION=$1

FILECOUNT=0

DIRCOUNT=0   



if [ "$#" -lt "1" ]

then   

echo "Usage: ./forloop.sh <directory>"   

exit 0   

fi   

for item in $LOCATION/* $LOCATION/.*   

do   

if [ -f "$item" ]

then   

FILECOUNT=$[$FILECOUNT+1]   

elif [ -d "$item" ]
  
  
then   

DIRCOUNT=$[$DIRCOUNT+1]   

fi   

done   

randomno=`shuf -i 20-40 -n 1`

echo "Random Number : $randomno"   

echo "File Count" : $FILECOUNT   

echo "DIR COUNT" : $DIRCOUNT   

if [ $FILECOUNT -gt $randomno ]
then
echo "TOO Many Files"
exit   
fi

------------------------

Output: No of Files in the directory is not greater than the random number

Output:No of Files in the directory is not greater than the random number


Related Solutions

Simple shell scripting Write a Bash shell script called strcount.sh that counts the number of occurances...
Simple shell scripting Write a Bash shell script called strcount.sh that counts the number of occurances of a given string within a file. The scripts takes two arguments: the name of the file to check and the string to count occurances of. If the file is not found (or not readable), the script should print the error message Cannot access file and exit with code -1. Otherwise, the script should print the number of occurances of the given string and...
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask...
Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask the user to input the number of kilometers they need to travel. - Display the number of equivalent miles for the number. Use formula, Kilometers = miles/0.62137 - Every time the loop runs, it should ask the user if they want to continue, if user enters “Y” or “y”, then the loop runs again, if user enters “N” or “n”, then stop the loop...
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in...
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in the current directory whose names end in 't' or 'z'. For such files only, your program should list certain LINES of the file(s). These lines are those that have at least 4 x's somewhere in the line. Note that the x's may be upper- or lower-case, and may be separated by other characters; so the following 3 lines DO match: XXxX and more things...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all the files in a directory. b. Write a command sequence or a script to insert a line “GHIJKLM” at every 10th line of a file? c. Write a bash command or script to find all the files modified in less than 5 days and print the record count of each.
Write a BASH shell script (see the Important Notes section) that acts as a DOS command...
Write a BASH shell script (see the Important Notes section) that acts as a DOS command interpreter (the user enters the DOS command and the script executes the corresponding Linux command). The script should loop continuously until the user enters the QUIT command Prior to accepting a command, display a prompt containing your first name and the greater than symbol (>) Example prompt: linux> The DOS command, and any arguments, should be stored in variables $command – the DOS command...
Write a bash script to find all the files ending with .c recursively for every directory...
Write a bash script to find all the files ending with .c recursively for every directory in your current working directory, then copy each one to a folder called programs, need handle duplicates by appending the number to the end of the file (ex main.c main-1.c ) compile them and generate a report report should look like: main.c compiles prog2.c failed main-2.c compiles etc.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Linux Sign into your lab account Write a bash shell script that does the following: Print...
Linux Sign into your lab account Write a bash shell script that does the following: Print out a friendly welcome message Ask the user for the name of their favorite animal Grep that animal from a text file noises.txt Tell the user what that animal says (i.e. what noise does it make) If the animal does not exist ask the user what noise the animal makes Store that new information in noises.txt
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT