a gentler introduction to unix - plymouth state...

47
A Gentler Introduction to Unix Zhizhang Shen * Dept. of Computer Science and Technology Plymouth State University April 30, 2014 Abstract Since this course is to run C/C++ in a Unix like environment, we give a brief introduction to Unix, based on[1], to learn a bit about the directory structure of the Unix system, some of the basic Unix commands, and how to compile and run a C program organized in either one file or multiple files. We will then proceed to learn now to write programs in C, and C++ to a lesser degree, to solve various problems. Later on, we will also look at a variety of Unix system calls and see a comprehensive example as how C is used to write programs that make use of system information as kept by Unix. Contents 1 An introduction 2 1.1 How to log into the system? ........................... 3 1.2 Simple Unix commands .............................. 3 2 Unix file system 5 2.1 Names of files and directories .......................... 5 2.2 Work with files .................................. 7 2.3 Work with directories ............................... 11 2.4 Background processing .............................. 13 2.5 Miscellaneous commands ............................. 14 * Address correspondence to Dr. Zhizhang Shen, Dept. of Computer Science and Technology, Plymouth State University, Plymouth, NH 03264, USA. E mail address: [email protected]. 1

Upload: phamxuyen

Post on 01-Feb-2018

229 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

A Gentler Introduction to Unix

Zhizhang Shen ∗

Dept. of Computer Science and Technology

Plymouth State University

April 30, 2014

Abstract

Since this course is to run C/C++ in a Unix like environment, we give a briefintroduction to Unix, based on [1], to learn a bit about the directory structure of the

Unix system, some of the basic Unix commands, and how to compile and run a Cprogram organized in either one file or multiple files.

We will then proceed to learn now to write programs in C, and C++ to a lesserdegree, to solve various problems.

Later on, we will also look at a variety of Unix system calls and see a comprehensive

example as how C is used to write programs that make use of system information askept by Unix.

Contents

1 An introduction 2

1.1 How to log into the system? . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2 Simple Unix commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Unix file system 5

2.1 Names of files and directories . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2 Work with files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.3 Work with directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.4 Background processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132.5 Miscellaneous commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

∗Address correspondence to Dr. Zhizhang Shen, Dept. of Computer Science and Technology, Plymouth

State University, Plymouth, NH 03264, USA. E mail address: [email protected].

1

Page 2: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

3 A Unix shell 15

3.1 How does a shell work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153.2 Options and arguments of commands . . . . . . . . . . . . . . . . . . . . . . 163.3 Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.4 Group commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173.5 Pipes and tees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173.6 Quoting special characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

4 The editors 19

5 C programming in Unix 19

5.1 First thing first . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195.2 Are you bored with it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195.3 Speak mathematically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205.4 Cut it into pieces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

6 A debugger 22

6.1 Set break points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246.2 How to proceed from a break point? . . . . . . . . . . . . . . . . . . . . . . . 246.3 Check out the values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266.4 A final example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

7 Shell scripts 30

7.1 Positional variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317.2 User-defined variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

8 Unix system interface 35

8.1 Look at one call in details . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368.1.1 An example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368.1.2 Another example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378.1.3 Scripts again? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

8.2 An example: Listing directories . . . . . . . . . . . . . . . . . . . . . . . . . 418.2.1 Given a standard interface... . . . . . . . . . . . . . . . . . . . . . . . 428.2.2 ... and an implementation of the interface . . . . . . . . . . . . . . . 458.2.3 A little summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

1 An introduction

Unix is a popular, and powerful operating system. It supports multitasking and time-sharing (Think about how turing is operating.). It consists of a kernel, , a file system, a shelland various utilities. As the core of the system, the kernel controls everything inside the

2

Page 3: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

computer and manages all kinds of resources, in particular, it implements various queues tosupport the multitasking ability.

With the support of the file system, Unix organizes all kinds of data into a bunch of files,categorized into directories (folders).

The Unix shell interprets user commands and passes their requests to the kernel so thatthey can be further carried out.

A utility is just a useful software that makes the system more user friendly. For example,we can print out calendar, and list all the files out in various forms.

There are various versions of Unix, such as System V Unix, BSD Unix, and Unix-likesystems, such as Linux. Turing is a Linux based system.

Since Unix is a multiuser system, we need a user account/password to get into the system,which is just the usual Turing account. You should have it. If you don’t, please let me knowas soon as possible.

1.1 How to log into the system?

We can get into Turing via Secure Shell (SSH), under All Programs/Network if you use a pro-gramming lab on campus. Since Unix is a multiuser system, we need a user account/passwordto get into the system. If you don’t have this information, please let me know as soon aspossible.

We can also remotely log into the turing system with, e.g., SSH, which can be installedin your laptop(desktop) by 1) mapping to \\owl\addon\ssh in a lab machine; 2) making acopy of SSHWinClient-3-2-0.exe; and 3) running this executable in wherever you want tokeep it.

If you do a Google on “ssh putty”, you can find and download another one, Putty.

1.2 Simple Unix commands

Once getting into the system, we can immediately try out a few simple things.

• Change your password.

> passwd

Changing password for user zshen.

Current password:

New password:

Re-enter password:

passwd: all authentication tokens updated successfully.

>

Here, by ‘>’, we mean the command line prompt, or simply prompt. It is often associatedwith a user path, which we will explain further later on. For example, in my case, theactual prompt looks like the following:

3

Page 4: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

/home/PLYMOUTH/zshen >

Question: What is the path in your case?

• What is the date today?

> date

Sun Jan 19 16:41:26 EST 2014

• Who am I?

> who am i

zshen pts/0 2014-01-19 16:32 (d-216-246-135-193.cpe.metrocast.net)

• What is it?

> man date

DATE(1) User Commands

With Unix, you can get access to the Unix manual with the man command.

• I am done.

> logout

Lab work 1.2: Unix is case-sensitive. Try the following out with turing to see whathappens. You might also want to use the man command to check if they are validate Unixcommands.

• WHO

• CAL 2004

• DATE

• WHO AM I

Collect what you do in a ‘.txt’ file and send it in as your lab report.

4

Page 5: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

2 Unix file system

There is nothing special, compared with what we have been dealing with every day with theWindows system, except perhaps a folder in Windows is referred to as a directory now.

To refresh your memory, an ordinary file can be either a text file or a binary file. A textfile is usually represented as ASCII codes; and a binary file, on the other hand, is composed asa sequence of binary bits, intended for a computer to use. A special file contains informationnecessary to work with a certain device.

Structurally speaking, a bunch of related files is organized into a directory, and a bunchof directories is organized into a parent directory, ..., until everything is collected under aroot directory. Notice that any directory is also regarded as a file. Thus, the whole thingis organized as a tree, which you should know very well after taking CS2381 Data Structurecourse and Intermediate Programming.

The very reason to use a tree structure to organize all these directories and files isthat, as you should have learned in either MA2200 Finite mathematics or MA3200 Discretemathematics, in such a structure, the path between the root directory and any other directoryand/or file is unique. This simple fact allows us to use the same name to identify differententities in such a structure, even they share the same name.

At the top level, a Unix file tree contains the following directories: bin, containingthe software for the shell and most of other commands; dev, containing all the specialfiles associated with various devices; etc, containing various administrative files, such asthe list of the authorized users; home, containing the home directories of various users;tmp, containing you-know-what; usr, containing something useful; and var, containingsomething that varies, e.g., users’ mailboxes.

When just logging into the system, you are in your working directory. For example,when I log into the system, I am in my working directory /home/PLYMOUTH/zshen. Youcan always find out your current working directory by using the print working directorycommand pwd. Once in, you can change to any directory you want to work with, by usingthe directory change command, cd. We will explain how to change to the desired directoryafter understanding how to identify the location of such directories in §2.1.

2.1 Names of files and directories

Every file and directory is identified by a name. On most of the systems, a name containsbetween 1 and 255 characters out of the following: upper case letters, lowercase letters,digits, period, underscore and comma. You should avoid some of the special symbols suchas ‘&’, ‘*’, ‘́, various brackets, ‘%’, etc. when coming up with a name, and you should notuse various reserved words as the names of your files.

To refer to a file located in your current directory, you only need to use its name. But,if you want to use a file located in another directory, you have to use its pathname, whichuniquely represents its location in the whole file structure.

For example, the root directory is ‘/’, and the absolute pathnames for the root’s child

5

Page 6: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

directories, as shown in the following figure is ‘/bin’, ‘/dev’, etc..

Figure 1: An example of a Unix tree

In general, to get the absolute pathname of a vertex in a tree structure, we start withthe name of the root vertex, represented with ‘/’, and collect the names of those verticesalong the unique path from the root to that vertex, until we get to that vertex. Notice thatwe also use ‘/’ to separate the names of all these vertices in a path name.

For example, as shown in Figure 1, the directory ‘home’ contains two sub-directories: jackand jill. Their absolute path names are ‘/home/jack’ and ‘/home/jill’, respectively.Similarly, the absolute pathname for kangaroo is ‘/home/jill/Marsupials/kangaroo’.

Although the absolute path name uniquely identifies a file, and allows us to give the samename to different files, it is certainly an overkill. Moreover, we sometimes don’t have accessrights to some of these directories, especially, the root directory. Thus, we often use relativepathname based on the key property of a tree: between any two vertices, there is a uniquepath.

For example, if we are in the directory jack, and want to refer to the directory Marsupials,we 1) move up to the parent of jack, as represented with ..; 2) move down to jill; and3) move further down to Marsupials. We also have to use ‘/’ to separate all these threenames to get “../jill/Marsupials”.

We can also use ‘.’ to refer to the current working directory, as in ‘./Oceans’, assumingthe current working directory is still “jack”. But we prefer to use “Oceans” as it saves ustwo key strokes.

Lab work 2.1:

1. Which of the following are valid names for ordinary Unix files, and why?

foo guess? book.chap1 BOOK.chap2 2good2Btrue {2bad} rank* serial#

6

Page 7: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

2. Which of the following are valid names for directories, and why?

Dir2 Directory.3 *Hook |Line1| "Sinker" money.$ .hideNseek 777

3. In Figure 1, what are the absolute path names for root, bin, jill, and kangaroo?

4. Again, in Figure 1, suppose that Marsupials is now the working directory, what arethe relative pathnames of root, bin, jill, and kangaroo?

5. In Figure 1, the directory jack has two subdirectories, Continents and Oceans, whatare their absolute pathnames?

Collect what you do in a ‘.txt’ file and send it in as your lab report.

2.2 Work with files

Once a directory is identified, we can list all the files included in that directory using the ls

command.

• List all the files in the current directory 1

> ls

Application Data CSDI1300 mbox TestShell

commands download OSSamples TestSimulator

Cookies ExamplesForCS316 Program Files Thumbs.db

Courses History temp unixExamples

cppsamples Home Test it For Andy.ppt USER.DAT

csamples Mail TestPipes UWFont

>

• A hidden file, i.e., a file with its name starting with a ‘.’, will not be listed, unless weuse the list all command.

> ls -a

. Cookies History temp

.. Courses Home Test it For Andy.ppt

.alias cppsamples .login TestPipes

Application Data csamples .login-old TestShell

.aspell.en.prepl CSDI1300 .mail TestSimulator

.aspell.en.pws .cshrc Mail Thumbs.db

.bash_history download mbox unixExamples

.bash_profile .elm .mysql_history USER.DAT

1What you will get is certainly different from mine.

7

Page 8: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

.bash.profile .emacs.d OSSamples UWFont

.bashrc ExamplesForCS316 .profile .viminfo

commands .history Program Files

>

• Once the files are listed, we can have a look at the content of a text file by using more:

> more first.c

#include <stdio.h>

main(){

printf("I feel lucky today

\n");

}

• Once the content is displayed, we can also print out a hardcopy of a text file by usinglpr:

> lpr -P prlaser first.c

Besides prlaser, located in the programming lab, you can also use syslaser, memlj,

mem2lj, or advlaser.

• If often happens that we have to change the name of a file, or even move a file toanother place, which is simply done by changing its absolute path name with the movecommand ‘mv’.

> ls

BACKUP countResultq fileListAll picoExamples shellExamples

catfile emacsExamples fileSystemExamples recip shellScripts

cExamples envVariables linkToresult.txt result.txt viExamples

countResult fileList multimedia setdate

> more result.txt

a write to stdout

before fork

pid = 52863, globalVar = 7, localVar = 89

before fork

pid = 52862, globalVar = 6, localVar = 88

> mv result.txt output.txt

> ls

BACKUP countResultq fileListAll output.txt shellExamples

catfile emacsExamples fileSystemExamples picoExamples shellScripts

cExamples envVariables linkToresult.txt recip viExamples

countResult fileList multimedia setdate

>

8

Page 9: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

You can also move a file somewhere else by changing its absolute path name. Heremkdir creates a directory, which we will discuss in § 2.3.

> mkdir sampleDir

> ls sampleDir

> mv output.txt sampleDir

> ls sampleDir

output.txt

>

Use ‘man mv’ to check out the other formats, option, etc..

• To delete a file, you use rm as follows:

> ls> sampleFile

> ls

a.out csamples Mail TestPipes

Application Data CSDI1300 mbox TestShell

commands download OSSamples TestSimulator

Cookies ExamplesForCS316 Program Files Thumbs.db

Courses fake sampleFile unixExamples

cppsamples History temp USER.DAT

CS2470 Home Test it For Andy.ppt UWFont

> rm sampleFile

> ls

a.out csamples Mail TestShell

Application Data CSDI1300 mbox TestSimulator

commands download OSSamples Thumbs.db

Cookies ExamplesForCS316 Program Files unixExamples

Courses fake temp USER.DAT

cppsamples History Test it For Andy.ppt UWFont

CS2470 Home TestPipes

• There are four ways to create a file: You can either 1) copy an existing file; 2) redirectthe “standard input” into a file; 3) use a text editor; or 4) use a computer program togenerate a file.

– To copy a file, you use the cp command.

> ls

output.txt

> cp output.txt anotherOutput.txt

> ls

anotherOutput.txt output.txt

>

9

Page 10: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

– To redirect the output of the standard input, you can use the ‘>’, as follows :

> ls > sampleRedirect

> ls

anotherOutput.txt output.txt sampleRedirect

> more sampleRedirect

anotherOutput.txt

output.txt

sampleRedirect

>

If you don’t have any files to play with, this might be the easiest way to createsome files for now.

– If you want to redirect the output of the standard input and append it to anexisting file, you can use the ‘>> ’ operator.

– We will talk about how to use an editor to create a text file in § 4; and now tocreate a file with a program in Section 8.

We will use these redirection operations rather heavily when we do C programming.

• The ‘l’ options shows everything about the items in a directory.

> ls -l

total 12

-rw-r--r-- 1 zshen faculty 126 Dec 19 09:06 anotherOutput.txt

-rw-rw-rw- 2 zshen faculty 126 Dec 31 1969 output.txt

-rw-r--r-- 1 zshen faculty 44 Dec 19 09:11 sampleRedirect

>

• Look at the long listing, you might be wondering what does “rw-r--r--” mean. Theydeal with the access privilege of a file or a directory. They are organized into threegroups: owner, group and public. For each group, it tells its access privileges regardingwhether it can be read, written or executed. Thus, “rw-r--r--” means that the ownercan read and update, but cannot execute, this file; all the members of his/her group,as well as the public, can only read this file.

• Only the owner of a file, or a directory, can change such privileges. For example,

> more output.txt

a write to stdout

before fork

pid = 52863, globalVar = 7, localVar = 89

before fork

10

Page 11: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

pid = 52862, globalVar = 6, localVar = 88

> chmod u-r output.txt

> more output.txt

output.txt: Permission denied

>

Besides u for owner, a for all, you can also use o for others (public) and g for public.Moreover, besides =, for assignment, and - for remove, you can also use + for add apermission.

The following gives everyone the read access privilege to the file.

> chmod a=r output.txt

> ls -l output.txt

-r--r--r-- 1 zshen domain^users 127 Jan 19 17:49 output.txt

/home/PLYMOUTH/zshen >

>

2.3 Work with directories

It is quite common to develop a tree like directory structure to manage the files more effi-ciently.

• As aforementioned, to find out the current working directory, you use pwd.

• If you forget where you are, you can use pwd to find it out.

> pwd

/sampleDir

> ls

input lsOut output output.txt sampleRedirect

• To create another directory within the current directory, you use mkdir.

> mkdir anotherDir

> ls

anotherDir input lsOut output output.txt sampleRedirect

• Change the working directory to this newly created directory

> cd anotherDir

> ls

11

Page 12: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

• Come back to the previous working directory

> cd ..

> ls

anotherDir input lsOut output output.txt sampleRedirect

• To take it out, use rmdir

> rmdir anotherDir

> ls

input lsOut output output.txt sampleRedirect

>

Lab work 2.3:

1. Create the directory structure as shown in Figure 1, notice that kangaroo is a file, butnot a directory.

2. Assume that we will set up some additional subdirectories for jack as follows: Continentsnow holds Africa, Antarctica, Asia, Austria, Europe, NAmerica, and SAmerica;

and each of these directories holds some countries and regions of their own.

(a) Use Unix command to set up the above structure.

(b) Give the absolute path names for the following countries: Norway, India, Egyptand Argentina

(c) Assume that your current working directory is USA, accomplish the followingusing a single command line with relative path names:

i. List the content of Marsupials.

ii. List the content of Australia.

iii. Make a copy of the kangaroo file and place this file, also named kangaroo,

in the Australia directory under Jack/Continents.

(d) Repeat the above using absolute path names.

3. The echo command takes a line of input that you type in through the keyboard andkicks it right back on the screen. Try it out

> echo try this out.

try this out.

Create a file fun by redirecting the output with the echo command, with the help ofthe redirection mechanism.

12

Page 13: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

4. Use the commands who, who am i, and date to append more data to the fun file.

5. Come up with a file stuff and place it in your working directory. Then do the following:

(a) Give everyone permission to read stuff, but do not change any other accessprivilege.

(b) Permit the owner and group members to read and write the file; remove theseprivileges from everyone else.

(c) Give the owner and group members permission to execute stuff while giving theowner sole permission to read or write it.

6. A hidden file has a name starting with ‘.’. Use the cal utility and the redirectionoperator ‘>’ to create a .hidden file. How to list this file?

7. Create a new directory Misc and move the file fun into Misc.

8. Without leaving your current directory, create a directory Vacations under Misc.

Then create calendars for the summer months of 2011 and move them into the Vacationsdirectory.

9. Redo Lab work 2.1.1 and 2.1.2 by testing them out, using appropriate commands. Tellme what happens when you try to create, e.g., {2bad}, etc..

Collect what you have done in a ‘.txt’ file and send it in as your lab report.

2.4 Background processing

As we mentioned earlier, Unix is a powerful OS that supports multitasking in terms ofmultiple processes identified with PID. Since there is only one processor in the machine, atany time, one process is running, while the others are simply waiting. For more details, youmight consider taking the OS course.

The command ps will show all the process id that have been running. The commandjobs will do the same.

> ps

PID TTY TIME CMD

23729 pts/3 00:00:00 bash

23787 pts/3 00:00:00 less

24262 pts/3 00:00:00 man

24265 pts/3 00:00:00 sh

24266 pts/3 00:00:00 sh

24270 pts/3 00:00:00 nroff

24271 pts/3 00:00:00 less

24279 pts/3 00:00:00 iconv

24425 pts/3 00:00:00 ps

13

Page 14: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

We can use the kill command to terminate a process, stop to suspend a process, bgbrings it to the background, and fg to bring a process to the foreground. The ‘&’ at the endof a line puts it into the background.

> (sleep 60; echo stop this command) &

[3] 24514

> ps

PID TTY TIME CMD

23729 pts/3 00:00:00 bash

23787 pts/3 00:00:00 less

24262 pts/3 00:00:00 man

24265 pts/3 00:00:00 sh

24266 pts/3 00:00:00 sh

24270 pts/3 00:00:00 nroff

24271 pts/3 00:00:00 less

24279 pts/3 00:00:00 iconv

24514 pts/3 00:00:00 bash

24515 pts/3 00:00:00 sleep

24516 pts/3 00:00:00 ps

> kill 24515

> -bash: line 18: 24515 Terminated sleep 60

stop this command

2.5 Miscellaneous commands

There are also quite a few other commands, such as comm, crypt, cut, diff, etc.. Tocheck them out for details, you can use man for manual to find out their respective syntaxand semantics. For example

> man grep

GREP(1) GREP(1)

NAME

grep, egrep, fgrep - print lines matching a pattern

SYNOPSIS

grep [options] PATTERN [FILE...]

grep [options] [-e PATTERN | -f FILE] [FILE...]

DESCRIPTION

Grep searches the named input FILEs (or standard input if no files are

named, or the file name - is given) for lines containing a match to

14

Page 15: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

the given PATTERN. By default, grep prints the matching lines.

In addition, two variant programs egrep and fgrep are available.

Egrep is the same as grep -E. Fgrep is the same as grep -F.

OPTIONS

-A NUM, --after-context=NUM

Print NUM lines of trailing context after matching lines.

Places a line containing -- between contiguous groups of

matches.

thus, one way to use this command is the following:

> grep printf first.c

printf("I feel lucky today

So, this utility just finds out a line in a file first.c that contains the pattern “printf”. Wewill see how the utility does it later on.

3 A Unix shell

As we mentioned earlier that a Unix shell interprets and executes user commands.

3.1 How does a shell work?

The general process looks like the following:

1. The shell displays a prompt symbol such as ‘> ’.

>

2. You type in a command such as cal.

> cal

3. The shell interprets your command by looking for an appropriate software. If it can’tfind it, it will print out an error message.

> wrong

-bash: wrong: command not found

4. If it finds it, the kernel runs the requested software to give you the output.

15

Page 16: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

> cal 2 2013

February 2013

Su Mo Tu We Th Fr Sa

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28

5. The shell then prints another prompt and waits.

>

3.2 Options and arguments of commands

For the cal command, you can specify which month and year you want. Here both themonth and the year are the arguments of this command.

Sometimes, we have options for a command. For example, we have -a, -F, -l, -r,

-s, -t, and -u for the listing command. For example, the -F option flags the files byshowing a ‘/’ after each directory and a ‘*’ after each executable file.

> ls -F

BACKUP/ countResultq fileListAll picoExamples/ shellExamples/

catfile emacsExamples/ fileSystemExamples/ recip* shellScripts/

cExamples/ envVariables linkToresult.txt sampleDir/

Lab work 3.2:

1. Try out all the above options for the ls command.

3.3 Redirection

We already talked about this stuff earlier, e.g.,

> more input

a write to stdout

before fork

pid = 52863, globalVar = 7, localVar = 89

before fork

pid = 52862, globalVar = 6, localVar = 88

> wc < input > output

> more output

5 26 126

16

Page 17: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

Here wc is another utility that “print the number of newlines, words, and bytes in files”.

Lab work 3.3:

1. check out the wc utility and use it to check out the number of characters in the calendarsfor all the summer months put together.

3.4 Group commands

Besides typing one command at a time, we can also type in several at a time and place themin the same line. For example,

> who am i; ls; cal 2 2013

zshen pts/3 2014-02-03 14:28 (cszs05.plymouth.edu)

brent1 cathie2 chrisC2 fowls scott1 testWord

brent2 chrisC1 fakeFile jeffW2 scott2

February 2013

Su Mo Tu We Th Fr Sa

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28

3.5 Pipes and tees

Pipe (‘|’) sends the output of one command as the input of another command. For example,

> ls | more

input

output

output.txt

sampleRedirect

Tee, on the other hand, does two things at the same time, besides passing the output ofone command to another as input, it also saves it in a file.

> ls |tee lsOut |more

input

output

output.txt

sampleRedirect

> ls

input lsOut output output.txt sampleRedirect

17

Page 18: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

> more lsOut

input

output

output.txt

sampleRedirect

Lab work 3.5:

1. View the calendars for 2013 and 2014, and also save them in a separate file.

2. Show the content of these just saved files.

3.6 Quoting special characters

A file name should not contain any of the special characters that Unix uses for some specificpurpose. But, when we have a need to use any of special characters, we have to tell thesystem using the escape character ‘\’. For example,

> echo *

input lsOut output output.txt sampleRedirect

> echo \*

*

We also use backquotes ‘‘’, the key above the tab key in the keyboard, to indicate acommand that we want the shell to run. For example

> echo It is now date

It is now date

> echo it is now ‘date‘

it is now Mon Feb 3 14:28:07 EST 2014

Lab work 3.6:

1. What does each of the following do, and why?

echo *

echo /*

echo \*

echo "\*"

echo

echo */*

2. Noticing the usage of " in the above problem, investigate what does " do to the specialmeaning of the reserved characters.

3. Investigate the wild cards such as ‘*’, ‘?’, and use cat to show the contents of allthe files with their names ending in ing; and how to list all the files with their namescontaining either ‘x’ or ‘X’.

18

Page 19: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

4 The editors

Besides copying a file from Windows to turing, we can also use some of the built-in editorsdirectly in Unix. There are a few editors that we can use to enter a program, such as vi,

pico and emacs. We once all used pico to edit emails. It is pretty easy to use.

> pico newFile

Try it out yourself.A very popular one is called vi, which is also always available with Unix. The negative

side is that it is not as easy. Check out the online vi manual.

Lab work 4: Make sure that you know at least one of the two editors reasonably well,before proceeding to the next unit.

5 C programming in Unix

5.1 First thing first

It is pretty easy to compile a c program in Unix, using the cc, or gcc, facility. For example,

> more hello.c

/* the first program in c */

#include <stdio.h>

int main(void){

printf("Hello, world\n");

return 0;

}

> gcc hello.c

> ./a.out

Hello, world

5.2 Are you bored with it?

You don’t need to always call the executable a.out, for example

> gcc hello.c -o hello

> ./hello

I feel lucky today

Here is another example which calculates the reciprocal of an integer.

19

Page 20: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

> more recip.c

/* Compute reciprocals */

#include <stdio.h>

int main(void){

int n;

float recip;

printf("This is a program computes reciprocals.\n");

printf("Enter a number: ");

scanf("%d", &n);

recip=1/(float) n;

printf("The reciprocal of %d is %f.\n", n, recip);

return 0;

}

> gcc recip.c -o recip

> ./recip

This is a program computes reciprocals.

Enter a number: 34

The reciprocal of 34 is 0.029412.

5.3 Speak mathematically

When using a mathematical function, we have to add on the -lm option to link to themathematics library. For example,

> more sqroot.c

/* Compute square roots */

#include <stdio.h>

#include <math.h>

int main(void){

float n, x, root;

printf("This program computes square root.\n");

printf("Enter a number: ");

//printf("%3.1f", sqrt(32));

scanf("%f", &n);

x=n;

printf("The number you entered is %f: ", x);

printf("The square root of %f is %f.\n", n, sqrt(n));

20

Page 21: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

return 0;

}

> gcc sqroot.c -lm

> ./a.out

This program computes square root.

Enter a number: 34

The number you entered is 34.000000: The square root of 34.000000 is 5.830952.

5.4 Cut it into pieces

We can also work with a program consisting of multiple files. For example,

> more main.c

/* Illustrate multiple source files */

void chicago(void);

void indiana(void);

int main(void){

chicago();

indiana();

chicago();

return 0;

}

> more chicago.c

/* The chicago() function */

#include <stdio.h>

void chicago(){

printf("\nI’m waiting at O’Hare International,\n ");

printf("Airport, a fun place");

}

> more indiana.c

/* The indiana() function */

#include <stdio.h>

void indianapolis(void);

void indiana(void){

printf("Back home again, Indiana.\n");

indianapolis();

21

Page 22: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

printf("Wander Indiana--come back soon!\n ");

}

> more indy.c

/* The indianapolis() function */

#define POP2000 1.6

#include <stdio.h>

void indianapolis(void){

printf("\nWelcome to Indianapolis, Indiana.\n");

printf("Population: %f million.\n", POP2000);

}

> cc -c main.c

> cc -c chicago.c

> cc -c indiana.c

> cc -c indy.c

> cc *.o

> ./a.out

I’m waiting at O’Hare International,

Airport, a fun placeBack home again, Indiana.

Welcome to Indianapolis, Indiana.

Population: 1.600000 million.

Wander Indiana--come back soon!

I’m waiting at O’Hare International,

Airport, a fun place.

Lab work 5: Test out all the program samples as shown in this section.

6 A debugger

A debugger is perhaps one of the most important tools for a system programmer, similarto a Java programmer. It allows us to check out various snapshots of the execution of aprogram, namely, values of various variables. It allows us to determine whether a particularline in a program will be the next one to be executed. We can also use it to execute one lineat a time, instead of letting the whole thing go through in a single shot. We can also use itto observe how a program behaves with a collection of choice points.

We will now check out this many possibilities through a particular debugger gdb thataccompanies gcc. Let’s use the following code as a running example.

22

Page 23: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

#include <stdio.h>

main(){

int i, sum;

sum=0;

for(i=0; i<10; i++)

if(i < 5)

sum=sum+1;

else

sum=sum+((i-3)/2+(i/3)); //line 11

printf("sum=%d\n", sum);

}

We can certainly run it as follows.

/home/PLYMOUTH/zshen > gcc sum.c

/home/PLYMOUTH/zshen > ./a.out

sum=24

Let’s send it through a debugger:

/home/PLYMOUTH/zshen > gcc -g sum.c

/home/PLYMOUTH/zshen > gdb a.out

GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6)

Copyright (C) 2010 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law. Type "show copying"

and "show warranty" for details.

This GDB was configured as "x86_64-redhat-linux-gnu".

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>...

Reading symbols from /home/PLYMOUTH/zshen/a.out...done.

(gdb) quit

/home/PLYMOUTH/zshen >

In the above, to facilitate the debugger, we use the “-g” switch to come up with addi-tional information, such as a symbol table, during the compilation process. Moreover, whenthis switch is used, no optimization is done when compiling the codes, which makes it lesscomplicated to do the debugging.

After compiling, we run the debugger on the executable a.out, which simply loads theexecutable into the debugger environment. We are now ready to execute various debugger

23

Page 24: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

commands such as run, which just executes the executable as it would be done at the OSprompt. Once it is done, it comes back to the debugger prompt

(gdb)

6.1 Set break points

It is often useful to execute a program partially, i.e., through part of a program, which canbe done by setting a break point. It tells the debugger to run the program until that breakpoint is reached, when the program just paused. We can then do other things to the pausedprogram. For example,

(gdb) break 11

Breakpoint 1 at 0x4004bc: file sum.c, line 11.

(gdb) run

Starting program: /users/facstaff/S/zshen/Courses/CS2470/programs/Debugger/a.out

Breakpoint 1, main () at sum.c:11

11 sum=sum+((i-3)/2+(i/3));

(gdb)

Question: What should be value of i at this point?Answer: 5. Since when i is less than 5, it will go through the then part.

Question: How do we find it out?Answer: We can certainly check this out using another debugger command display:

(gdb) display i

1: i = 5

(gdb)

We can also check out where the program is paused.

(gdb) where

#0 main () at sum.c:11

6.2 How to proceed from a break point?

There are three ways: step, next and continue.

1. The step command executes the next line and then pause again.

Question: What is the next line at this point in the logic flow?

Answer: Since line 11 is the last line of the loop body, the control has to go back tothe for line, i.e., line 7.

24

Page 25: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

Question: How do we find it out?

Answer: Use step.

(gdb) step

7 for(i=0; i<10; i++)

1: i = 5

Question: Then what?

(gdb) step

8 if(i < 5)

1: i = 6

(gdb)

The first step executes line 11, and brings the control to the loop at line 7, when thevalue of i is still 5. The next step does the increment of i and brings the control toline 8, where the value of i turns into 6.

2. The next command does the same, except if the next line is a function call, it willcomplete everything that call does.

(gdb) next

Breakpoint 1, main () at sum.c:11

11 sum=sum+((i-3)/2+(i/3));

1: i = 6

(gdb) next

7 for(i=0; i<10; i++)

1: i = 6

(gdb) next

8 if(i < 5)

1: i = 7

The next is more preferable over step.

3. The command continue allows the execution continue until either a breakpoint isreached, the program exits normally, or it tries to do something illegal.

(gdb) continue

Continuing.

Breakpoint 1, main () at sum.c:11

11 sum=sum+((i-3)/2+(i/3));

1: i = 7

25

Page 26: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

6.3 Check out the values

There are two ways to observe the values of a variable: print and display.

1. The print command is a one-time request to see the value. The debugger shows itscurrent value then forgets about it.

(gdb) print i

$1 = 7

2. As we have already seen, the display command shows it on an ongoing basis: Thedebugger will shows its value every time the program is paused.

6.4 A final example

The program sum.c generates different values of sum for different value of i. We might wantto see the big picture as what specific values of sum will be generated for waht specific valuesof i.

What we could do is to set break points at lines 8 and 9, and print out values of sum andi in that context.

/home/PLYMOUTH/zshen > gdb a.out

GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)

Copyright (C) 2010 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law. Type "show copying"

and "show warranty" for details.

This GDB was configured as "x86_64-redhat-linux-gnu".

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>...

Reading symbols from /home/PLYMOUTH/zshen/Courses/CS2470/programs/unixExamples/debug6/a.o

(gdb) break 8

Breakpoint 1 at 0x4004dd: file sum.c, line 8.

(gdb) break 9

Breakpoint 2 at 0x4004e3: file sum.c, line 9.

(gdb) run

Starting program: /home/PLYMOUTH/zshen/Courses/CS2470/programs/unixExamples/debug6/a.out

Breakpoint 1, main () at sum.c:8

8 if(i < 5)

Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64

(gdb) display i

26

Page 27: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

1: i = 0

(gdb) step

Breakpoint 2, main () at sum.c:9

9 sum=sum+1;

1: i = 0

(gdb) display sum

2: sum = 0

(gdb) continue

Continuing.

Breakpoint 1, main () at sum.c:8

8 if(i < 5)

2: sum = 1

1: i = 1

(gdb)

Continuing.

Breakpoint 2, main () at sum.c:9

9 sum=sum+1;

2: sum = 1

1: i = 1

(gdb)

Continuing.

Breakpoint 1, main () at sum.c:8

8 if(i < 5)

2: sum = 2

1: i = 2

(gdb)

Continuing.

Breakpoint 2, main () at sum.c:9

9 sum=sum+1;

2: sum = 2

1: i = 2

(gdb)

Continuing.

Breakpoint 1, main () at sum.c:8

8 if(i < 5)

2: sum = 3

27

Page 28: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

1: i = 3

(gdb)

Continuing.

Breakpoint 2, main () at sum.c:9

9 sum=sum+1;

2: sum = 3

1: i = 3

(gdb)

Continuing.

Breakpoint 1, main () at sum.c:8

8 if(i < 5)

2: sum = 4

1: i = 4

(gdb)

Continuing.

Breakpoint 2, main () at sum.c:9

9 sum=sum+1;

2: sum = 4

1: i = 4

(gdb)

Continuing.

Breakpoint 1, main () at sum.c:8

8 if(i < 5)

2: sum = 5

1: i = 5

(gdb)

Continuing.

Breakpoint 1, main () at sum.c:8

8 if(i < 5)

2: sum = 7

1: i = 6

(gdb)

Continuing.

Breakpoint 1, main () at sum.c:8

8 if(i < 5)

2: sum = 10

28

Page 29: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

1: i = 7

(gdb)

Continuing.

Breakpoint 1, main () at sum.c:8

8 if(i < 5)

2: sum = 14

1: i = 8

(gdb)

Continuing.

Breakpoint 1, main () at sum.c:8

8 if(i < 5)

2: sum = 18

1: i = 9

(gdb)

Continuing.

sum=24

Program exited with code 07.

(gdb) quit

/home/PLYMOUTH/zshen > y

-bash: y: command not found

/home/PLYMOUTH/zshen >

The order of the break points does not matter, the debugger will pause whenever a breakpoint is reached.

Break points can be removed using the clear command.

(gdb) clear 9

Deleted breakpoint 2

(gdb) continue

Continuing.

As we just saw, once we are done, we simply quit.

(gdb) quit

A debugging session is active.

Inferior 1 [process 18704] will be killed.

Quit anyway? (y or n) y

/home/PLYMOUTH/zshen >

29

Page 30: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

This debugger also works with g++, i.e., the c++ stuff.

Lab work 6

1. Play with the debugger with the sum.c program, and familiarize with its features.

2. Find a non-trivial program that you did in the past. Identify one or more aspects thatyou want to identify, particularly, a malfunctioning feature of your program. Then usevarious features of this gdb debugger to identify the bugs.

7 Shell scripts

A very powerful feature of Unix is that we can group together various Unix commands tocome up with a script, or a little program, that will execute. For example, the followingscript, simpleShell, containing the following lines:

/home/PLYMOUTH/zshen > more simpleShell

#A simple shell script

cal

date

who

Here ‘#’ indicates a comment.We can then run the program by using a redirection:

/home/PLYMOUTH/zshen > sh < simpleShell

April 2014

Su Mo Tu We Th Fr Sa

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30

Sat Apr 19 09:58:50 EDT 2014

zshen pts/0 2014-04-19 09:26 (d-216-246-135-193.cpe.metrocast.net)

We can also make this file executable by changing its access privilege:

/home/PLYMOUTH/zshen > chmod u+x simpleShell

/home/PLYMOUTH/zshen > ./simpleShell

April 2014

Su Mo Tu We Th Fr Sa

1 2 3 4 5

30

Page 31: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30

Sat Apr 19 10:00:10 EDT 2014

zshen pts/0 2014-04-19 09:26 (d-216-246-135-193.cpe.metrocast.net)

/home/PLYMOUTH/zshen >

In this sense, shell becomes a system programming language, where the control structuresare pretty much the same stuff; but the variables are bit different. Besides the user-definedvariables, there are two other: environment variables and positional variables.

Environment variables are used to keep values for special shell variables such as HOME,

TERM, referring to your home directory, the type of the terminal you use, respectively. Theyset the scene for everything, but we will not touch on them too much here.

7.1 Positional variables

