Lab #7
CS 2351
UNIX Operating System

Name _________________________________

Section ___________________

This lesson will cover the Korn shell and its features such as the .profile and .kshrc files. Use of the history command, command line editing, and aliases will by discussed. The read command, and comments, and special meaning to characters in print statements will be used. 

In this lab, you will learn about some of the special features of the Korn shell. These are:

1) the history command and command editing,
2) and aliasing .

The history command allows you to refer to commands that you have already used before without  retyping the entire command again. It also allows you to use an editor to change a command that you typed in already that has an error in it. Aliases are used to change commands to something else that you would rather use. The Korn shell also has more built-in functions that can be performed directly by the shell.

Be sure that you are in your home directory. You will need to make a few changes to a few special files. These are the .profile file and also make a new file, .kshrc. The ksh uses these two files as startup files.

Making Startup Files for the Korn shell

The .profile file works when you first login to your account. One of the most important uses for a startup file is to set the values of variables used by the shell. The .profile file also is used in the Bourne shell. A variable is a named storage location that can hold a value. You will be setting environment variables and also user-created variablesEnvironment variables are special shell variables that are predefined shell variables that hold information about the computer system that the shell needs to operate correctly. You will be changing some of these to fit your needs. These variables are listed in capital letters.

Some Environment variables are: HOME, PATH, USER, PWD, PS1, MAIL, SHELL, ENV.  You will be using some others also.

The user-created variables are defined by you and are called personal variables. These are in lower case letters.

The .profile file

First, you will need to change your .profile file. This file only works once when you first logon to the  system. Type in:

                vi .profile

This particular file already has one line in it.  It is:

                stty erase '^h'

This file is case sensitive meaning that you must type in upper case letters in upper case and lower case  letters in lower case or they will not match. Add the next lines that are listed in bold to this file:

stty erase '^h'
PATH=$PATH:$HOME/bin
export PATH
ENV=$HOME/.kshrc
export ENV
calendar
rm .sh_history
 

Save the file. Normally after you have made any changes in a file that begins with the (.)--period,  you need to logoff your account and log back in again before they become effective. However you can use the "dot" command. At the prompt, you can type in a period, a space, and the name of the file. Type in the following:

                $ . .profile

Press the return key. The .profile file will be executed just the same as if you logged out and logged back on again. There are a few new commands used in the .profile. The PATH command was used in this file. The PATH variable controls the search path to find commands and applications you request. Without the PATH , the shell wouldn't be able to find any of the many UNIX commands. In this case, the bin directory is mentioned. More directories can be listed.

The $HOME is the name of the directory that you always begin your UNIX session on.

The ENV refers to the environment variables. You will be changing some of these in the .kshrc file.  You may receive a message saying that the ".sh_history" file is non-existent.  Don't worry about the message.  The line containing rm .sh_history will be explained shortly. 

Note that you added the 'calendar' command.  It was added to the startup file, so every time you logon on to your account, the calendar will be checked and if you have information for that particular date, the information will automatically be listed for you.  This saves you typing in the command each day that you get on the computer.

The .kshrc file is another special file that you need in the Korn shell. Once you have logged on to the  system, every time you type in a command, the .kshrc file is accessed. This particular file contains many Environment commands and also the aliases. Aliases change a UNIX command to some other command you may prefer to use in its place. It may be a command that you are more familiar with. You will notice that some of the lines start with "#". These are comment statements and are used for documentation. You will learn more about them later.

The .kshrc file

Use the editor to make a file called .kshrc. Type the following into this file:

# This is the .kshrc file with ENV,
# aliases, and set
# First, changes in the ENV commands
EDITOR=/usr/bin/vi
FCEDIT=/usr/bin/vi
PS1='! $PWD > '
HISTSIZE=100
export EDITOR FCEDIT PS1 HISTSIZE
# Aliases used in the Korn shell
alias ll="ls -F | more"
alias dir="ls -l | more"
alias del=rm
alias la="ls -al | more"
alias h=history
alias f=finger
alias p=print
# set commands
set -o noclobber

Do not omit any of these lines and be sure to type these in as they appear above.   Many questions that must be answered in this lab depend on the contents of this file.  Save this file.  In order to get this one to work, you need to use the dot command as you did with the .profile file.  Type in:

                $. .kshrc

Now the .kshrc file should become effective. The first thing you should see is that your prompt has  changed from the $. It should have a number followed by the directory that you are now in--which should be your home directory. If your path name to your home directory was /d/srp, your prompt would now look similar to this:

                15 /d/srp >

The number that you see may not be the same as the one above in the example. It could start with another number. This number is the number of the command that will be executed and will be used with the history command that you will soon learn to use. The exclamation point in the line PS1='! $PWD >' is what gives you this command number.  When you change directories, the prompt will show you the entire path name just as if you had executed the pwd command. As you may have guessed, the $PWD is what does this. This is an explanation of what the other commands in the .kshrc file do.  ENV commands are:

EDITOR - This is the editor that will be used when you use the history command to make changes in commands that you have typed in before.

FCEDIT - This is used in the history command and works with the built-in fc command which stands for "fix command." It allows you to re-execute a specified set of previous commands, and secondly it allows you to make changes in them before re-executing them.

PS1 - This is the primary prompt. If you don’t change this prompt, it will have the $ prompt just the same as in the Bourne shell. However the ! means that there will be a number before the prompt. This number keeps track of all of the commands that you enter. These line numbers can be used to repeat commands. The $PWD will list out the directory in the prompt line that you are in also. You can change this prompt to something else if you don’t like this one.

HISTSIZE - It tells how many commands are kept for the history file. You will set it to 100. If none is listed, the default is 128.

HISTFILE - This is the file where the list of commands is saved. The default is called the .sh_history file. You will not be changing it. One problem with this file is that if you don’t delete the commands from that file each time you login, the numbers keep getting larger. If you want to start them at "1" each time you logon, you must remove the old commands from the file. You entered the rm .sh_history in the .profile file. This will allow you to start out at the beginning each time you logon to the computer.

export — the export command exports all of these commands to all the subshells so these commands are available to you.

Aliases - Aliases allow you to customize your own commands by using the alias command. If you would prefer to change the name of one of the commands to something that is easier to remember, then you can use an alias.  A renamed command may be used to shorten long commands or to protect you from accidentally deleting an important file. An example is:

        alias la="ls -al | more"

The la is the command that you will type in. The ls -al | more is the command that you would normally have to type in. There is no space between the new word and the "=" sign and the other command. Notice that if the command is longer than one word, it has to be enclosed in quotes. If the command is only  one word, it does not need quotes around it.

You were given a number of aliases to try out. You can add to this list with any  other commands that you may wish to use. You can still use the commands the regular way also and they will work.

Set - The ksh shell includes many options that change its behavior and customize it to your preferences. These options are changed with the set command. The one that you will use here is the set -o option. The -o turns on the option and the +o turns off the option. The set -o noclobber prevents the redirection operator (>) from overwriting an existing file. This is very useful for catching mistakes and saving files that might otherwise be lost. You will get a message that the file already exists so you won’t write over a file that exists.

After you have fixed these two files and used the dot commands to make these files become active, you should have a prompt similar to this:

                1 /d/srp >

There will be a number followed by your home directory.

Testing out the History and Aliases

To see how some of the aliases work, type in the commands in bold:

                 1 /d/srp > dir

You should receive a list all the files and directories in your home directory. Try the next one:

                 2 /d/srp > la

This one should give you a list of all the files in your directory including the invisible files. Name three invisible files that were listed:

_______________________________________________________________________

Next, type in:

                 3 /d/srp > ll (These are two lower case "L"s)

This is short for the ls -F | more command. The -F gives you some new information that you have not seen before. If you have a directory in the list, the name of it will be followed by a "/" at the end. This way you can tell if it is a directory easier. If you have a file that is executable, there will be an * (asterisk) next to the name.

Look at the list and write down two files that have the / at the end of their names.

________________________________________________________________________

Did you see any files with an * at the end of it at this time? _______________________

Next, change to the directory, lab4. Notice your prompt has now changed to reflect the new directory  that you are in. It should look similar to this:

                 5 /d/srp/lab4 >

You can now try out some of the features of the history command where you can re-execute or make changes in commands you have already typed in. Next type in:

                 5 /d/srp/lab4 > cat employee > books

You should receive an error message.   What does the message say?

_________________________________________________________________________

Why did you get this message? _________________________________________________

_________________________________________________________________________

You will not be able to copy this file to books. Type in the following commands:

6 /d/srp/lab4 > date | cut -c5-11
7 /d/srp/lab4 > date +%A
8 /d/srp/lab4 > cal
9 /d/srp/lab4 > h

When you type in h, which is aliased as history, you will receive a list of commands that you have already typed in. If you have followed the lab, it will look very similar to the following list. If it does not, execute the commands in this list:

1 dir
2 la
3 ll
4 cd lab4
5 cat employee > books
6 date | cut -c5-11
7 date +%A
8 cal
9 h

You can now re-execute some of the commands that you have already typed in or make changes in those you already typed in. You will get a list of the last 16 commands that you used. In order to redo a command, you can use the r command. If you just type in r at the prompt, it will re-execute the last command that you typed in. Try this by typing in:

                 10 /d/srp/lab4 > r

What happened? _____________________________________________________________

Which command was executed? ______________________________________

You can also refer to any command that you have done in the past by using the r and referring to the  number of the command that you want to repeat or you can refer to the first letter of the command. Next, type in the r and the   number of the command for the ll command (this command should have been previously executed so should be in your command history). In the example above, it would look like:

                 11 /d/srp/lab4 > r 3

What command was executed? __________________________________________________

What output did you receive? ____________________________________________________

___________________________________________________________________________

___________________________________________________________________________

Another example is the use of r and a letter of a command. Type in the following:

                 12 /d/srp/lab4 > r c

What command was executed? __________________________________________________

What was listed this time? ______________________________________________________

You should have had at least three commands that started with the letter c. If there are some that are similar, the most recent is repeated. You can also choose by adding an extra letter to distinguish the difference in them.

Next, you can make changes in the lines that have already been typed in. You do this by using the escape key at the command prompt. Try an example now:

                 13 /d/srp/lab4 > (press the escape key)

You are now in the command mode just like when you use the vi editor. If you want to go up to another command, you can press the k key and the most recent command will appear at the prompt. Keep pressing the k key until you get to the command cat employee > books. You were not allowed to add the employee file to the books  file because you already had a file by that name. You can make a change in the line with the command. When the line comes up on your command line, use the vi editor commands to add an extra > to the command. Use the a or i to make the changes in the command. After you have made the changes, press the return key and the command should be executed.

13 /d/srp/lab4 > cat employee >> books
14 /d/srp/lab4 > cat books

Try one more example. Press the escape key again and go to the line that says date +%A.  Change the A to D and press the return key to execute the command. What was the result?

__________________________________________________________________________

Return to your home directory and then go to lab6. Make sure that you are in that directory as you will be using some files in that directory. What does the prompt say?

__________________________________________________________________________

Read Command

Sometimes when writing a shell command, you may want to have the user enter some data from the  terminal. The shell statement read exists for this purpose. The read command is a feature of the Korn  shell and the Bourne shell. The simple format is:

                read <variable list>

When this statement is executed, the shell reads in a line from the terminal and assigns the values typed  into the shell variables specified in the variable-list. If only one variable is listed, then the entire line is assigned to that variable. Values typed in are delimited by blanks or tabs. The Korn shell also has some  other features which you will see shortly.  Use the editor to enter the following shell script called copy. This script can be used when you want to copy one file to another file:

#! /bin/ksh
print "Source file: \c"
# program will stop so you can type in a source file
read source
print "Destination file: \c"
# program will stop to read in a destination file
read destination
cp $source $destination

Make this file executable.  What is the command to do this?

_________ ________________________________________________

Type in:     copy

When you are asked for the "Source file", type in:    combined

When you are asked for the "Destination file," type in:   com2

Next, enter the command:         ls

What files do you now have in the directory?  ______________________________________

_________________________________________________________________________

Notice the use of the "\c" on the print lines. You will see what it does later in this lab.

Comment Statements

Notice that the # (pound sign) was used in the file. It is used as a comment statement. Everything on the rest of that line is ignored by UNIX. You can use them anywhere in your file if you want to document your file. HOWEVER, the use of the # sign on the first line can have different meanings depending on which shell you are using. Some rules are:

* If it is used on the first line as just # as the first character, then the script is interpreted by the C shell
* If the first line is of the form #! path name, then the executable program path name is used to interpret  the script.

An example of using the # sign with the Bourne shell is:

        #! /bin/sh

This would tell the computer to run the program in the Bourne shell. If you wanted to execute the script in the Korn shell, you would use:

        #! /bin/ksh

* If there is no # sign in the first line, the script is interpreted by the default login shell, which is the Korn shell. At this time, you have not been introduced to the Bourne shell or the C shell. You will learn about them in later labs.

Other Meanings for Read in the Korn Shell

The Korn shell also has other uses of the read command that are not available in other shells.
These are as follows:

Format Meaning
read answer Reads a line from standard input and assigns it to the variable answer. This is the same as in the Bourne shell.
read first last Reads a line from standard input to the first white space or new line, putting the first word typed into the variable first and the rest of the line into the variable last.
read response?"How are you?" Displays "How are you?" to standard error  and waits for user to type a reply, then puts the reply in the variable response. This form of read requires and accepts only one variable. What ever the user types until a new line, will be stored in response.
read Reads input into a built-in variable REPLY

Type in the following shell script called major to use the test out the different read statements.

This shell script uses all four uses of the read command.

#!/bin/ksh
# Script to find out a student’s name and major
print What year are you in college?
read year
print "I am a $year."
print "What is your full name?"
read first middle last
print "Hello $first"
read major?"What is your major?"
print "Wow, $major is a tough degree, $first."
print "What town do you live in now?"
read
print $REPLY is a nice place to live.

Make this file executable and then try out the file by typing in:            major

When you are asked for information, type in some responses to see what happens.

Next, type in:             ll

These are lower case "L". You will see files with asterisks after them. This means that the files are  executable. Name three files that have asterisks after them.

                        ___________________________________

                        ___________________________________

                        ___________________________________

Special meaning to certain characters in print

Note that there are a few new features in the file copy. One of the features is the \c in the print  commands. The print command gives special meaning to certain characters that are preceded by a backslash \. These characters give you more flexibility and control in your output displays.

Staying on the Same Line: \c

Normally, after print displays its last argument, it goes to the beginning of the next line. This is usually what you want. However, if you want to display a part of a message on the same line, you can use the characters " \c" at the end of a print command. Then print will not automatically go to the beginning of the next line. It will stay right where it is.

Use the editor to make a file called number. It should contain the following:

print "one \c"
print "two \c"
print "three"

Make the file executable. Then run it. What was the output from the file number?

_____________________________________________________________________________

Moving to the Next Line: \n

If the print command sees a backslash character followed immediately by the letter n, then a new  line will be displayed. The cursor will go to the beginning of a new line. Make a new file called number2. It should contain:

                print "one\ntwo\nthree"

Make the file executable and run it. What was the output?

                                ________________________________

                                ________________________________

                                ________________________________

Make a new file called number3. It should contain:

                print "1\t2\t3\t4\t5"

What is the result?   ____________________________________________________________

The "t" stands for tab. UNIX assumes that tabs are set at every 8 positions starting with position 1.  So tabs would be at 1, 9, 17, 25, etc.

You should have the following files now:

New files added to your home directory:

.profile
.kshrc

New files added to your lab6 directory

start ACME ACME2
combined com com2
copy major
number number2 number3

Please indicate any parts of the lab that were hard to understand.

___________________________________________________________________________

___________________________________________________________________________

05/25/03