USPreport
USPreport
Submitted in partial fulfillment of the requirement for the curriculum of the 5th Semester
Bachelor of Engineering
in
Computer Science and Engineering
Submitted by
JAYASHREE S : 1VI23CS404
Manupriya : 1VI22CS060
Lalitha s : 1VI22CS055
Objective:
The primary objectives of the shell script are as follows:
1. Input Handling:
The script accepts six arguments: a target word and five filenames. It validates that exactly
six arguments are provided and alerts the user if the input is incomplete or incorrect.
2. File Validation:
Each file is checked to confirm its existence and whether it is a regular file (not a directory
or special file). If a file does not meet these conditions, the script notifies the user.
3. Word Counting:
The script leverages tools like grep and wc to count occurrences of the exact word in each
file, ensuring precise and efficient text processing.
4. Output Formatting:
Results are displayed in a user-friendly format, indicating the number of times the word
appears in each file, or an error message if a file is invalid.
Functions are used to encapsulate tasks like word counting and file validation, making the
script easier to extend or adapt for similar use cases.
6. Error Handling:
The script handles missing files, incorrect input, and other unexpected conditions
gracefully, providing informative error messages without crashing.
7. Documentation:
Comments throughout the script explain its functionality, and usage instructions are
included to guide users on how to run it effectively.
8. Cross-Platform Compatibility:
The script uses standard shell commands compatible with most Unix-like environments,
ensuring portability across different systems.
SHELL PROGRAM
bash
#!/bin/bash
# Loop through each file and count the occurrences of the word
for file in "${files[@]}"; do
if [ -f "$file" ]; then
count=$(grep -o -w "$word" "$file" | wc -l)
echo "The word '$word' occurs $count times in file '$file'."
else
echo "File '$file' does not exist or is not a regular file."
fi
done
Step-by-Step Breakdown
1. Shebang Line:
#!/bin/bash
• This specifies the interpreter for the script. It tells the system to use the Bash shell to
execute the script.
word=$1
files=("${@:2}")
• A for loop is used to iterate over each filename stored in the files array.
5. File Validation:
if [ -f "$file" ]; then
• [ -f "$file" ]: Checks if the current file exists and is a regular file (not a directory or special
file).
else
echo "File '$file' does not exist or is not a regular file."
fi
• If the file does not exist or is not a regular file, a message is displayed instead of attempting
to process it.
9. End of Loop:
done
• The loop moves to the next file until all files in the array are processed.
Example Usage
bash
./word_count.sh example file1.txt file2.txt file3.txt file4.txt file5.txt
Sample Output
• Tools Used:
• Error Handling:
• Reusability:
Can easily be modified to handle more files or additional features (like searching recursively).
CONCLUSION
In this analysis, the shell script was used to search for a specified word across five different
files. The script successfully counted the occurrences of the word in each file and provided a
detailed report for every file, indicating how many times the word appeared. Based on the
results, the conclusion can be drawn: if the word was found in any of the files, it demonstrates
that the word is present and has a certain frequency within the file contents. Conversely, if the
word was absent from all files, it indicates that the word does not appear in the specified
documents. This approach allows for efficient text search and can be useful in analyzing large
volumes of text data for specific keywords.