Final answer:
A bash script has been provided that takes two parameters: a directory name and a string. It verifies the directory, searches through the files, and either exits with an error or lists the matching file names with the specified string parts replaced with an error message.
Step-by-step explanation:
To create a bash script that meets the specified requirements, you can follow this template:
#!/bin/bash
# Check if exactly two arguments were given
if [ "$#" -ne 2 ]; then
echo "Usage: $0 directory string"
exit 1
fi
# Check if the first argument is a directory
if [ ! -d "$1" ]; then
echo "Error: The first parameter is not a directory."
exit 2
fi
directory=$1
search_string=$2
# Loop through all the files in the directory
for file in "$directory"/*; do
filename=$(basename "$file")
if [ "$filename" == "$search_string" ]; then
echo "INVALID FILE EXISTS"
exit 3
fi
if [[ "$filename" == *"$search_string"* ]]; then
echo "File '${filename/'$search_string'/'ERROR'}' contains string but is not an exact match"
fi
done
This script checks the number of parameters and whether the first parameter is a directory. It then loops through all the files within that directory, checking for an exact match to the search string or if the filename contains the string. If an exact match is found, it displays an error message and exits. Otherwise, it lists the specified files with the search string partially replaced with an error message.