In: Computer Science
how to create s recursive function, explore that prompts the user to enter a directory name and an executable name, and checks that they are both readable and executable (terminating with an error message if not). It then passes the two names to the explore function.
this is a part of bash scripting.
Please find the code and added all the comments for your better understanding and screenshots of execution below.
Please find the same
# function read_execute_check is an recursive funtion that checks whether dir is readable and executable also for executable files
function read_execute_check
{
#Here we are checking whether directory and executable files are exists or not
#if they are not present then we will print the same and exit
if [ -f "$executable" ] && [ -d "$directory" ]; then
echo "$directory and $executable exists and"
#if any of them are true, we set corresponding value to 1 or else 0 if not readable/ not executable
#here we are checking for execute are not
[ -x $directory ] && X1=1 || X1=0
#echo "$X1"
# here we are checking for readable or not
[ -r $directory ] && R1=1 || R1=0
#echo "$R1"
#here we are checking for execute are not
[ -x $executable ] && X2=1 || X2=0
#echo "$X2"
# here we are checking for readable or not
[ -r $executable ] && R2=1 || R2=0
#echo "$R2"
if [ "$X1" -eq 1 ] && [ "$R1" -eq 1 ] && [ "$X2" -eq 0 ] && [ "$R2" -eq 1 ]; then
echo " The $directory and $executable file are both readable and executable"
echo -n "Enter your directory name: "
read directory
echo -n "Enter your executable name: "
read executable
#calling recursively until if anything fails
read_execute_check $directory $executable
else
echo " ERROR: The directory or executable file is NOT readable or executable"
fi
#if path is invalid then we will print does not exist
else
echo "$directory or $executable does not exist in the given path."
fi
}
#Reading iniitial inputs
echo -n "Enter your directory name: "
read directory
echo -n "Enter your executable name: "
read executable
#calling recursive function
read_execute_check $directory $executable
Execution screenshots:
sample 2: