Lab 6 - Unix Lab#6 ’0...

Info iconThis preview shows pages 1–9. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Unix Lab#6 ’0 http://cs.okstate.edu/~cs235l/unix06.html Av Lab #6 CS 2351 UNIX Operating System Name Section 3ft? 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, and the chmod 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 Department’s 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 /i§inl.§: / You should get some answer such as lbin/ksh. If you did not, you should enter an instance of the Korn shell now, by typing: What response did you get to this command? ksh The Korn shell is designated by the ksh. The bin is a directory where the shells are kept. In the mid 19805, 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 1 of9 2/24/2011 12:29 PM Unix Lab#6 http://cs.okstate.edu/~csZ351/unix06.html 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. 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. 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 3 z 23% What shells were listed? 5L : it tame... Ska: -' You should see several choices. The /bin/sh, /bin/csh, /bin/bash, and /bin/ksh are the shells used mo{ frequently. The others are used less frequently. Print Command The Korn shell uses the print command to print its arguments to standard output (the terminalscreen). Its uses includeproducing 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 it"? print CS 2351 Q5 "53%; 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? (a f"? ér'fl, {9 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: 2 of9 2/24/2011 12:29 PM Umxhw#6 3of9 http://cs.okstate.edu/~csz35 1/unix06.l cd Create a new directory, lab6, by entering: mkdir 1ab6 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? mi ‘ 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: #! /bin/ksh 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 l we —1 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? cammi ’” " K: (M it may» .0 . n It is possible to look at the file characteristics with the Is command. Enter the command: ls -1 start / 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: At the left of the line which was printed are 10 character positions. List this string of 10 characters. —(w.. a _. a. —— w r = read 2/24/2011 12:29 Umeflfi6 4of9 w = write - execute permission not granted. N I 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 11 (user), g (group) and 0 (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, 0 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 -1 start Write the 10 character string displayed. ’YVJX "n *- '“"“ ”” Has it changed since the last time ? at? 31". - I e \\ ~ I; ,, HOW?—j"—Q 1A5“ {"55 M“ Mr MKWV “3 i Lirtréfiéi Vi)? I} Now type in: start What is the result this time ? ’m raga. we ‘ fi' Wt llilj rain; .3... “1 , xw“"" fig... $35.:ng ‘zJ ’:...Ua a, E Tirk we've-194.5 “ti 'W http://cs.okstate.edu/~cs235 1/unix06.html / 2/24/2011 12:29 PM Unix Lab#6 http://cs.okstate.edu/~cs235l/unix06.htm1 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. 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 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? 1237' mm 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: 5 of9 2/24/2011 12:29 PM Unix Lab#6 http://cs.okstate.edu/~c5235l/unix06.htm print $person What is displayed ? W If there are spaces in the character string, then the string must be quoted. Either single quotes C) 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 O (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: #! /bin/ksh 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 be calculator to add up the three departments so you can have a Total for this report. Type in: be scale=2 1243.95 + 1580.50 + 1970.37 What was the total that you got? ' . 5 '13- \/ Next, press ctrl-d to get out of the be and back to the $ prompt. You can now type in th information you need to complete the report. Type in: 6 of9 2/24/2011 12:29 PlV Unix Lab#6 http://cs.okstate.edu/~c5235 l/unix06.html 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: /’ #! /bin/ksh ‘ _ 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: g 1’; w. 0433 u 1‘ Z ACME Z Enter ACME2 on a line by itself. What is the result ? 41:; at Miami « 434$ a “Had” are, Enter: ACME2 July 2555 4890 2854 10299 What is the result 7 0 Mm Anti " CW: E: L. ... 1:; ,4 'nfi‘ / ix 5-5 '9 ii) ",3 Expat—yin 2555 Win 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: #5 ‘k i 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 7* 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: 7 of9 2/24/2011 12:29 PM Unix Lab#6 http://cs.okstate.edu/~c52351/unix06.html #! /bin/ksh start ACME August 2679 0980 8876 12535 ACMEZ September 1000 7098 3567 11665 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. VVhatistheresuh‘? £Rm ilia, “thai’ tqcfléé ta A C? 2 Wfim mmifi- é’ v»! fight 57f 4” s14. i":f‘\.5%/¥/fi5 V i g/ { 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: #! /bin/ksh 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 ? :25 If so, make com executable, and try to execute com again. Was there any output ? N0 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: M Use the editor to change the contents of com. Add the following to the file com: computer=" Sun Microsys tems " print $computer 8 of9 2/24/2011 12:29 PM Unix Lab#6 http://cs.okstate.edu/~c52351/unix06.html Execute com. Was the value of the variable computer changed during the execution of the shell script? ;[e '5 What is the value of the variable computer now? 5&6» Mitfwgifimi 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 n_ot affect the corresponding variable in the parent shell. At this time, you should have these files in your lab6 directory. These are: start ACME: ACMEZ combined com Type in: print * What was listed? flaw item if ’2. 525m? DO NOT REMOVE ANY FILES YOU HAVE CREATED IN THIS LAB. You will be referring to them in later labs. You should have the Lab6 directory in your HOME directory. Please indicate any parts of this lab that were hard to understand. Fa1107 9 of9 2/24/2011 12:29 PM ...
View Full Document

This note was uploaded on 01/10/2012 for the course CS 2315 taught by Professor Staff during the Fall '11 term at Oklahoma State.

Page1 / 9

Lab 6 - Unix Lab#6 ’0...

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

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