Question

In: Computer Science

how to create s recursive function, explore that prompts the user to enter a directory name...

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.

Solutions

Expert Solution

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:


Related Solutions

Create a program that when run, prompts the user to enter a city name, a date,...
Create a program that when run, prompts the user to enter a city name, a date, the minimum temperature and the maximum temperature. Calculate the difference between the maximum temperature and the minimum temperature. Calculate the average temperature based on the minimum and maximum temperature. Print out the following: City name today's Date minimum temperature maximum temperature average temperature difference between minimum and maximum The following is a sample run, user input is shown in bold underline. Enter City Name:...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
C# Create a console application that prompts the user to enter a regular expression, and then...
C# Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a regular expression...
Write a function posnum that prompts the user to enter a positive number and loops to...
Write a function posnum that prompts the user to enter a positive number and loops to error-check. It returns the square root of the positive number entered by the user. It calls a subfunction in the loop to print an error message. The subfunction has a persistent variable to count the number of times an error has occurred. Here is an example of calling the function: >> squarerootvalue = posnum Enter a positive number: -5 Error #1: Follow instructions! Does...
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
C# & ASP.NET Create a console application that prompts the user to enter a regular expression,...
C# & ASP.NET Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a...
jgrasp environment, java write a complete program that prompts the user to enter their first name,...
jgrasp environment, java write a complete program that prompts the user to enter their first name, middle name, and last name (separately). print out thier name and initials, exactly as shown: your name is: John Paul Chavez your initials are: J. P. C. use string method chartAt() to extract the first (zero-th) character from a name(the name is a string type): username.charAt(0). thank you.
ARDUINO Create a function that will ask the user to enter the values for each of...
ARDUINO Create a function that will ask the user to enter the values for each of the three colors (red, green and blue) between 0 and 255. The function should check that the user does not enter a number bigger than 255 or smaller than 0. If this happens, the function should keep asking for a valid number.
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT