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...
First, the Python program prompts user to enter user information (name, email, and phone number). Then...
First, the Python program prompts user to enter user information (name, email, and phone number). Then it displays a menu called “Fish Information” that has the following fish type: 1. Cat Fish 2. Red Fish 3. Any other fish Let user choose the fish type that he/she got and input the length of the fish. Then the program will determine what should be done with this particular fish. Based on the following criteria: Criteria: Length: FISHTYPE - Cat Fish <10:...
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...
Use c# Create a program called ResortPrices that prompts the user to enter the number of...
Use c# Create a program called ResortPrices that prompts the user to enter the number of days for a resort stay. Then display the price per night and the total price. Nightly rates are R 500.00 for one or two nights, R 650.00 for three or four nights, R 850.00 for five, six or seven nights, and R 1500.00 for eight nights or more.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT