48.2k views
1 vote
Write a script in Bash that will do the following: take a name of a directory as a parameter and a string (verify that the first parameter is a directory, output an error message and terminate. Second parameter can be anything, terminate if more than two parameters are given. loop through all files in the directory and display their names. if one of the names matches the second parameter exactly, terminate the script and output INVALID FILE EXISTS for all of the files in that directory, also display only those files that contain (but do not make an exact match) the second input string. do so with an error message that replaces the named file with the name in our second argument

User Keram
by
8.1k points

2 Answers

5 votes
#!/bin/bash

# Check the number of parameters
if [ "$#" -ne 2 ]; then
echo "Error: Invalid number of parameters. Please provide a directory name and a string."
exit 1
fi

# Check if the first parameter is a directory
directory="$1"
if [ ! -d "$directory" ]; then
echo "Error: The first parameter must be a directory."
exit 1
fi

# Loop through all files in the directory
for file in "$directory"/*; do
# Extract the filename from the path
filename=$(basename "$file")

# Check if the filename is an exact match with the second parameter
if [ "$filename" == "$2" ]; then
echo "INVALID FILE EXISTS"
exit 1
fi

# Check if the filename contains the second parameter
if [[ "$filename" == *"$2"* ]]; then
# Display an error message with the replaced filename
echo "Error: $filename contains '$2'. Replace with 'ERROR_MESSAGE'."
else
# Display the filename
echo "$filename"
fi
done

# Script execution completed
echo "Script executed successfully."
User Huang Chen
by
8.2k points
1 vote

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.

User Elijah M
by
7.5k points