Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Ex. 2 Using shell environment variables( 7 Points) In this exercise, you will learn how to use a set of environment variables that are setup

image text in transcribedimage text in transcribedimage text in transcribed

Ex. 2 Using shell environment variables( 7 Points) In this exercise, you will learn how to use a set of environment variables that are setup in a "configuration script", in your main program script, so that your main script can be run without modification by users of your program. You have been provided with a configuration script a3config that is a simple text file which contains the environment variables that we will be using. The contents of this file is shown below for discussion. You can use vi to edit this file. $ cat a3config # You may edit this file for your testing # You should only change the values of the variables for the purpose of your testing # However, TAs will use their own a3config file to test your script with. # Therefore, do not add any other code here that is required for your script's execution. DIRNAME=/tmp/-_206__$LOGNAME/mydir EXTENSION=msg SHOW=true Your task is to write a script finder.sh that will check if there are files in the directory whose value is stored in the variable DIRNAME such that the files have the extension as indicated in the variable EXTENSION. If there are such files, you should print a list of them. Further, if the variable SHOW has the value true, the contents of the file must be displayed to screen. In order to accomplish this, you will have to execute the contents of a3config within finder.sh. Explore the source or dot (.) commands to accomplish this. If your finder.sh script cannot find a3config, then it should throw an error message and terminate the script with a non zero exit code. $ ./finder.sh Error cannot find a3config $ echo $? The echo $? command is included above to merely show you the exit code from the script was non zero. If either of the variables DIRNAME or EXTENSION is not present in a3config or if their values are empty, the script should display an error message and terminate with a non zero exit code. $ ./finder.sh Error DIRNAME and EXTENSION must be set $ echo $? If the directory mentioned in DIRNAME does not exist, then the script should throw an error message and terminate with a non zero exit code. $ ./finder.sh Error directory osuchdir/diri does not exist $ echo $? If the script cannot locate any files in the directory that have the expected extension, then it should display the following message, but still terminate with a zero exit code. $ ./finder.sh Unable to locate any files with extension msg in /etc $ echo $? If the script is able to locate files in the directory, and the value of SHOW is true then it should list the names of the files, along with the contents of each file and terminate with a zero exit code. $ ./finder.sh /home/2013/jdsilv2/comp206/Mini3/first.msg 7 HOMO ZUIO JUDIIVzr compzvu/ 10/1110V.Wag Greetings to all of you! /home/2013/jdsilv2/comp206/Mini3/ second.msg Howdy ? $ echo $? If the script is able to locate files in the directory, but the value of SHOW is not set to true or if the SHOW variable itself is missing from a3config, then it should list the names of the files, but do not display the contents of files and terminate with a zero exit code. $ ./finder.sh /home/2013/jdsilv2/comp206/Mini3/first.msg /home/2013/jdsilv2/comp206/Mini3/ second.msg $ echo $? 1. Your finder.sh must be executable using the bash shell in mimi. 2.(1 Point) For properly commenting throughout the source code. 3.(1 Point) Detecting and displaying an error message and terminating with non zero exit code when the script cannot find a3config. 4.(1 Point) Detecting and displaying an error message and terminating with non zero exit code when any of the variables DIRNAME and EXTENSION are not present a3config or is empty. 5.(1 Point) Displaying an error message and terminating with non zero exit code when the directory does not exist. 6.(1 Point) Displaying a message and terminating with zero exit code when the directory does exist but cannont locate any files with the expected extension. Ex. 2 Using shell environment variables( 7 Points) In this exercise, you will learn how to use a set of environment variables that are setup in a "configuration script", in your main program script, so that your main script can be run without modification by users of your program. You have been provided with a configuration script a3config that is a simple text file which contains the environment variables that we will be using. The contents of this file is shown below for discussion. You can use vi to edit this file. $ cat a3config # You may edit this file for your testing # You should only change the values of the variables for the purpose of your testing # However, TAs will use their own a3config file to test your script with. # Therefore, do not add any other code here that is required for your script's execution. DIRNAME=/tmp/-_206__$LOGNAME/mydir EXTENSION=msg SHOW=true Your task is to write a script finder.sh that will check if there are files in the directory whose value is stored in the variable DIRNAME such that the files have the extension as indicated in the variable EXTENSION. If there are such files, you should print a list of them. Further, if the variable SHOW has the value true, the contents of the file must be displayed to screen. In order to accomplish this, you will have to execute the contents of a3config within finder.sh. Explore the source or dot (.) commands to accomplish this. If your finder.sh script cannot find a3config, then it should throw an error message and terminate the script with a non zero exit code. $ ./finder.sh Error cannot find a3config $ echo $? The echo $? command is included above to merely show you the exit code from the script was non zero. If either of the variables DIRNAME or EXTENSION is not present in a3config or if their values are empty, the script should display an error message and terminate with a non zero exit code. $ ./finder.sh Error DIRNAME and EXTENSION must be set $ echo $? If the directory mentioned in DIRNAME does not exist, then the script should throw an error message and terminate with a non zero exit code. $ ./finder.sh Error directory osuchdir/diri does not exist $ echo $? If the script cannot locate any files in the directory that have the expected extension, then it should display the following message, but still terminate with a zero exit code. $ ./finder.sh Unable to locate any files with extension msg in /etc $ echo $? If the script is able to locate files in the directory, and the value of SHOW is true then it should list the names of the files, along with the contents of each file and terminate with a zero exit code. $ ./finder.sh /home/2013/jdsilv2/comp206/Mini3/first.msg 7 HOMO ZUIO JUDIIVzr compzvu/ 10/1110V.Wag Greetings to all of you! /home/2013/jdsilv2/comp206/Mini3/ second.msg Howdy ? $ echo $? If the script is able to locate files in the directory, but the value of SHOW is not set to true or if the SHOW variable itself is missing from a3config, then it should list the names of the files, but do not display the contents of files and terminate with a zero exit code. $ ./finder.sh /home/2013/jdsilv2/comp206/Mini3/first.msg /home/2013/jdsilv2/comp206/Mini3/ second.msg $ echo $? 1. Your finder.sh must be executable using the bash shell in mimi. 2.(1 Point) For properly commenting throughout the source code. 3.(1 Point) Detecting and displaying an error message and terminating with non zero exit code when the script cannot find a3config. 4.(1 Point) Detecting and displaying an error message and terminating with non zero exit code when any of the variables DIRNAME and EXTENSION are not present a3config or is empty. 5.(1 Point) Displaying an error message and terminating with non zero exit code when the directory does not exist. 6.(1 Point) Displaying a message and terminating with zero exit code when the directory does exist but cannont locate any files with the expected extension

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions