Lab #6
CS 2351
UNIX Operating System
Name _________________________________
Section ___________________
This lab is designed to give you some familiarity with the shell and UNIX command files, called shell scripts. The shell scripts that you write as a part of this lab exercise are designed to run under the Korn shell. Print, shell variables, chmod, calendar commands are used. |
Shells
A shell is a portion of the operating system (systems program) referred to as a "Command Processor." This processor or interpreter reads and interprets the commands that you enter. Some shells offer utilities or commands to make your work more convenient. Besides being a command interpreter, the shell is also a programming language. You can write programs called "scripts" for the shell to interpret. These scripts can contain regular UNIX commands as well as special programming commands. Each shell has its own programming language and rules.
There are several shells that are available on UNIX machines so you have a choice as to which one you want to use. However, not all machines will have all of the shells. The default one for the Computer Science Departments particular machine (the Sun Microsystems Inc. ) is the Korn shell. Usually the default shell for most computers is the Bourne shell but our department decided to use the Korn shell as the default shell. When you log on to the computer, you will see the $ prompt on your screen. This tells you that you are in the Korn shell. However, the prompt for the Bourne shell is also the $ prompt. One way to know exactly what shell you are in is to type in:
echo $SHELL
What response did you get to this command?___________________________________________
You should get some answer such as /bin/ksh. The Korn shell is designated by the ksh. The bin is a directory where the shells are kept. The Bourne shell is the earliest UNIX shell to be developed. This shell is named after its primary developer, Steven Bourne at AT&T Bell Labs. This is the main shell that is used on every UNIX machine in the world. The Bourne shell is a command interpreter and has its own programming language also. It is represented by an sh.
There is another shell that is used frequently. It is the Korn shell. In the mid 1980s, a Bell Labs scientist, David Korn created a replacement for the Bourne shell, called the Korn shell. The name of the Korn shell program is ksh. It is intended to be a superset of Bourne shell features. Any shell script or command line that is acceptable to the Bourne shell should also work with the Korn shell. However, Korn shell scripts may not work correctly under the Bourne shell.
The Korn shell has three important features: the history command and command editing, job control, and aliasing.
The history command allows you to refer to commands that you have already used before without retyping them. 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. Job control will be discussed later.
You will use the Bourne shell later in some other labs. You will also be introduced to the C shell. The C shell is represented by a csh. There is also a % prompt instead of a $ prompt. You will learn about how the shells are similar and how they are different.
Before you go on to some other commands, you can see what other shells are available on the Computer Science Department's machine. Type in the following command:
cat /etc/shells
What shells were listed?___________________________________________________________
You should see several choices. The /bin/sh, /bin/csh, and /bin/ksh are the shells used most frequently. The others are used less frequently.
Print Command
The Korn shell uses the print command to print its arguments to standard output (the terminal screen). Its uses include producing error messages in shell scripts, and displaying the values of shell variables. If you were using the Bourne shell or the C shell, you would use the echo statement in its place. Issue the command on each line below. Write the resulting output display in the space provided to the right.
print HI _____________________________
print CS 2351 _____________________________
The print command is useful for producing diagnostic comments in shell programs and for writing constant data to the screen. For another example of print , type in the following:
print *
What was printed?
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
The * was used as a wildcard in an earlier lab. It lists all of the files that you have in this directory in horizontal form. Next, make certain you are in your home directory with a pwd command. If you were not already in your home directory, change to your home directory with the command:
cd
Create a new directory, lab6, by entering:
mkdir lab6
Change your working directory to lab6 with a cd command. Verify that this is your working directory.
What command did you use to verify the directory? ____________________________________________________
Shell scripts
A shell script is a file that contains commands to be executed by the shell. The commands in a shell script can be any commands you can enter in response to a shell prompt. You can add several commands into one shell script that can then be executed by using just one command. When you use a certain group of commands often, the shell script is a way to save time and do a complex series of tasks easily. An example below shows how you can put several commands together to make a shell script that you could use when you first log on to the computer each time.
Use vi to make a shell script called start. It should contain the following:
print Today is
date
print
print The directory that I am in is:
pwd
print
print The number of people on the system now is
who | wc -l
print
Save the document using the :wq. Use this file name as though it were a command by typing:
start
When you did this, you received an error message.
What was it?_______________________________________________________________
It is possible to look at the file characteristics with the ls command. Enter the command:
ls -l start
At the left of the line which was printed are 10 character positions.
List this string of 10 characters. __________________________________________________
The first character indicates a file type if the file has some special type of system's use. It will usually have a "-" for file, or a "d" for directory. The following characters represent these access codes:
r = read
w = write
x = execute
- = permission not granted.
The last nine characters are grouped to give permissions (in order, from left to right)
for the user (you), the group, and others. When talking about file privileges, we refer to
these as u (user), g (group) and o (others).
If you typed in the following command:
ls -l newfile
and you received the following access codes, -rwxrw-r--
The owner would have read, write and execute access. The group would have read and write access, and the other would have read access only. In order to make a shell script executable so it will be the same as a command, it must be given execute permission. You can do that by using the chmod command.
Chmod Command
The chmod command changes the access mode of a file. It can be changed for the user , group or others. The access codes can only be changed by the owner of the file or the superuser (the systems administrator). There are two different ways to change the access codes using the chmod command. The Symbolic mode and the Numeric mode. The symbolic mode uses different combinations of symbols and letters to modify the file permissions and may be easier to remember. Use the symbolic method for the chmod command to make start an executable file. Type:
chmod u+x start
The above command grants execute access to the user. To grant execute access to other user groups, you can use one of the following commands:
chmod u+x, g+x, o+x start
or
chmod a+x start
In the above example, u refers to user, g refers to group, o refers to others, a refers to all user groups. Similarly, you can remove access by substituting the + sign with a - sign. Now, issue the command:
ls -l start
Write the 10 character string displayed. ____________________________
Has it changed since the last time ? __________________________
How ? ____________________________________________________________________
Now type in: start
What is the result this time ? _________________________________________________
_______________________________________________________________________
You probably received an error message again. If the computer system is set up where your search path includes the current directory, the shell will run the commands in the file. However, it is not, the shell will give you an error message. On the Computer Science departments computer, you may see a message similar to: ksh: start: not found. If this happens, you can still get the shell to run your commands by typing in:
./filename <Enter>
NOTICE: There is a period and then the slash and then the filename. Type in:
./start
Hopefully, you will now have constructed an executable shell script in start. You can treat start as though it were any UNIX command as long as you stay in the current working directory. If you wish to use this command in your home directory, you would need to copy it to that directory.
NOTE: Anytime you are asked to make a shell script executable, you will have to type in the "./" before the shell script name to get it to work on our machine.
You can also use the numeric mode for chmod. It uses a three digit number that is computed by adding together the numeric equivalents of the desired permissions. The first digit represents the user permissions, the second digit represents the group permissions, and the third digit represents the world permissions. It looks like this table:
| Owner | Group | Other |
| r w x | r w x | r w x |
| 4 | 4 | 4 |
| + 2 | 2 | 2 |
| + 1 | 1 | 1 |
If you wanted to give access permission to the start file, you could use:
chmod 700 start
instead of the symbolic mode. If you wanted the group or other to have access to the file, you could use:
chmod 755 start
You will use the chmod command several times in the following labs. When you have been asked to copy files to your account, those files have a chmod 644 access to them. This allows you, the other, to access the files and copy them but you cannot write over that file. If you could, students would make changes in the class files. You may use either the numeric method or the symbolic method when you use this command.
Shell Variables
In the Korn shell you can use any string of characters for a variable. The only limitation is that a variable name cannot include any embedded blanks. You declare a shell variable by assigning a character string to it in the Korn Shell. The assignment operator is the equal sign (=). No blanks can appear on either side of the equal sign in a shell assignment.
Type the following on a line by itself.
person=Anna-Lee
This assigns the character string "Anna-Lee" to the shell variable person. It is possible to use the print command to print the value of a shell variable. A variable has both a name and a value. Type:
print person
What is displayed on the screen? _________________________________________________
This is the name of the variable, not its value. In order to see the value of the variable, you must use the explicit dereferencing operator, dollar-sign ($). Type the following:
print $person
What is displayed ? ____________________________________________________________
If there are spaces in the character string, then the string must be quoted. Either single quotes (') or double quotes (") may be used here. The ending quotation mark must be of the same type as the leading quotation mark. Each of these two ways of quoting strings in the shell has its own meaning. The meanings are sufficiently close for them to be used interchangeably here. In a later lab, you will learn when there are some differences using single quotes or double qoutes. Type:
person="Anna Lee"
The variable, person, should have a new value. Check this value with the command:
print $person
What is displayed ? ____________________________________________________________
This next example is a more complicated shell script. The shell has a variable for each item on a command line. The command itself is the value assigned to the variable named 0 (This is the number zero). The command may be considered to be argument 0 to itself. Other arguments on the command line are number 1, ..., n. You can display the value of these variables with the print command from within a shell script. Use the editor to create a new shell script named ACME. Put the following into the file:
print "The name of the shell script
is:" $0
print "The ACME Company has Departmental Totals"
print "for the Month of" $1
print "Accounting: " $2
print "Production: " $3
print "Research: " $4
print "Total is: " $5
This shell script can be a short report that is run every month and you can just fill in the particular information called "arguments" on a line and they will be used in the report. Next make this file executable by typing in:
chmod u+x ACME or chmod 700 ACME
Before supplying the results, you need a Total. Use the bc calculator to add up the three departments so you can have a Total for this report. Type in:
bc
scale=2
1243.95 + 1580.50 + 1970.37
What was the total that you got? __________________________________________
Next, press ctrl-d to get out of the bc and back to the $ prompt. You can now type in the information you need to complete the report. Type in:
./ACME June 1243.95 1580.50 1970.37 *total
(*Substitute the value you got in the last step for total)
What was displayed?
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
Create a new shell script in file ACME2 using vi. Make ACME2 have the following contents:
print "command $0
has $# arguments"
print "they are
$*"
Be sure that you use the double quotes here. In the next lab, you will make changes in this particular file. When you complete the file, use chmod to make ACME2 executable.
The exact command you used was: ______________________________________
Enter ACME2 on a line by itself. What is the result ? __________________________________
____________________________________________________________
Enter:
./ACME2 July
2555 4890 2854 10299
What is the result ? ___________________________________________________________
___________________________________________________________
The shell variables * and # have special meanings. The shell variables which give the total number of arguments on a command line (excluding the command itself) is:___________________.
The variable whose value is the list of all arguments (in the example above was the month and the totals) on the command line is ______________________.
Shell Scripts
It is possible to nest shell scripts and to call one from another. Create a new shell script in a file named combined. The file combined should contain the following lines:
./start
./ACME August 2679 0980
8876 12535
./ACME2 September 1000
7098 3567 11665
NOTE: The "./" has to be used in the shell script at the beginning of each shell script name or the new shell script will not work.
Use chmod to make the file named combined executable.
What command did you use to do this ? _______________________________________
Use combined as though it were a shell command.
What is the result ? ____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
Shell Variables within Shell Scripts
Shell variables can be used within shell scripts. Create a new shell script in a file named com which contains:
print $computer
After you have saved the shell script, then at the $ prompt, assign a value to computer as follows:
computer=IBM
Execute com. Did you receive an error message ? _______________________________
If so, make com executable, and try to execute com again.
Was there any output ? _________________________________________________________
You should have just received the $ prompt. This is due to the fact that the system forks a new shell to execute the shell script and the value of computer is not passed from one shell to the next. In order to achieve the desired result, the export command is used. Type:
export computer
This gives access to the variable computer to all subshells as well as the current shell. Type in:
./com
The result is:____________________________________________________________________
Use the editor to change the contents of com. Add the following to the file com:
computer="Sun
Microsystems"
print
$computer
Execute com. Was the value of the variable computer changed during the execution of the shell script? _________________
What is the value of the variable computer now? __________________________________________________
Type: print $computer
Notice that the value of computer is the same as it was prior to executing com. When a new shell starts, it has variables which are named the same as variables exported from the old shell. The values of these variables are initialized to the values of the corresponding value in the parent (old) shell. The new variables are local to the new shell. A change to a variable in the child (new) shell does not affect the corresponding variable in the parent shell.
At this time, you should have these files in your lab6 directory. These are:
start
ACME ACME2
combined com
Type in: print *
What was listed? _____________________________________________________________
___________________________________________________________________________
Return to your home directory. Type in:
cd
Calendar command
Many people keep track of their scheduled appointments by recording them in a book. Today, students may keep track of their tests and other important dates in a Personal Digital Assistant (PDA). If you don't have a small computer to carry around to record your appointments, you can use the calendar command on the computer. At the beginning of each day, you can consult the calendar to see what you need to do for the day. At the end of the day, you may want to see what you have planned for tomorrow so you can be prepared.
The calendar command allows you to type in the message or appointment along with the date. You can use any format for the date, but it must have the month and the day of the appointment. The date may appear anywhere on the line, and it may be spelled out (April 12), abbreviated (Apr 12), or written in numerals (4/12). However, you must put the Month before the Day. If you include the year, it will be ignored. Calendar assumes it is the current year. The calendar file should be made in your Home directory.
Now you can make a calendar file. Type in:
date
What is the date today? _____________________________________________
Now make the calendar file by starting with:
vi calendar
Look at the Month and Day that you got when you typed in the date command above. Start the calendar using the date for tomorrow and the next day. If the date above was Jun 18, then you would start with Jun 19 in the calendar. Follow this example below, except use the dates for tomorrow and the next day:
May 26: Be sure to copy Lab 8 for class
tomorrow
Midterm Exam 05/28
May 26: Call John about
the project for next week
Final exam for
UNIX Jun 6
Save the file. You can look at it by typing in:
calendar
What was printed out? __________________________________________________________
The calendar command will search through the calendar file for either today's date or tomorrow's date. It also remembers that "tomorrow" extends through Monday if "today" is Friday. When it finds such a line, it simply displays it. This means that if you have an appointment that extends on more than one line, you must put a date on every line.
The calendar file must be completely maintained by you. You have to add and remove appointments from the file. The system does not do it for you. If you want to add more items to the calendar, use the vi editor to add and delete items.
DO NOT REMOVE ANY FILES YOU HAVE CREATED IN THIS LAB. You will be referring to them in later labs. You should have the calendar file and the Lab6 directory in your HOME directory.
Please indicate any parts of this lab that were hard to
understand.
_________________________________________________________________________
_________________________________________________________________________
05/21/03