Lab 10 - Unix Lab#10 IO/w Page 1 of 9 Lab #10 CS 2351 UNIX...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Unix Lab#10 IO/w Page 1 of 9 Lab #10 CS 2351 UNIX Operating System meg/E Section 7/01 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 Boume 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. When you change to the C shell, you will have a % prompt. Issue the following command: csh 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. Be sure that you are in your home directory. Use the editor to put the following lines in a file named .login stty erase 'Ah' 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. When you make changes in the .login file, you usually have to logoff your account before they become http://cs.okstate.edu/~csZ35 l/unix l O.html 4/ 7/201 1 Unix Lab#10 Page 2 of 9 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. 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. 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 Kom 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 I more" alias cd 'cd \!*; echo $cwd' alias rm "rm —i" alias ll "ls -F l more" alias la "ls —al I more" alias h history alias f finger http://cs.okstate.edu/~cs2351/unix10.html 4/7/201 1 Unix Lab#10 Page 3 of 9 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 don t use the "=" sign. You must use the source 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. r 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 History When you first enter csh, 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 start 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 ? [,(l M06 / When you change to 13b6, 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? /0 gm; t»; Next type in : di r What is the result? Type in: Name two of the files that have an * at the end of their names: http://cs.okstate.edu/~cs235 l/unix10.html 4/7/201 1 Unix Lab#lO Page 4 of 9 Next, type in: cat news tart Next, issue the command: h .r ,lK/i’gt’a‘l» What was the result? Th6 was / ‘ 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? 2?, What were the three most recent numbers and commands in your history list? Number Command __7£—_ Zl ' l a at? in / Type in the following command: dir An abbreviation may be used to reissue the most recent command; that abbreviation is l!. Issue the following command (followed by a <cr>): \t “- ’4 l: r What was the result? "i (M Mr? a 5’- 6“ «MS 5M timing no w» l 53 Which command was executed? {E‘s ( Issue the command: 1d http://cs.okstate.edu/~cs2351/unix10.html 4/7/201 1 Unix Lab#lO Page 5 of 9 What 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: lc / What was the result? f ai um: (3 m Thus instead of retyping previous commands, you can issue previous commands by issuing one of the following ! ! !<command_nu.mber> !<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 lca 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? f/erxnp.§sfi r J Now correct the command as follows. Issue the command: / !!:s/ct/cat J V‘ffi‘g CLO”? 9W “l”? ‘ t’ s If: r! V»)? f» What was the result? 4 a The :s/<old>/<new> part substituted the new string for the old string in the event referenced by H. 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: l<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 thi C shell In an earlier lab, you learned how to read in a name of a variable by using the read command. This http://cs.okstate.edu/~cs235 l/unix10.html 4/7/201 1 Unix Lab#lO Page 6 of 9 allows you to enter some name into the program interactively. You made three files, the srch, pm 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: 11 What do ou see? all its: 3, 4:3“. .gfxirvzesnrmcg y 4% / 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: #! /bin/csh # This is a C shell program to find files # Read in a file name in C shell echo "Enter the file you want to find" set filename = $< find ~ -name $filename NOTE: C shells must begin with the #! /bin/csh 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 ebb command that you made for the Bourne shell. Did it work to make this file executable? 3' «E 5 Even though you are in the C shell, the ebb 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 #! lbin/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 http://cs.okstate.edu/~cs235 l/unixl 0.html 4/7/2011 Unix Lab#10 Page 7 of 9 out if you want. In the Boume 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? , V , //i (314,29: / £4: at}: 5” «if! f’? 'éwt“! / You can now make another shell script for the chmod command in the C shell syntax. You just made one in the Korn shell. Type in the following shell script called chc: #! /bin/csh # 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. Let's test your ability to make a shell script by yourself. You made a file called the prn file in a previous lab. Now you should make one in the bin directory that uses the C shell syntax. Name it prnc. Develop this program yourself. Remember that you must use the #! /bin/csh in the first line. Put a comment line in the program with YOUR NAME on it. 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: col 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 http://cs.okstate.edu/~cs23 5 l/unix l O.html 4/7/2011 Unix Lab#10 ‘ Page 8 of 9 their current values. In the C shell, type in: set What is displayed? List only the following cwd, history, home, prompt, and shell. /L\Oym£ homo: /r:-:.* if 7 in r 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. At this time, return to the Korn shell by typing in: exit You should now have the $ prompt again. Type in: set What is displayed for PWD, SHELL, HISTSIZE, PSl Slide 43:. AM 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 PSI (prompt string #1) and P32 (prompt string #2) are the primary and secondary prompts for the Korn shell. Note that the C shell only has one. In this lab, you have used the following files: in your home directory http://cs.okstate.edu/~cs235 l/unix10.html 4/7/201 1 Unix Lab#10 Page 9 of 9 .cshrc .login In the bin directory, you have added the following: srchc prnc chc There were no new files added to lab6. Please indicate any parts of this lab that were hard to understand. F allO7 http ://cs.okstate.edu/~cs23 51/unix10.html 4/ 7/201 1 #! /bin/csh # This program is used in the C shell to print # Steven Belcher echo "Read in the file you want to print: " set filename = $< lp —dmleSpr2 $filename ...
View Full Document

Page1 / 10

Lab 10 - Unix Lab#10 IO/w Page 1 of 9 Lab #10 CS 2351 UNIX...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online