The Korn Shell is the UNIX shell (command execution program, often called a command interpreter) that was developed by David Korn of Bell Labs as a comprehensive, combined version of other major UNIX shells. Incorporating all the features of the C shell (csh) and the Tab C-shell (tcsh) with the script language features similar to that of the Bourne Shell, the Korn Shell is considered the most efficient shell. Korn, Bourne, and C are the 3 most commonly used UNIX shells. You will be using the Korn Shell (ksh) for this project in which you will be writing shell script files.
1. Begin by executing the following command from a terminal session in your Ubuntu system:
sudo apt-get install ksh <Enter>
You will immediately be asked to enter your password (csci345). This will install the Korn shell into your Ubuntu system on your PC or Mac for use in this project. Simply type ksh and <Enter> to go into the Korn Shell (your system prompt will change to $ only) and then enter CTRL-D to exit out of it (your default system prompt of csci345@csci345-vm:~$ will re-appear).
As you go through this exercise and start writing shell scripts, you will learn some additional troubleshooting tips to help you navigate through the Korn Shell (ksh).
Keep track of some of these tips (5 of them) and put them into a Word document in a list format with explanations (at least 150 words in current APA format). Make sure that these are different from those mentioned in the textbook.
2. Do some research on what the Korn Shell is about and what it has to offer. Provide some basic information (3–5 key points) in a short report (at least 150 words in current APA format) and compile it into the same Word document as above and submit it with this project. Also, provide 5–7 positive features of the Korn Shell (ksh) in the same report (at least 150 words in current APA format) in a list. Below are 3 websites that can assist you in this effort. You will need at least 2 external references for this short report (in addition to your textbook):
– http://kornshell.com/
– http://www.dartmouth.edu/~rc/classes/ksh/print_pages.shtml
– http://www.bolthole.com/solaris/ksh.html
There are many other websites out there that will give you additional help, if needed. Take a look at the ksh shell script below. There are 3 versions of essentially the same program, which is a wrapper to edit a file under SCCS version control. The basic task is to use the sccs command to “check out” a file under version control and then automatically edit the file. The script will then be used by users who may not be particularly advanced UNIX users. Hence, the need for a wrapper script. While the basic functionality is the same across all versions, the differences in safety and usability between the first version and the last (4th) version are worth noting. The first one is extremely bad: it would be written by someone who has just picked up a book on shell scripting and has decided, “I’m a programmer now.”
#!/bin/ksh
sccs edit $1
if [ “$EDITOR” = “” ] ; then
EDITOR=vi
fi
$EDITOR $1
This version makes somewhat of an attempt to be user-friendly by having a check for an environmental variable EDITOR setting and using it, if available; however, there are no comments, no error checking, and no help for the user at all. The second one is an improvement, showing some consideration to potential users by having safety checks.
#!/bin/ksh
# Author: Name Here
if [ $# -lt 1 ] ; then
print “This program will check out a file, or files, with sccs”
exit 1
fi
# set EDITOR var to “vi” if not already set
EDITOR=${EDITOR:-vi}
sccs edit $@
$EDITOR $@
This is a step above the prior version. It accepts multiple files as potential arguments. It is always nice to be flexible about the number of files your scripts can handle. It also has a usage message if the script is called without arguments. Plus, it is always a good idea to put your name in as well. Unfortunately, there is still quite a bit lacking as you can tell by comparing it to the next version. The third one is a good, solid version; it is a positive role model for your scripts.
#!/bin/ksh
# SCCS editing wrapper, version 0.3
# Author – Sys Admin
# Usage: see usage() function, below
usage(){
print sedit – a wrapper to edit files under SCCS
print “usage: sedit file {file2 …}”
}
# Set EDITOR var to “vi” if not already set to something
EDITOR=${EDITOR:-vi}
# Could already be in path, but it doesn’t hurt to add it again.
# adjust as needed, if your sccs lives somewhere else
SCCSBIN=/usr/ccs/bin
if [ ! -x $SCCSBIN/sccs ] ; then
print ERROR: sccs not installed on this machine. Cannot continue.
usage
exit 1
fi
PATH=$SCCSBIN:$PATH
if [ $# -lt 1 ] ; then
usage
print ERROR: no files specified
exit 1
fi
# Yes, I could use “sccs edit $@” and check for a single error, but # this approach allows for finer error reporting
for f in $@ ; do
sccs edit $f
if [ $? -ne 0 ] ; then
print ERROR checking out file $f
if [ “$filelist” != “” ] ; then
print “Have checked out $filelist”
fi
exit 1
fi
filelist=”$filelist $f”
done
$EDITOR $filelist
if [ $? -ne 0 ] ; then
print ERROR: $EDITOR returned error status
exit 1
fi
Here are some nice things to note with this script:
· Sets special variables at the top of the script in a unified place;
· Paranoid checks about EVERYTHING;
· Returns an error status from the script on non-clean exit condition (“exit 1” vs. “exit”); and
· Use of comments. Not only does he/she specify what he/she is doing, he/she clarifies what she/she is NOT doing and why.
3. Now that you have the Korn Shell (ksh) installed and have learned more about the language itself, it is time to do some programming:
a. Write a Korn shell script that accepts exactly 1 command line argument that must be a positive integer. The script will print a comma separated list of integers, all on the same line, starting with the initial command line value and decreasing it by one-to-one. The last printed value must not be followed by a comma. Do not write this script to be executed interactively.
Requirements:
The script must be to handle the following error situations:
1. Incorrect number of arguments and
2. Non-positive arguments.
The script file name must be: printnum.sh
The script permissions must be 705 (show in your Word document).
Sample Output (provide yours in same Word document)
Sunny Day Scenarios:
csci345@csci345-vm:~$ sh printnum.sh 3
3, 2, 1
csci345@csci345-vm:~$
csci345@csci345-vm:~$ sh printnum.sh 10
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
csci345@csci345-vm:~$
Rainy Day Scenarios:
csci345@csci345-vm:~$ sh printnum.sh
error: program must be executed with 1 argument. usage: printnum.sh value (where value >= 1)
csci345@csci345-vm:~$ sh printnum.sh 2 5
error: program must be executed with 1 argument. usage: printnum.sh value (where value >= 1)
csci345@csci345-vm:~$ sh printnum.sh -1
error: argument must be a positive number. usage: printnum.sh value (where value >= 1)
Add at least 2 more scenarios in your Word document.
b. Write a Korn shell script that will determine which file in a directory has the maximum number of lines (this may be different than the file that has the maximum number of bytes). After determining the file with the maximum number of lines, the script will print out the name of the file and the number of lines. The script must only focus on files and ignore subdirectories. The command wc may be helpful. Do not write this script to be executed interactively.
Requirements:
· The script must allow either no arguments or 1 argument.
1. If zero arguments are specified, the script by default will examine the files in the current directory.
2. If 1 argument is specified, the argument must be the name of a directory. The script will then examine the files in the specified directory.
The script must be able to handle the following error conditions:
1. More than 1 argument is specified.
2. The specified argument is not a directory.
The script file name must be: maxlines.sh
The script permissions should be 705
Sample Output (provide yours in same Word document)
Sunny Day Scenarios(Note: output depends on current directory and may not match the values below):
csci345@csci345-vm:~$ sh maxlines.sh
File maxlines.sh has maximum number lines (36 lines).
csci345@csci345-vm:~$ sh maxlines.sh /etc
File mime.types has the maximum lines with 827 lines.
Rainy Day Scenarios:
csci345@csci345-vm:~$ sh maxlines.sh junk
error: argument must be a directory usage: maxlines.sh [directory]
csci345@csci345-vm:~$ sh maxlines.sh junk trash
error: can only use 0 or 1 arguments. usage: maxlines.sh [directory]
Add at least 2 more scenarios in your Word document.
c. Create a search utility by writing 2 Korn Shell scripts named srchfile.sh and srch.sh, respectively. The script srchfile.sh will list all occurrences of a string found in a file. The script srch.sh will list all occurrences of a string in a file or all files in a specified directory. Actually, srch.sh will use the script srchfile.sh as if it were just another UNIX command to perform the actual search in a file. In other words, the script srch.sh needs to be able to use the script srchfile.sh. Therefore, this assignment recommends that the first script created be the srchfile.sh script. The script srch.sh calls the srchfile.sh script.
Requirements:
Part 1
Description.
The shell script srchfile.sh must accept 2 command line arguments.
1. The script shall first print the name of the file being searched.
2. If there are one more instances of the string in the file, the script shall print each line that contains the matching string, along with the line number of the file.
You may use the UNIX command grep in your script to do the actual search. In fact, grep can even print the line number if used with the appropriate option (see man page for grep).
If the user enters more or fewer than the required number of arguments, the script shall produce an error message and stop.
The first argument shall represent the string to be searched.
The second argument shall represent the name of the file to be searched. If the second argument is not a file, the script shall produce an error message and stop.
The script file name must be: srchfile.sh.
The script permissions must be 705
Sample Output (provide yours in same Word document)
In the Sunny Day Scenario, the srchfile.sh script is invoked searching for string root pattern in file passwd in the /etc directory:
csci345@csci345-vm:~$ sh srchfile.sh root /etc/passwd
—— File = /etc/passwd ——
1:root:x:0:0:root:/root:/bin/bash
In the Rainy Day Scenario, script srchfile.sh is invoked with only 1 command line argument:
csci345@csci345-vm:~$ sh srchfile.sh xxxx
error: must provide 2 arguments
usage: sh srchfile.sh search-pattern file
In the Rainy Day Scenario, script srchfile.sh is invoked with second command line argument /etc., which is a directory rather than a file:
csci345@csci345-vm:~$ sh srchfile.sh xxxx /etc
error: second argument must be a file
usage: ./srchfile.sh search-pattern file
Add at least 2 more scenarios in your Word document.
Part 2
Description.
Now, write a second Korn Shell script named srch.sh. The srch.sh script will call the srchfile.sh script to perform a specific task and accept 2 command line arguments. The first argument shall be a string (pattern) found in the file being searched. The second command line argument shall be the name of a file or a directory.
Requirements:
The second argument represents either the name of the file to be searched or a directory. If a directory is entered, then all files in the directory shall be searched.
The script srch.sh shall use the script srchfile.sh to search for the string in a given file. If the second argument is a directory, then srch.sh will invoke srchfile.sh repeatedly in a loop.
If the user enters more or fewer than the required number of arguments, the script shall produce an error message and stop.
The script file name must be: srch.sh.
The script permissions must be 705.
Sample Output (provide yours in same Word document)
Sunny Day Scenario, the srch.sh script searching for string root in the file /etc/passwd:
csci345@csci345-vm:~$ sh srch.sh root /etc/passwd
—— File = /etc/passwd ——
1:root:x:0:0:root:/root:/bin/bash
Rainy Day Scenario, script srch.sh is invoked with only 1 command line argument:
csci345@csci345-vm:~$ sh srch.sh root
error: must provide 2 arguments
usage: sh srch.sh search-pattern file
Rainy Day Scenario, script srch.sh is invoked with 3 command line arguments:
csci345@csci345-vm:~$ sh srch.sh root1 root2 root3
error: must provide 2 arguments
usage: sh srch.sh search-pattern file
Add at least 3 more scenarios in your Word document.
d. In this exercise, you are to write a Korn shell script that spawns 5 processes and then kills them.
Requirements.
This homework assignment requires the creation of 2 Korn shell scripts; 1 script shall be named simple_script.sh and the main script will be named process.sh.
The source code for simple_script.sh is given to you below.
Simple_script.sh script
Create a small shell script file called simple_script.sh with the following contents:
echo “I am simple_script.sh with process ID $$” sleep 5
The simple_script.sh script file name must be: simple_script.sh.
Try running simple_script.sh at the command line; it will print out its process ID and then sleep for 5 seconds before terminating. Each time you run the script, it will have a different process ID.
process.sh script
Design your script named process.sh as follows:
Have your script create 5 instances of simple_script.sh in the background. Each time the script process.sh creates an instance of simple_script.sh, the
shell variable $! in process.sh will contain the process ID of the latest
simple_script.sh script.
After your process.sh script has created 5 instances of simple_script.sh, let your process.sh script sleep for 1 second and then let your process.sh script start killing the processes using the process ID obtained from $! (see man page for kill command).
Your script must only start killing processes AFTER it has created ALL 5 instances of simple_script.sh (i.e., DO NOT create simple_script.sh process 1. Kill it and then create process 2, kill it and so on).
You MUST use a shell array for the process.sh script. Use the shell array to store the process IDs of the simple_script.sh processes.
The script file name must be: process.sh
The script permissions must be 705.
Sample Output (provide yours in same Word document)
Sunny Day Scenarios (add here) …
Rainy Day Scenarios (add here) …
Below is a sample of the output messages from process.sh (NOTICE the order of how the scripts are created and destroyed in the same order):
csci345@csci345-vm:~$ sh process.sh
I am simple_script.sh with process ID 12301
I am simple_script.sh with process ID 12302
I am simple_script.sh with process ID 12305
I am simple_script.sh with process ID 12307
I am simple_script.sh with process ID 12309
process is killing process 12301
process is killing process 12302
process is killing process 12305
process is killing process 12307
process is killing process 12309
Add at least 2 scenarios in your Word document.
·
· Submit a zip file with all Korn Shell script files for step 3, parts a–d, and submit your Word document for steps 1–3 (scenarios).
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more