95.6k views
3 votes
Write a shell script called checkpath that extracts each directory from the user's PATH and ensures that the directory exists and is readable. Report either error condition or confirm that the directory is correctly configured. Hint: directories in the PATH are separated by a colon (:).

1 Answer

6 votes

Final answer:

To create a shell script called checkpath, use a scripting language like Bash to split the PATH variable and iterate over each directory to check if it exists and is readable. Print an error message if the directory is not valid.

Step-by-step explanation:

To create a shell script called checkpath, you can use a scripting language like Bash. The script should first split the PATH variable using the colon separator. Then, it can iterate over each directory and check if it exists and is readable using the 'test' command. If the directory does not exist or is not readable, an error message can be printed. Here's an example:

#!/bin/bash

IFS=":"

for dir in $PATH;
do
if ! [ -d "$dir" ] || ! [ -r "$dir" ];
then
echo "Error: $dir does not exist or is not readable"
else
echo "$dir is correctly configured"
fi
done
User Peter Eisentraut
by
7.9k points