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.
Write a program that prompts the user to enter a file name, then opens the file...
Write a program that prompts the user to enter a file name, then opens the file in text mode and reads names. The file contains one name on each line. The program then compares each name with the name that is at the end of the file in a symmetrical position. For example if the file contains 10 names, the name #1 is compared with name #10, name #2 is compared with name #9, and so on. If you find...
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 script that prompts the user for a pathname of a file or directory and...
Write a script that prompts the user for a pathname of a file or directory and then responds with a description of the item as a file along with what the user's permissions are with respect to it (read, write, execute).
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT