Question
Part2. Shell Script (Sobell Chapter 8 and 10). Part2 #1. Consider the following addentry script listed below. $ cat addentry # addentry: add a journal
Part2. Shell Script (Sobell Chapter 8 and 10).
Part2
#1. Consider the following addentry script listed below.
$ cat addentry # addentry: add a journal entry to the file # $HOME/entry-file file=$HOME/entry-file date >> $file echo -n "Enter name of person or group: " read name echo "$name" >> $file echo >> $file cat >> $file echo "----------------------------------------------------" >> $file echo >> $file
Rewrite this script (and name it addentry1) by adding commands to verify that the user has write permission for a file named entry-file in the users home directory, if such a file exists. The script should take appropriate actions if entry-file exists and the user does not have write permission to the file. Verify that the modified script works.
Listing of the updated addentry1 script
|
Execution of the addentry1 script when the user has no write-permission for entry-file and then output the content of the entry-file using cat command.
|
Execution of addentry1 script when the user has write-permission for entry-file and then output the content of the entry-file using cat command.
|
Part2
#2. Lists are commonly stored in environment variables by putting a colon (:) between each of the list elements. (The value of the PATH variable is an example.) You can add an element to such a list by catenating the new element to the front of the list, as in
PATH=/opt/bin:$PATH
If the element you add is already in the list, you now have two copies of it in the list.
Write a shell function named addenv that takes two arguments: (1) the name of a shell variable and (2) a string to prepend to the list that is the value of the shell variable only if that string is not already an element of the list. For example, the call
addenv PATH /opt/bin
would add /opt/bin to PATH only if that pathname is not already in PATH. Be sure your solution works even if the shell variable starts out empty. Also make sure you check the list elements carefully. If /usr/opt/bin is in PATH but /opt/bin is not, the example just given should still add /opt/bin to PATH. (Hint: You might find this exercise easier to complete if you first write a function locate_field that tells you whether a string is an element in the value of a variable.)
Listing of the addenv script
|
Execution of the addenv script with a few test cases for the verification. You should print the value of the PATH before and after the run of the script.
|
Write a shell function named delenv that takes two arguments: (1) the name of a shell variable and (2) a string to be deleted from the list that is the value of the shell variable only if that string is already an element of the list. For example, the call
delenv PATH /opt/bin
would delete /opt/bin from PATH only if that pathname is already in PATH. Be sure your solution works even if the shell variable starts out empty. Also make sure you check the list elements carefully. If /usr/opt/bin is in PATH but /opt/bin is not, the example just given should still remove /opt/bin only from PATH.
Listing of the delenv script
|
Execution of the delenv script with a few test cases for the verification. You should print the value of the PATH before and after the run of the script.
|
Part2
#3. Write a function that takes the directory name as an argument and writes to standard output the maximum of the lengths of all filenames in that directory. If the functions argument is not a directory name, write an error message to standard output and exit with nonzero status.
Name the shell file to be maxfilename.
1. Listing of the script
|
2. Execution of the script with your home directory. You should list the file names in your home directory to verify the result.
|
3. Provide the list of the file names in your home directory to verify the result above.
|
4. Execution of the script with the directory ("/etc").
|
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started