Positional variables are used to capture the command-line arguments used by the shell script,numbered 0, 1, 2, ..., 9. Similar to the argv[] usage, $0 keeps the name, S1 the firstargument, etc.. For example, the following is the echo.args script.

/home/PLYMOUTH/zshen > more echo.args

#!/bin/sh

#Illustrate the use of positional variables

echo $0 $1 $2 $3 $4 $5 $6 $7 $8 $9

Notice that the very first line

#!/bin/sh

specifies that we will always use a particular shell, sh, to run the scripts.Thus,

/home/PLYMOUTH/zshen > ./echo.args I love Spring!

./echo.args I love Spring!

Positional variables $1 through $9 are also collected as $*. Their number is denoted as$#. For example,

/home/PLYMOUTH/zshen > more echoRead.args

#!/bin/sh

#Illustrate the use of positional parameters, used defined variables and

#the read command.

echo ’What is your name?’

31

Page 32: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

read name

echo "Well, $name, you typed $# arguments:"

echo $0 $*

Thus,

/home/PLYMOUTH/zshen > ./echoRead.args This is great!

What is your name?

Zhizhang

Well, Zhizhang, you typed 3 arguments:

./echoRead.args This is great!

You can also set up those variables using the set command, which is very useful. Forexample,

/home/PLYMOUTH/zshen > more dailyReminder

#!/bin/sh

# A daily reminder service

set ‘date‘

echo "Remember for today:"

case $1 in

Mon) echo "Plan the week.";;

Tue) echo "Take clothes to the cleaners.";;

Wed) echo "Attend group meeting.";;

Thu) echo "Make plans for the weekend.";

echo "Pick up clothes at the cleaners.";;

Fri) echo "Answer e-mail";;

Sat) echo "You should not be here working.";

echo "Finish your work and log off.";;

Sun) echo "Call Grandma and Grandpa.";;

esac

/home/PLYMOUTH/zshen > date

Sat Apr 19 10:01:07 EDT 2014

/home/PLYMOUTH/zshen > ./dailyReminder

Remember for today:

You should not be here working.

Finish your work and log off.

/home/PLYMOUTH/zshen >

Just one more example, setdate,

/home/PLYMOUTH/zshen > more setDate

#!/bin/sh

32

Page 33: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

#Demonstrate the set command

set ‘date‘

echo "Time: $4 $5"

echo "Day: $1"

echo "Date: $3 $2 $6"

/home/PLYMOUTH/zshen > chmod u+x setDate

/home/PLYMOUTH/zshen > date

Sat Apr 19 10:03:00 EDT 2014

/home/PLYMOUTH/zshen > ./setDate

Time: 10:03:13 EDT

Day: Sat

Date: 19 Apr 2014

/home/PLYMOUTH/zshen >

Question: What is going on?Answer: The backquoted ‘date‘ run the date command, which sends back the following

Sat Apr 19 10:01:07 EDT 2014

as values of positional parameters: $1, $2, $3, $4, $5 and $6.

Lab work 7.1:

1. Type up the above scripts and play with them.

2. Write a script to come up a daily reminder, e.g., for the week days,

(a) If it is about 6 a.m., get up

(b) If it is about 7 a.m., have breakfast

(c) If it is about 8 a.m.

If it is either Saturday or Sunday, though, do something appropriate for the weekend.

Note: For the “about” part, you might put in a difference, say, “ about 6 a.m.” is thesegment between 5:55 and 6:05 in the morning. You might also use a wild card, e.g.,“05:5*” stands for a period between 5:50 through 5:59. Do some research to find outmore relevant expressions.

7.2 User-defined variables

The following mySpell script provides a speller, with a file name as it sole argument, whichis passed to a user-defined variable, file. Notice how it is declared, and later used.

33

Page 34: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

/home/PLYMOUTH/zshen > more mySpell

#!/bin/sh

# An improved spelling checker

file=$1

for word in ‘spell $file‘

do

# grep tries to find the line where $word occurs in $file

line=‘grep -n $word $file‘

#print out a line

echo " "

#print out the following line

echo "Missplled word: $word"

#print out the line where the misspelled word occurs

echo "$line"

done

/home/PLYMOUTH/zshen > more testWord

this is intendly spelleed wrong

Let’s put in another wroong word

How about annother one?

/home/PLYMOUTH/zshen > ./mySpell < testWord

Missplled word: annother

Missplled word: intendly

Missplled word: spelleed

Missplled word: wroong

Please don’t try the following delFile, if you don’t want to accept the consequence.

/home/PLYMOUTH/zshen > more delFile

#!/bin/sh

#Delete a file interactively

filename=$1

if test ! -f $filename

then

echo "There is no file \"$filename\"."

34

Page 35: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

else

echo "Do you want to delete \"$filename\"?"

read choice

if test $choice = y

then

rm $filename

echo \"$filename\" deleted

else

echo \"$filename\" not deleted.

fi

fi

/home/PLYMOUTH/zshen > ./delFile fakedir

There is no file "fakedir".

/home/PLYMOUTH/zshen > ls testFile

testFile

/home/PLYMOUTH/zshen > ./delFile testFile

Do you want to delete "testFile"?

y

"testFile" deleted

/home/PLYMOUTH/zshen > ls testFile

ls: cannot access testFile: No such file or directory

/home/PLYMOUTH/zshen >

Lab work 7.2:

1. Modify the mySpell script so that if you fail to enter the file name, it will print areminder, e.g., “Usage: mySpell filename”.

2. Modify the delFile script so that it will test if the one to be deleted is a directory, inthis case, it should call rmdir instead of rm to delete it.

Notice that the following line tests if name is a directory, which sends back True is thevalue of $dirname holds a directory’s name.

if test ! -d $dirname

8 Unix system interface

The Unix provides its services via a set of system calls, which can be used by various userprograms. We now look at a few important and useful system calls. Since most of the Unixare written in C, such system calls also provide a way to us to have an insight look of C.

35

Page 36: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

8.1 Look at one call in details

We will experiment with an important UNIX system call, fork(), which creates a newprocess. When a process creates another child process, it simply creates a copy (PCB) ofthe calling process (parent process), except that the child process has its own identificationnumber, pid, and its own pointers to shared kernel entities. After fork() is called, twoprocesses will execute the next statement after this fork() call statement in their respectivedata space.

If the fork call succeeds in the parent process, fork() returns the pid of the newlycreated child process. In the child process, fork() returns a 0.

It may seem like an overkill by creating another process to carry out certain computation,but it is actually essential from at least a reliability consideration. By doing so, the callingprocess actually protects itself from any fatal errors this computation may lead: only thenew process will crash.

This technique is used extensively. For example, after the system is booted, a process 0is created, which is the parent of every processes created during that session. Whenever acommand is to be executed; or whenever a user is to log on, a new process is created.

This important tool will be needed in doing the next, real, project.

8.1.1 An example

Below is a simple example involving the fork() call.

/* Slight modification of Richard Stevens example from "Advanced Programming

in the UNIX Environment"

*/

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int globalVar = 6; /* external variable in the initialized data */

char buf[] = "Write to the standard output\n";

int main(void){

int localVar; /* local variable in the initialized data */

pid_t pid;

localVar = 88;

if (write(STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf)-1){

printf("write error");

exit(1);

36

Page 37: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

}

printf("Now, the fork starts\n");

if ( (pid = fork()) < 0){

printf("fork error\n");

exit(1);

}

else if (pid == 0) {

globalVar++;

localVar++;

}

else sleep(2);

printf("pid = %d, globalVar = %d, localVar = %d\n", getpid(), globalVar, localVar);

exit(0);

}

Labwork 8.1.1: Run the program, then check it out carefully so that you can answer thefollowing questions:

1. What are the pids for the child and parent processes?

