Lab #10
CS 2351
UNIX Operating System
Name _________________________________
Section ___________________
This lab introduces you to features of the C shell. Commands that you will use in this lab include: history, and source. The .login file and the .cshrc file are also explained. The feature for reading in information into a C shell file is introduced. The set command is also discussed. |
The C shell
Next, you will be introduced to a new shell, the C shell. The C shell was written by Bill Joy at University of California at Berkeley. It was designed as the Berkeley alternative to the Bourne shell, which was the standard shell. The C shell has most of the same features as the Bourne shell. However, it has some other advantages. Some of the advantages are that you can customize your account more easily to fit your needs similarly to what you did in the Korn shell. The C shell will be covered in the next two labs.
By the way, you cannot use the command line substitutions in the C shell. You also cannot use the aliases that you set up in the Korn shell. However, you will learn to set up aliases and a history command in the C shell.
At this time, you are going to change your default shell from the Korn shell to the C shell. When you change to the C shell, you will have a % prompt.
Issue the following command:
chsh
You will get some reply such as:
Changing login shell for <loginname>
Old shell: /bin/ksh
New shell:
In the case of the "loginname", your login name will be listed. The shell you are in will be listed as the Old shell. At the New shell: prompt type in:
/bin/csh
The command chsh stands for "change shell". You have been successful at changing your default shell if the only response you got after issuing the above command was the shell prompt that you already had when you logged on today.
You need to do one more thing before you can log off the system and log back on in the C shell. In the Korn shell, you set up a .profile file. This file works when you first logon to the system. In the C shell, you need to have another file that is called the .login file. It works the same way that the .profile file did. Every time you login to your account the .login file is used. Be sure that you are in your home directory.
Use the editor to put the following lines in a file named .login
stty erase '^h'
set prompt = '! % '
echo HELLO Today is `date +%A`
set history = 25
set path = ($path $home/bin)
NOTE: the marks that surround the date command are the backquotes. You will learn more about this file shortly. Now logoff and log back on. You should now see something similar to this:
HELLO Today is Tuesday
1 %
This is the prompt for the C shell. The first line in the file, .login allows you to use the backspace key to erase a letter. The "set prompt" sets the prompt for the C shell to a number and the percent symbol (%) . The default for the C shell is the percent sign. When you define a shell variable in the C shell, you always start with the set statement first. You will learn more about this later in the lab.
When you make changes in the .login file, you usually have to logoff your account before they become effective. However, if you make changes in them during a login session, you can issue the command:
source .login
The new changes will become effective immediately.
Type in:
echo $shellWhat reply did you get? ___________________________________________________________
You should now see that you are in the C shell.
Another C Shell file - .cshrc
The interface between the user and the system is called a shell which interprets commands and calls programs to process them. The shell also serves as a high level programming language. The C shell has three files in your home directory that are used to tailor UNIX to your needs. These files have the filenames .login (which you already saw above), .cshrc, and .logout. The .logout file can contain commands that execute when you logout.
The C shell executes the .cshrc (C shell run command) file located in your home directory each time you execute a command while you are in the C shell. This is similar to the .kshrc file in the Korn shell. It is mainly used to keep aliases in it.
Aliases
Many of the names of commands in UNIX are not easy to remember and are not "user friendly" or intuitive, which you may have already discovered! It is possible to rename UNIX commands using alias to change the commands to something that is easier for you to remember. You have already seen how you can use aliases in the Korn shell. You will now be able to make changes in the C shell. You will see some aliases here but you also can use others if you wish.
In order to see how the .cshrc file works, create your .cshrc file by typing:
vi .cshrc
Enter the following aliases in your .cshrc file:
alias copy cp
alias del rm
alias dir "ls -l | more"
alias cd 'cd \!*; echo $cwd'
alias rm "rm -i"
alias ll "ls -F | more"
alias la "ls -al | more"
alias h history
alias f finger
Notice that when you made up aliases in the Korn shell, you used the "=" sign between the word you wanted to use and the command you were substituting. In the C shell, you dont use the "=" sign. These aliases become effective when you log on next time. However, you can use the source command along with the .cshrc command to make this file effective immediately. To do this, type in:
source .cshrc
You can now use the commands in the .cshrc file. Every time you change the .cshrc file, you must use the "source" command again or log off your account and back in again.
You can use the new words such as: copy, del or dir instead of the other commands that UNIX uses. The other commands are still available to you. As you learn other commands that are hard for you to remember, you can add other aliases to the .cshrc file.
You just entered an alias for the cp command. You also typed in an alias for "ls -l". In order to see if these aliases work, type in the following:
copy filea filed
dir
ll
What happened when you did this?_______________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
History
When you first logon to your account, your prompt starts with 1 %. Each time you type in a command, the number will be incremented. This is the history command working. The history starts when you login using the C shell even if you did not have the history activated before. The history command will keep a record of the most recent 15 commands issued in your current session. You may view a list of the recent commands by typing the command history.
Change to lab6. What command did you issue to accomplish this ? ______________________________________________________
When you change to lab6, you should see a listing of the directory name. This is because of the alias statement for the cd command. Besides the % prompt, what was listed? _________________________________________________________________________________
Next type in: dir
What is the result? ____________________________________________________________________
Type in: ll
Name two of the files that have an * at the end of their names:
__________________________________________________________________________________
What kind of files are these? ___________________________________________________________
Next, type in: cat newstart
Next, issue the command: h
What was the result?__________________________________________________________________
__________________________________________________________________________________
Type in the following commands:
cp ../lab5/employee .
cat employee
date
cal
la
Now use the alias command for history. Issue the command:
h
The number of events retained in the history file is determined by the set history = 25 placed in your .login file. Whenever the command history is issued, the 25 most recent commands are displayed along with the numbers. How many total numbers were displayed? ______________________
What were the three most recent numbers and commands in your history list?
Number Command
______________ ____________________
______________ ____________________
______________ ____________________
Type in the following command: dir
An abbreviation may be used to reissue the most recent command; that abbreviation is !!. Issue the following command (followed by a <cr>):
!!
What was the result? ___________________________________________________________
Which command was executed? ___________________________________________________
Issue the command:
!dWhat was the result? ___________________________________________________________
You can look at the list of commands and select the first letter of any command other than a history command; such as !p if you had entered "pwd" in your list in the last 25 entries. Now issue the command:
!c
What was the result? ___________________________________________________________
Next, choose one of the commands to repeat by using the number. Type in the ! and a number of one of the items listed. What was the number and what was the result? ___________________________________________________________________________
Thus instead of retyping previous commands, you can issue previous commands by issuing one of the following
!! !<command_number> !<command_first_letter>What happens if two commands begin with the same letter such as cp and cat? The !c command would refer to the command with the larger number. To override this, one could use either !cp or !ca to obtain the most recently issued cp or cat command, respectively.
If a long command is incorrectly entered, it can be corrected without reentering the entire command. Issue the following (incorrect) command and remember the command number, you will need it shortly:
ct $home/lab5/employee
What is the message that you received when you did this? _____________________________________________________________________________
Now correct the command as follows. Issue the command:
!!:s/ct/cat
What was the result? ____________________________________________________________
The :s/<old>/<new> part substituted the new string for the old string in the event referenced by !!. Two things should have happened:
(i) ct should have been changed to cat
(ii) the corrected command should have been issued
Refer back to the number you recorded above for the ct command. Correct the command once again by issuing the command:
!<number>:s/c/ca
You cannot edit the command line to make correction in the same way you could in the Korn shell.
Read command in the C shell
In an earlier lab, you learned how to read in a name of a variable by using the read command. This allows you to enter some name into the program interactively. You made three files, the srch, prn and the chk files where you used the read statement along with some others.
In the C shell, the command is not called the "read" command, but there is a feature that will allow you to enter information from the keyboard as you did with the Bourne shell. It is:
set <varname> = $<
In the C shell, you must leave a space between the "
=" sign and any other information. In order to try this out, change to your home directory by typing in:cd
Next, change to your bin directory by typing in:
cd bin
Type in:
ll
What do you see? ___________________________________________________________________
_________________________________________________________________________________
This next section will give you practice in setting up shell scripts in the C shell. Make a file to use in the C shell that is similar to the srch file you wrote in the Bourne shell. Name this file srchc. It should contain the following:
# This is a C shell program to find files
# This next line will return me to the home directory
cd
# Read in a file name in C shell
echo "Read in file name you want to find"
set filename = $<
find . -name $filename
NOTE: C shells must begin with the # sign in the first line. You also cannot use the "\c" at the end of a line in the C shell. These newline characters do not work in the C shell. You cannot stop on the same line and read in a name like you could in the Bourne and Korn shells. You also have to use the set command in place of the read statement. The echo statement is also used instead of the print statement. You can see some of the differences in the three shells.
After you have completed this file, you must make it executable by using the chmod command. Try out the chb command that you made for the Bourne shell. Did it work to make this file executable? ____________________
Even though you are in the C shell, the chb shell script that was made in the Bourne shell will still work in the C shell. That is because of the information listed on the first line of the shell script. The #! /bin/sh tells the UNIX system to run that particular program in the Bourne shell. So the shell script will execute this program in the Bourne shell. The same would be true if you used the chk shell script. You can try it out if you want.
In the Bourne shell, you had to add the path name for the bin directory to a file called .profile. You already added a line of information in the .login file about the path name. Try out the file by typing in:
srchc
Next type in: number
What was the answer you received? _______________________________________________________
You can now make another shell script for the chmod command when you are in the C shell. You just made one for the Korn shell. Type in the following shell script called chc:
# C shell program to make a file executable
echo "Read in the file you want to make executable: "
set filename = $<
chmod u+x $filename
echo "$filename is now executable"
ls -l $filename
Save this file and make it executable. Use the chk file this time. You will be able to use the chc shell script later in this lab and the next one with the C shell scripts.
Let's test your ability to make a shell script by yourself. You made a file called the prn file in the last lab. Now you should make one in the bin directory that you can use in the C shell. Name it prnc. Develop this program yourself. Remember that you must use the # sign in the first line. Put one comment line in the program with YOUR NAME on it. You will print out this file later. The read statement is different and the echo statement is also used in the C shell.
After you have it finished debugging your new prnc, and have made it executable (using chc) , use prnc to print out the prnc file.
*********************************************************
Attach this file to your lab and turn it in with your lab.
*********************************************************
You have now learned how to write some shell scripts that can help you do things that you do frequently in a shorter amount of time. Return to your home directory by typing in:
cd
Set Command
There are several variables set up by the shell for you. You can access all of them for reading and most of them for writing. You can have a shell display a complete list of variables for you with the set command.
The set command has two forms. If used along with arguments, set can initialize variables in the C shell. If used as a command with no arguments, set will list the system and user defined variables along with their current values. In the C shell, type in:
set
What is displayed? List only the following cwd, history, home, prompt, and shell.
__________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
There are several variables listed. In the C shell, you will see a cwd which will list your current working directory. History is listed with the default number that you want to keep. There is also a home variable listing your home directory. You also have a prompt which lists the prompt that you see when you are in the C shell (which in this case is %).
You can also list the set command in the Bourne shell and the Korn shell.
If at any time you wish to change back to the Bourne shell, you can do it easily by typing in sh at the % prompt. You will then get the $ prompt again and you get out of the Bourne shell by typing in exit at the $ prompt. You will then be back in the C shell. You can get to the Bourne shell by typing in the information in bold print.
(you are now back in the Bourne shell and will stay there until you type in exit)% sh
$
At this time, return to the Bourne shell by typing in:
sh
You should now have the $ prompt again. Type in:
set
What is displayed for PWD, SHELL, PS1, PS2 ____________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
There are several different variables listed. An explanation of a few of them is included here. The variable PATH specifies the search path the shell will take when it is looking for an executable file. The colon (:) is used as a field separator between directory names. The period (.) indicates the current directory. The variable HOME specifies the path to your default home directory. The variable SHELL specifies the login shell for your account. The variables PS1 (prompt string #1) and PS2 (prompt string #2) are the primary and secondary prompts for the Bourne shell. Note that the C shell only has one.
Primary and Secondary Prompt
To cause the Bourne shell to print the secondary prompt, enter a quote that extends to a second line, i.e., put a <cr> in a quoted string. The secondary prompt appears on subsequent lines until a closing quote is encountered. The following should allow you to determine the secondary prompt. Enter the lines below with an enter at the end of each line. Make sure you are in Bourne shell before typing the following lines.
'This is line 1
and this is line 2.
Here is line 3.'
The output that you get does not make any sense! You will receive a message to the effect "Command not found". The main reason for this lesson is to find out what the secondary prompt is for the Bourne shell. What is the secondary prompt for the Bourne shell? ____________________________________
To change the Bourne shell primary prompt, set the shell variable PS1 to the new prompt. An example follows. You must use upper case letters for the PS.
PS1='}} '
Make this change. What is the primary prompt now? _______________________________________
Remember blanks cannot be on either side of the equal sign. To change the secondary prompt, set the shell variable PS2 to the new prompt. For example,
PS2=' ^^ '
Type in the example used above again:
'This is line 1
and this is line 2.
Here is line 3.'
Did both prompts change? _______________________________
Change the prompts back to their original ones. Type in:
exit
You should now be in the C shell again. Now type in:
ksh
What prompt did you receive when you changed into the Korn shell? ________________________________
Type in:
set
You can look at the information for the Korn shell by typing in ksh at the prompt. You will now be changed into the Korn shell. If you then use the set command, you will find that there are four prompts available for the Korn shell. Try it and list what the four prompts are.
PS1 ___________________ PS2 ___________________ PS3 ___________________ PS4 ___________________
Return to the C shell again by simply typing in:
exit
You already changed the C shell prompt at the beginning of this lab when you typed in:
set prompt = '! %'
If you wanted to change it again, you could use the following:
set prompt = '<p1>'
where <p1> is the C-shell prompt you would like to have in your C-shell after you log in. You can only logoff the computer from the shell that you use as your default shell--in this case it is now the C shell.
In this lab, you have used the following files:
in your home directory
.cshrc .loginIn the bin directory, you have added the following:
srchc prnc chcThere were no new files added to lab6.
Please indicate any parts of this lab that were hard to understand.
________________________________________________________________________________
________________________________________________________________________________
05/30/03