46.5k views
0 votes
Write a script 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 Nstoitsev
by
8.3k points

1 Answer

1 vote

Final answer:

To write a script that meets the given requirements, you can use a programming language like Python.

Step-by-step explanation:

To write a script that meets the given requirements, you can use a programming language like Python. Here is an example script:

import os
import sys

# Check if the first parameter is a directory
if not os.path.isdir(sys.argv[1]):
print('Error: First parameter is not a directory')
sys.exit()

# Check if there are more than two parameters
if len(sys.argv) > 2:
print('Error: Too many parameters')
sys.exit()

# Loop through all files in the directory
for filename in os.listdir(sys.argv[1]):
if filename == sys.argv[2]:
print('INVALID FILE EXISTS: ' + filename)
elif sys.argv[2] in filename:
print('Error: ' + filename.replace(sys.argv[2], '[REPLACED STRING]'))
else:
print(filename)
User Brandon Manchester
by
6.7k points