2. Which parts are done by both child and parent processes, which by child only, andwhich by the parent only?

3. Why does only the child process increment the two variables? Why are both the globaland the local variables affected?

4. Which process (child or parent) executes the sleep command? Why is that?

8.1.2 Another example

More realistically, a parent wants to create a process to get something done, and will actuallywait until the newly created process to complete. The following code, consisting of two files,demonstrates this situation.

/* The parent.c file */

#include <stdio.h>

#include <stdlib.h>

#include <sys/wait.h>

int main(){

if(fork()==0){

execve("child", NULL, NULL);

exit(0);

37

Page 38: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

}

printf("Process[%d]: Parent in execution...\n", getpid());

sleep(2);

if(wait(NULL)>0)

printf("Process[%d]: Parent detects teminating child \n", getpid());

printf("Process[%d]: parent terminating...\n", getpid());

}

/* The child.c file */

#include <stdio.h>

int main(){

printf("Process[%d]: child in execution...\n", getpid());

sleep(2);

printf("Process[%d]: child terminating...\n", getpid());

}

The parent program will create a process, which executes the child program using thesystem function execve(3), as contained in child.c. The parent then calls the systemfunction wait to block itself until the OS kernel signals the process to continue again, becauseone of its child processes has just terminated.

8.1.3 Scripts again?

Anything executable, including those shell scripts as discussed in Section 7, can be used tostart a child process in this forking context. Below is an example.

/* parent1.c */

#include <stdio.h>

#include <stdlib.h>

#include <sys/wait.h>

int main(int argc, char * argv[]){

if(fork()==0){

char *cmd[] = { "mySpell", argv[1], NULL };

if(argc==1)

execve("child", NULL, NULL);

else if(argc==2)

execve("mySpell", cmd, NULL);

else exit(1);

exit(0);

}

38

Page 39: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

printf("Process[%d]: Parent in execution...\n", getpid());

sleep(2);

if(wait(NULL)> 0)

printf("Process[%d]: Parent detects teminating child \n", getpid());

printf("Process[%d]: parent terminating...\n", getpid());

}

We can now call it with, e.g., the following:

/home/PLYMOUTH/zshen > more parent1.c

#include <stdio.h>

#include <stdlib.h>

#include <sys/wait.h>

int main(int argc, char * argv[]){

if(fork()==0){

char *cmd[] = { "mySpell", argv[1], NULL };

if(argc==1)

execve("child", NULL, NULL);

else if(argc==2)

execve("mySpell", cmd, NULL);

else exit(1);

exit(0);

}

printf("Process[%d]: Parent in execution...\n", getpid());

sleep(2);

if(wait(NULL)> 0)

printf("Process[%d]: Parent detects teminating child \n", getpid());

printf("Process[%d]: parent terminating...\n", getpid());

}

/home/PLYMOUTH/zshen > more Child.c

Child.c: No such file or directory

/home/PLYMOUTH/zshen > more child.c

#include <stdio.h>

int main(){

printf("Process[%d]: child in execution...\n", getpid());

sleep(2);

printf("Process[%d]: child terminating...\n", getpid());

}

39

Page 40: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

/home/PLYMOUTH/zshen > more mySpell

#!/bin/sh

# An improved spelling checker

file=$1

for word in ‘spell $file‘

do

# grep tries to find the line where $word occurs in $file

line=‘grep -n $word $file‘

#print out a line

echo " "

#print out the following line

echo "Missplled word: $word"

#print out the line where the misspelled word occurs

echo "$line"

done

/home/PLYMOUTH/zshen > cc child.c -o child

/home/PLYMOUTH/zshen > cc parent1.c

If we call it without giving it any parameter, thus argc=1, it executes the child program.

/home/PLYMOUTH/zshen > ./a.out

Process[31193]: Parent in execution...

Process[31194]: child in execution...

Process[31194]: child terminating...

Process[31193]: Parent detects teminating child

Process[31193]: parent terminating...

On the other hand, if we call it with a parameter, child.c in this case, it calls mySpellto check the spelling of the words as contained in this file.

/home/PLYMOUTH/zshen > ./a.out child.c

Process[31196]: Parent in execution...

Missplled word: getpid

4: printf("Process[%d]: child in execution...\n", getpid());

6: printf("Process[%d]: child terminating...\n", getpid());

Missplled word: printf

4: printf("Process[%d]: child in execution...\n", getpid());

6: printf("Process[%d]: child terminating...\n", getpid());

Process[31196]: Parent detects teminating child

Process[31196]: parent terminating...

/home/PLYMOUTH/zshen > ’

40

Page 41: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

Labwork 8.1.3:

1. Compile the two files separately. Make sure that you will compile the “child.c” firstand its executable is named “child” (If you forget how to name the executable, checkout earlier chapters of this notes). Then compile the parent file and run it. You maychange the time that the child process waits to see its impact.

2. To get ready for the project, find out the general format, and discuss the various usage,of those “strange” functions as shown in this program, particularly, the exec family,fork(), vfork(), getenv(), getpid(), and wait().

3. (Extra 1 point:) Write a program to calculate the sum of the areas of a triangle, acircle, and a square 2, such that each of them shall be passed to a child process tocalculate, then the result will be sent back to the parent process to sum up. (Hint: Iffork() does not do it, try vfork().

8.2 An example: Listing directories

After writing some thing simple, let’s now read something that is a lot more complicated,which is taken from [2, §8].

What we have discussed is something of a file, i.e., the content of a file. Sometimes, wealso need to know something about a file. For example, a Unix service, ls, which we oftenuse, lists the names of files in a directory, together with other information such as their size,permissions, and so on.

There are two aspects of this issue: Since a directory is just a file, we only need to getaccess to this file and print out its content. But, it is necessary to use a system call toget other information such as the sizes of files. We will show how to write such a program,fsize, that is to find out the size of a file, and when the argument is a directory, it willrecursively finds out the sizes of all the files as contained in such a directory.

As we mentioned in § 2.1, a directory is a file that consists of a list of filenames withsome other information, including the location of those files. Technically, a location of a fileis given as an index into another table called the inode list. An inode for a file tells where allthe information about that file, except its file name is kept. Thus, an entry in a directoryabout a file contains only two pieces: a file name and an index number.

However, the format and precise contents of a directory vary with system. Thus, wehave to specify a standard interface to deal with such a variety by dividing the directorylisting task into two pieces. The outer level defines a structure, Direnet, and three routinesopendir, readdir and closedir to provide system-independent access to the file name andthe associated inode number in a directory entry. We will write fsize with this interface inmind.

2Make up the sizes yourself, I certainly don’t care about that piece at all. You should have learned,

somewhere between preschool and high school, how to calculate those areas, given, for a triangle, its bottom

b, and height, h; for a circle, its radius, r; and for a square, its side length, s. Just in case you have forgotten,

it is b×h

2for a triangle; π × r2 for a circle; and you-know-what for a square, respectively.

41

Page 42: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

Then, we will show how to implement such a structure and routines on the directorystructures of Version 7 and System V Unix.

The advantage for such an approach is that, when given a different implementation, weonly need to revise the implementation of such an interface, rather than changing everything.

8.2.1 Given a standard interface...

The Dirent structure contains the name of a file and its associated inode index number.The declaration of this structure and other routines are collected in a file dirent.h as thestandard interface we can work with.

#define NAME_MAX 14 /* longest file name */

typedef struct } /* portable directory structure */

long ino;

char name[NAME_MAX];

} Dirent;

typedef struct { /* a minimal directory structure with no buffer */

int fd; /* file descriptor for a directory */

Dirent d; /* the Dirent for this directory */

} DIR;

DIR *opendir(char *dirname);

Dirent *readdir(DIR *dfd);

void closedir(DIR *dfd);

The system call stat takes a file name and returns all of the information in the inodefor that file, or -1 if there is an error.

char *name;

struct stat stbuf;

int stat(char *, struct stat *);

With the above declaration, the way to call it is certainly the following:

stat(name, &stbuf);

which fills the structure stbuf with the inode information for the file with its name beingname. The structure stat, as declared in <sys/stat.h> typically contains the followinginformation:

struct stat {/*inode information returned by stat */

dev_t st_dev; /* divide of inode */

ino_t st_ino; /* inode number */

short st_mode; /* mode bits */

42

Page 43: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

short st_nlinks; /* number of links to file */

short st_uid; /* owner’s user id */

short st_gid; /* owner’s group id */

dev_t st_rdev; /* for special files */

off_t st_size; /* file size i characters */

time_t st_atime; /* time last accessed */

time_t st_mtime; /* time last modified */

time_t st_ctime /* time inode last changed */

};

Now, we have some ideas as where the information for file Properties is kept; and also howdoes the OS know who has what access rights to a file.

The types dev_t and ino_t are defined in <sys/types.h> , which should be includedas will.

The st_mode contains a bunch of flags to further describe the nature of the file in question.The following is just a part of such description, which is also defined in <sys/types.h> .

#define S_IFMT 0160000 /* type of file */

#define S_IFDIR 0040000 /* directory */

#define S_IFCHR 0020000 /* character special */

#define S_IFBLK 0060000 /* block special */

#define S_IFREG 0100000 /* regular */

/* ... */

We are now ready to write the program fsize. If the mode st_mode obtained tells usthat it is a file, then its size st_size can be immediately printed. Otherwise, we have toprocess that directory one file at a time. The process can be recursive since such a directorymight contain subdirectory. Let’s look at it.

#include <stdio.h>

#include <string.h>

#include "syscalls.h"

#include <fcntl.h>

#include <sys/stat.h>

#include "dirent.h"

void fsize(char *);

main(int argc, char *argv[]){

if (argc==1)

fsize(".");

else

while (--argc> 0)

fsize(*++argv);

43

Page 44: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

return 0;

}

The function fsize prints the size of the file. If the file is a directory, it calls a functiondirwalk to process all the files within such a directory.

int stat(char *, struct stat *);

void dirwalk(char *, void (*fcn)(char *));

void fsize(char *name){

struct stat stbuf;

if(stat(name, &stbuf)==-1){

fprintf(stderr, "fsize: can’t access %s\n", name);

return;

}

if(stbuf.st_mode & S_IFMT)==S_IFDIR)

/* check the st_mode structure */

dirwalk(name, sfize);

/* go ahead with the guts first */

printf("%8ld %s\n", stbuf.st-size, name);

/* prints out information for this directory file */

}

The function dirwalk applies a function, as given in terms of a pointer, to each file (subdirectory)in a directory. The general flow is clear: it opens the directory, goes through all the fileswithin with the function, then closes the directory and returns. To find out the size of a file,it calls fsize indirectly.

#define MAX_PATH 1024

void dirwalk(char *dir, void (*fcn)(char *)){

char name[MAX_PATH];

Dirent *dp; /* portable interface */

DIR *dfd;

/* calls opendir to fill the fstat structure */

if((dfd=opendir(dir))==NULL){

fprintf(stderr, "fsize: can’t access %s\n", name);

return;

}

while((dp=readdir(dfd))!=NULL){

if(strcmp(dp-> name, ".")==0||strcmp(dp-> name, "..")==0)

continue; /* skip itself and parent, see Section 2.1 */

44

Page 45: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

if(strlen(dir)+strlen(dp-> name)+2> sizeof(name))

/* dir/dp-> name/ contains more than MAX_PATH characters /*

fprintf(stderr, "dirwalk: name %s %s too long\n", dir, dp-> name);

else {

sprintf(name, "%s%s", dir, dp-> name);

/* fsize is applied in dirwalk in fsize declaration */

(*fcn)(name);

}

}

/* Done when no more files are left */

fclose(dfd);

}

8.2.2 ... and an implementation of the interface

We are done with the standard interface, and now have to get down to the next level toprovide a minimal implementation for this standard interface.

The following one is for Version 7 and System V Unix, two of the more popular versionsof Unix, which uses the following directory information as declared in <sys/dir.h> .

/* See Chapter 4 notes

#ifndef DIRSIZE

#define DIRSIZ 14

#endif

struct direct {/* directory entry */

ino_t d_ino; /* inode number */

char d_name[DIRSIZ]; /* long name does not have ’\0’ */

}

It is clear that in those two versions, the names are rather short. For some other versions,much longer names are allowed which could lead to a more complicated directory structure.

The type ino_t as occurred in the above direct and stat structures is a typedef thatdescribes the index into the inode list. We regularly implement it as a unsigned short

type, but it may change with a different system.Now, we will check out the three basic routines: opendir, readdir and closedir.

The routine opendir opens the directory as a file, verifies its status as a directory,allocates a directory structure for it, and then records the information.

/* fstat is similar to stat, but applies to a file descriptor

rather than a file name */

int fstat(int fd, struct state *);

DIR *openfir(char *dirname){

45

Page 46: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

int fd;

struct stat stbuf;

/* DIR is part of the standard interface */

DIR *dp;

if((fd=open(dirname, O_RDONLY, 0))==-1 ||fstat(fd, &stbuf)==-1

||(stbuf.st_mode & S_IFMT)!=S_IMDIR ||(dp=(DIR*) malloc(sizeof(DIR)))==NULL)

return NULL;

dp-> fd=fd;

return dp;

}

The routine closedir simply closes a directory opened by an invocation of opendir.

void closedir(DIR *dp){

if(dp){

/* for close system call, see Section 6.3 of this lab notes */

close(dp-> fd);

/* for the free function, see the address arithmetic unit in Chapter 5 notes */

free(dp);

}

}

The important(:-)) routine readdir uses the read system call to reach each directoryentry. If a directory slot is not currently in use, the inode number is 0, and this position isskipped. Otherwise, the inode number and the associated name will be placed in a static

structure as called for the standard interface, then return a pointer to such a structure.

#include <sys/dir.h>

Dirent *readdir(DIR *dp){

struct direct dirbuf;

static Dirent d;

while(read(dp-fd, (char *)&dirbuf, sizeof(dirbuf))==sizeof(difbuf)){

if(dirbuf.d_info==0) /* slot not in use */

continue;

d.ino=dirbuf.d_ino;

strcpy(d.name, dirbuf.d_name, DIRSIZ);

d.name[DIRSIZ]=’\0’;

return &d;

}

return NULL

}

46

Page 47: A Gentler Introduction to Unix - Plymouth State Universityturing.plymouth.edu/~zshen/Webfiles/notes/CS2470/labNotes/unix... · A Gentler Introduction to Unix Zhizhang Shen ∗ Dept

8.2.3 A little summary

The function fsize is one of those programs that they are not “system programs” in thesense that they are not exclusively written for the system, but they merely make use of theinformation provided by the system, through the stat structure in this case. For such pro-grams, all the system information should be included as a head file, such as the sys/stat.h

file, but not embedded into these programs. There are at least two advantages for this ap-proach: one is to share all such information consistently among all the files that have a needto use them; and the other is to remove as much redundancy as possible.

The division of the design in terms of a standard interface and a system dependentimplementation is also an important point since this makes the fsize function specificsystem independent.

Another important point for this example is that it demonstrates how to use C, a high-level programming language, to write programs at a system level.

It also demonstrates a few applications of bitwise operators, space allocations, etc..

References

[1] Anderson, P., Just Enough Unix (Fourth Ed.), McGraw-Hill, Boson, MA, 2003.

[2] Kernighan, B., and Ritche, D., The C Programming Language (Second Ed.), PrenticeHall, Englewood Cliffs, NJ, 1988.

47