Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State...

132
September 23, 1998 Introduction to Unix Frank G. Fiamingo Linda DeBula Linda Condron University Technology Services The Ohio State University

Transcript of Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State...

Page 1: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Introduction to Unix

Frank G. Fiamingo

Linda DeBula

Linda Condron

University Technology Services

The Ohio State University

September 23, 1998

Page 2: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

eering

wing

imer.

romote

e and

ude

© 1996-1998 University Technology Services, The Ohio State University, Baker Systems EnginBuilding, 1971 Neil Avenue, Columbus, OH 43210.All rights reserved. Redistribution and use, with or without modification, are permitted provided that the folloconditions are met:

1. Redistributions must retain the above copyright notice, this list of conditions, and the following discla

2. Neither the name of the University nor the names of its contributors may be used to endorse or pproducts or services derived from this document without specific prior written permission.

THIS PUBLICATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. THIS PUBLICATION MAYINCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS.

UNIX is a registered trademark of The Open Group, AT&T is a trademark of American TelephonTelegraph, Inc.

This publication is provided “as is” without warranty of any kind. This publication may incltechnical inaccuracies or typographical errors.

Copyright and URLs revised September 1998

The authors’ email addresses are:Frank Fiamingo [email protected] DeBula [email protected] Condron [email protected]

This document can be obtained via:http://wks.uts.ohio-state.edu/unix_course/unix.htmlorftp://wks.uts.ohio-state.edu/unix_course/unix_book.ps

2 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 3: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Table of Contents

1 History of Unix ..............................................................................72 Unix Structure ...............................................................................9

2.1 The Operating System ..................................................................92.2 The File System ...........................................................................112.3 Unix Directories, Files and Inodes .............................................122.4 Unix Programs ............................................................................13

3 Getting Started ............................................................................143.1 Logging in ....................................................................................14

3.1.1 Terminal Type .............................................................143.1.2 Passwords ....................................................................153.1.3 Exiting .........................................................................153.1.4 Identity ........................................................................16

3.2 Unix Command Line Structure .................................................163.3 Control Keys ................................................................................173.4 stty - terminal control .................................................................173.5 Getting Help ................................................................................193.6 Directory Navigation and Control .............................................20

3.6.1 pwd - print working directory .....................................213.6.2 cd - change directory ...................................................213.6.3 mkdir - make a directory .............................................223.6.4 rmdir - remove directory .............................................223.6.5 ls - list directory contents ............................................23

3.7 File Maintenance Commands ....................................................253.7.1 cp - copy a file .............................................................263.7.2 mv - move a file ..........................................................263.7.3 rm - remove a file ........................................................273.7.4 File Permissions ..........................................................273.7.5 chmod - change file permissions .................................283.7.6 chown - change ownership ..........................................293.7.7 chgrp - change group ..................................................29

3.8 Display Commands .....................................................................303.8.1 echo - echo a statement ...............................................30

Introduction to Unix 1998 University Technology Services, The Ohio State University 3

Page 4: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

3.8.2 cat - concatenate a file .................................................313.8.3 more, less, and pg - page through a file ......................313.8.4 head - display the start of a file ...................................323.8.5 tail - display the end of a file ......................................32

4 System Resources & Printing ....................................................334.1 System Resources ........................................................................33

4.1.1 df - summarize disk block and file usage ....................344.1.2 du - report disk space in use ........................................344.1.3 ps - show status of active processes ............................354.1.4 kill - terminate a process .............................................364.1.5 who - list current users ................................................374.1.6 whereis - report program locations ............................374.1.7 which - report the command found .............................384.1.8 hostname/uname - name of machine ...........................384.1.9 script - record your screen I/O ....................................384.1.10 date - current date and time .........................................40

4.2 Print Commands .........................................................................414.2.1 lp/lpr - submit a print job ............................................414.2.2 lpstat/lpq - check the status of a print job ...................424.2.3 cancel/lprm - cancel a print job ...................................424.2.4 pr - prepare files for printing .......................................43

5 Shells ............................................................................................455.1 Built-in Commands .....................................................................46

5.1.1 Sh ................................................................................465.1.2 Csh ..............................................................................47

5.2 Environment Variables ..............................................................485.3 The Bourne Shell, sh ...................................................................495.4 The C Shell, csh ...........................................................................505.5 Job Control ..................................................................................515.6 History ..........................................................................................525.7 Changing your Shell ...................................................................54

6 Special Unix Features .................................................................556.1 File Descriptors ...........................................................................556.2 File Redirection ...........................................................................55

6.2.1 Csh ..............................................................................566.2.2 Sh ................................................................................57

6.3 Other Special Command Symbols .............................................586.4 Wild Cards ...................................................................................58

7 Text Processing ............................................................................597.1 Regular Expression Syntax ........................................................597.2 Text Processing Commands .......................................................61

7.2.1 grep .............................................................................617.2.2 sed ...............................................................................65

4 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 5: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

7.2.3 awk, nawk, gawk .........................................................678 Other Useful Commands ............................................................70

8.1 Working With Files ....................................................................708.1.1 cmp - compare file contents ........................................718.1.2 diff - differences in files ..............................................728.1.3 cut - select parts of a line ............................................738.1.4 paste - merge files .......................................................748.1.5 touch - create a file ......................................................768.1.6 wc - count words in a file ............................................778.1.7 ln - link to another file ................................................788.1.8 sort - sort file contents .................................................798.1.9 tee - copy command output .........................................828.1.10 uniq - remove duplicate lines ......................................848.1.11 strings - find ASCII strings .........................................858.1.12 file - file type ...............................................................868.1.13 tr - translate characters ................................................868.1.14 find - find files ............................................................89

8.2 File Archiving, Compression and Conversion .........................918.2.1 File Compression ........................................................918.2.2 tar - archive files .........................................................938.2.3 uuencode/uudecode - encode a file .............................948.2.4 dd - block copy and convert ........................................958.2.5 od - octal dump of a file ..............................................96

8.3 Remote Connections ...................................................................988.3.1 TELNET and FTP - remote login and file transfer protocols 988.3.2 finger - get information about users ..........................1008.3.3 Remote commands ....................................................101

9 Shell Programming ...................................................................1039.1 Shell Scripts ...............................................................................1039.2 Setting Parameter Values .........................................................1039.3 Quoting ......................................................................................1049.4 Variables ....................................................................................1059.5 Parameter Substitution ............................................................1079.6 Here Document ..........................................................................1099.7 Interactive Input .......................................................................110

9.7.1 Sh ..............................................................................1109.7.2 Csh ............................................................................110

9.8 Functions ....................................................................................1119.9 Control Commands ...................................................................113

9.9.1 Conditional if ............................................................1139.9.1.1 Sh ........................................................................1139.9.1.2 Csh ......................................................................114

9.9.2 Conditional switch and case ......................................1159.9.2.1 Sh ........................................................................115

Introduction to Unix 1998 University Technology Services, The Ohio State University 5

Page 6: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

9.9.2.2 Csh ......................................................................1169.9.3 for and foreach ..........................................................117

9.9.3.1 Sh ........................................................................1179.9.3.2 Csh ......................................................................117

9.9.4 while ..........................................................................1189.9.4.1 Sh ........................................................................1189.9.4.2 Csh ......................................................................119

9.9.5 until ...........................................................................1199.9.6 test .............................................................................1209.9.7 C Shell Logical and Relational Operators ................122

10 Editors ........................................................................................12310.1 Configuring Your vi Session ....................................................12410.2 Configuring Your emacs Session .............................................12510.3 vi Quick Reference Guide ........................................................12610.4 emacs Quick Reference Guide ..............................................127

11 Unix Command Summary .......................................................12811.1 Unix Commands ........................................................................128

12 A Short Unix Bibliography ......................................................13112.1 Highly Recommended ...............................................................13112.2 Assorted Others .........................................................................131

6 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 7: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

newevel

e Bellnaday,DP-7.

r user

izationmong

ten in

g thely great fromational

CHAPTER 1 History of Unix

1965 Bell Laboratories joins with MIT and General Electric in the development effort for theoperating system, Multics, which would provide multi-user, multi-processor, and multi-l(hierarchical) file system, among its many forward-looking features.

1969 AT&T was unhappy with the progress and drops out of the Multics project. Some of thLabs programmers who had worked on this project, Ken Thompson, Dennis Ritchie, Rudd Caand Doug McIlroy designed and implemented the first version of the Unix File System on a Palong with a few utilities. It was given the name UNIX by Brian Kernighan as a pun on Multics

1970, Jan 1 time zero for UNIX

1971 The system now runs on a PDP-11, with 16Kbytes of memory, including 8Kbytes foprograms and a 512Kbyte disk.

Its first real use is as a text processing tool for the patent department at Bell Labs. That utiljustified further research and development by the programming group. UNIX caught on aprogrammers because it was designed with these features:

• programmers environment

• simple user interface

• simple utilities that can be combined to perform powerful functions

• hierarchical file system

• simple interface to devices consistent with file format

• multi-user, multi-process system

• architecture independent and transparent to the user.

1973 Unix is re-written mostly in C, a new language developed by Dennis Ritchie. Being writthis high-level language greatly decreased the effort needed to port it to new machines.

1974 Thompson and Ritchie publish a paper in the Communications of the ACM describinnew Unix OS. This generates enthusiasm in the Academic community which sees a potentialteaching tool for studying programming systems development. Since AT&T is preventedmarketing the product due to the 1956 Consent Decree they license it to Universities for educpurposes and to commercial entities.

1977 There are now about 500 Unix sites world-wide.

Introduction to Unix 1998 University Technology Services, The Ohio State University 7

Page 8: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

History of Unix

ms, of

later

and

tems

1980 BSD 4.1 (Berkeley Software Development)

1983 SunOS, BSD 4.2, SysV

1984 There are now about 100,000 Unix sites running on many different hardware platforvastly different capabilities.

1988 AT&T and Sun Microsystems jointly develop System V Release 4 (SVR4). This wouldbe developed into UnixWare and Solaris 2.

1993 Novell buys UNIX from AT&T

1994 Novell gives the name "UNIX " to X/OPEN

1995 Santa Cruz Operations buys UnixWare from Novell. Santa Cruz OperationsHewlett-Packard announce that they will jointly develop a 64-bit version of Unix.

1996 International Data Corporation forecasts that in 1997 there will be 3 million Unix sysshipped world-wide.

8 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 9: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

The Operating System

ices for

anything kernelat mostortable

e: openershipabling

to aess andother I/O

CHAPTER 2 Unix Structure

2.1 The Operating System

Unix is a layered operating system. The innermost layer is the hardware that provides the servthe OS. The operating system, referred to in Unix as thekernel, interacts directly with the hardwareand provides the services to the user programs. These user programs don’t need to know about the hardware. They just need to know how to interact with the kernel and it’s up to theto provide the desired service. One of the big appeals of Unix to programmers has been thwell written user programs are independent of the underlying hardware, making them readily pto new systems.

User programs interact with the kernel through a set of standardsystem calls. These system callsrequest services to be provided by the kernel. Such services would include accessing a filclose, read, write, link, or execute a file; starting or updating accounting records; changing ownof a file or directory; changing to a new directory; creating, suspending, or killing a process; enaccess to hardware devices; and setting limits on system resources.

Unix is a multi-user, multi-tasking operating system. You can have many users logged insystem simultaneously, each running many programs. It’s the kernel’s job to keep each procuser separate and to regulate access to system hardware, including cpu, memory, disk and devices.

Introduction to Unix 1998 University Technology Services, The Ohio State University 9

Page 10: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Unix Structure

FIGURE 2.1 Unix System Structure

Hardware

Kernel

System Calls

Programs

10 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 11: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

The File System

2.2 The File System

The Unix file system looks like an inverted tree structure. You start with theroot directory, denotedby /, at the top and work down through sub-directories underneath it.

FIGURE 2.2 Unix File Structure

/

bin dev etc lib tmp usr home

sh date csh

ttya cua0

passwd group

bin lib local

condron frank lindadb

source mail bin

xntp traceroute

Introduction to Unix 1998 University Technology Services, The Ohio State University 11

Page 12: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Unix Structure

ndorhes of

ry that

rent filenel toate ofthe disk

an bet code

Each node is either afile or a directory of files, where the latter can contain other files adirectories. You specify a file or directory by itspath name, either the full, or absolute, path name the one relative to a location. The full path name starts with the root, /, and follows the brancthe file system, each separated by /, until you reach the desired file, e.g.:

/home/condron/source/xntp

A relative path name specifies the path relative to another, usually the current working directoyou are at. Two special directory entries should be introduced now:

. the current directory

.. the parent of the current directory

So if I’m at /home/frank and wish to specify the path above in a relative fashion I could use:

../condron/source/xntp

This indicates that I should first go up one directory level, then come down through thecondrondirectory, followed by thesource directory and then toxntp.

2.3 Unix Directories, Files and Inodes

Everydirectory andfile is listed in its parent directory. In the case of the root directory, that pais itself. A directory is a file that contains a table listing the files contained within it, givingnames to theinode numbers in the list. An inode is a special file designed to be read by the kerlearn the information about each file. It specifies the permissions on the file, ownership, dcreation and of last access and change, and the physical location of the data blocks on containing the file.

The system does not require any particular structure for the data in the file itself. The file cASCII or binary or a combination, and may represent text data, a shell script, compiled objecfor a program, directory table, junk, or anything you would like.

There’s no header, trailer, label information orEOF character as part of the file.

12 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 13: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Unix Programs

thecript; a

u can

cated inlly

2.4 Unix Programs

A program, or command, interacts with the kernel to provide the environment and performfunctions called for by the user. A program can be: an executable shell file, known as a shell sbuilt-in shell command; or a source compiled, object code file.

Theshell is a command line interpreter. The user interacts with the kernel through the shell. Yowrite ASCII (text) scripts to be acted upon by a shell.

System programs are usually binary, having been compiled from C source code. These are loplaces like/bin, /usr/bin, /usr/local/bin, /usr/ucb, etc. They provide the functions that you normathink of when you think of Unix. Some of these aresh, csh, date, who, more, and there are manyothers.

Introduction to Unix 1998 University Technology Services, The Ohio State University 13

Page 14: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Getting Started

e known

d;

e set to

o

CHAPTER 3 Getting Started

3.1 Logging in

After connecting with a Unix system, a user is prompted for alogin username, then apassword. Thelogin username is the user's unique name on the system. The password is a changeable codonly to the user. At thelogin prompt, the user should enter the username; at thepassword prompt,the current password should be typed.

Note: Unix is case sensitive. Therefore, thelogin andpassword should be typed exactly as issuethe login, at least, will normally be in lower case.

3.1.1 Terminal Type

Most systems are set up so the user is by default prompted for a terminal type, which should bmatch the terminal in use before proceeding. Most computers work if you choose "vt100". Usersconnecting using a Sun workstation may want to use "sun"; those using an X-Terminal may want tuse "xterms" or "xterm".

The terminal type indicates to the Unix system how to interact with the session just opened.

Should you need to reset the terminal type, enter the command:

setenv TERM<term type> - if using the C-shell (see Chapter 4.)

(On some systems, e.g. MAGNUS, it’s also necessary to type "unsetenv TERMCAP".)

-or-

TERM=<term type>; export TERM - if using the Bourne shell (see Chapter 4.)

where <term type> is the terminal type, such asvt100, that you would like set.

14 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 15: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Logging in

andg. The

tor new

ed.

ord (is itrogram.

ither byons for

sword

3.1.2 Passwords

When your account is issued, you will be given an initial password. It is important for systempersonal security that the password for your account be changed to something of your choosincommand for changing a password is "passwd". You will be asked both for your old password andtype your new selected password twice. If you mistype your old password or do not type youpassword the same way twice, the system will indicate that the password has not been chang

Some system administrators have installed programs that check for appropriateness of passwcryptic enough for reasonable system security). A password change may be rejected by this p

When choosing a password, it is important that it be something that could not be guessed -- esomebody unknown to you trying to break in, or by an acquaintance who knows you. Suggestichoosing and using a password follow:

Don't use a word (or words) in any language

use a proper name

use information that can be found in your wallet

use information commonly known about you (car license, pet name, etc)

use control characters. Some systems can't handle them

write your password anywhere

ever give your password to *anybody*

Do use a mixture of character types (alphabetic, numeric, special)

use a mixture of upper case and lower case

use at least 6 characters

choose a password you can remember

change your password often

make sure nobody is looking over your shoulder when you are entering your pas

3.1.3 Exiting

^D - indicates end of data stream; can log a user off. The latter is disabled on many systems

^C - interrupt

logout - leave the system

exit - leave the shell

Introduction to Unix 1998 University Technology Services, The Ohio State University 15

Page 16: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Getting Started

rid or

not ase one

nistrator.ghts to

es of

be

h all

3.1.4 Identity

The system identifies you by the user and group numbers (userid and groupid, respectively)assigned to you by your system administrator. You don’t normally need to know your usegroupid as the system translates username↔ userid, and groupname↔ groupid automatically. Youprobably already know your username; it’s the name you logon with. The groupname is obvious, and indeed, you may belong to more than one group. Your primary group is thassociated with your username in the password database file, as set up by your system admiSimilarly, there is a group database file where the system administrator can assign you riadditional groups on the system.

In the examples below% is your shell prompt; you don’t type this in.

You can determine your userid and the list of groups you belong to with theid and groupscommands. On some systemsid displays your user and primary group information, e.g.:

% id

uid=1101(frank) gid=10(staff)

on other systems it also displays information for any additional groups you belong to:

% id

uid=1101(frank) gid=10(staff) groups=10(staff),5(operator),14(sysadmin),110(uts)

Thegroups command displays the group information for all the groups you belong to, e.g.:

% groups

staff sysadmin uts operator

3.2 Unix Command Line Structure

A command is a program that tells the Unix system to do something. It has the form:

command [options] [arguments]where anargument indicates on what the command is to perform its action, usually a file or serifiles. An option modifies the command, changing the way it performs.

Commands are case sensitive.command andCommand are not the same.

Options are generally preceded by a hyphen (-), and for most commands, more than one option canstrung together, in the form:

command -[option][option][option]e.g.:

ls -alRwill perform a long list on all files in the current directory and recursively perform the list througsub-directories.

For most commands you can separate the options, preceding each with a hyphen, e.g.:

command -option1 -option2 -option3

16 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 17: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Control Keys

usually

ow theptionsitespace

You

re

put

ysh were

he

uding

as in:

ls -a -l -RSome commands have options that require parameters. Options requiring parameters arespecified separately, e.g.:

lpr -Pprinter3 -# 2 filewill send 2 copies of file to printer3.

These are the standard conventions for commands. However, not all Unix commands will follstandard. Some don’t require the hyphen before options and some won’t let you group otogether, i.e. they may require that each option be preceded by a hyphen and separated by whfrom other options and arguments.

Options and syntax for a command are listed in theman page for the command.

3.3 Control Keys

Control keys are used to perform special functions on the command line or within an editor. type these by holding down theControl key and some otherkey simultaneously. This is usuallyrepresented asK ey. Control-S would be written asS. With control keys upper and lower case athe same, soS is the same ass. This particular example is astop signal and tells the terminal tostop accepting input. It will remain that way until you type astart signal,^Q .

Control-U is normally the "line-kill " signal for your terminal. When typed it erases the entire inline.

In thevi editor you can type a control key into your text file by first typing^V followed by the controlcharacter desired, so to type^H into a document typeV^H .

3.4 stty - terminal control

stty reports or sets terminal control options. The "tty " is an abbreviation that harks back to the daof teletypewriters, which were associated with transmission of telegraph messages, and whicmodels for early computer terminals.

For new users, the most important use of thestty command is setting the erase function to tappropriate key on their terminal. For systems programmers or shell script writers, thestty commandprovides an invaluable tool for configuring many aspects of I/O control for a given device, inclthe following:

- erase and line-kill characters

- data transmission speed

- parity checking on data transmission

- hardware flow control

- newline (NL) versus carriage return plus linefeed (CR-LF)

Introduction to Unix 1998 University Technology Services, The Ohio State University 17

Page 18: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Getting Started

you

- interpreting tab characters

- edited versus raw input

- mapping of upper case to lower case

This command is very system specific, so consult theman pages for the details of thestty commandon your system.

Syntax

stty [options]

Options

(none) report the terminal settings

all (or -a) report on all options

echoe echo ERASE as BS-space-BS

dec set modes suitable for Digital Equipment Corporation operating systems (whichdistinguishes between ERASE and BACKSPACE) (Not available on all systems)

kill set the LINE-KILL character

erase set the ERASE character

intr set the INTERRUPT character

Examples

You can display and change your terminal control settings with thestty command. To display all (-a)of the current line settings:

% stty -a

speed 38400 baud, 24 rows, 80 columns

parenb -parodd cs7 -cstopb -hupcl cread -clocal -crtscts

-ignbrk brkint ignpar -parmrk -inpck istrip -inlcr -igncr icrnl -iuclc

ixon -ixany -ixoff imaxbel

isig iexten icanon -xcase echo echoe echok -echonl -noflsh -tostop

echoctl -echoprt echoke

opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel

erase kill werase rprnt flush lnext susp intr quit stop eof

^H ^U ^W ^R ^O ^V ^Z/^Y ^C ^\ ^S/^Q ^D

You can change settings usingstty, e.g., to change the erase character from^? (the delete key) toH :

% stty erase ^H

This will set the terminal options for the current session only. To have this done forautomatically each time you login, it can be inserted into the.login or .profile file that we’ll look atlater.

18 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 19: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Getting Help

ix

t you

re found.ou can

3.5 Getting Help

The Unix manual, usually calledman pages, is available on-line to explain the usage of the Unsystem and commands. To use a man page, type the command "man" at the system prompt followedby the command for which you need information.

Syntax

man [options] command_name

Common Options

-k keyword list command synopsis line for all keyword matches

-M path path to man pages

-a show all matching man pages (SVR4)

Examples

You can useman to provide a one line synopsis of any commands that contain the keyword thawant to search on with the "-k" option, e.g. to search on the keywordpassword, type:

% man -k password

passwd (5) - password file

passwd (1) - change password information

The number in parentheses indicates the section of the man pages where these references weYou can then access the man page (by default it will give you the lower numbered entry, but yuse a command line option to specify a different one) with:

% man passwd

PASSWD(1) USER COMMANDS PASSWD(1)

NAME

passwd - change password information

SYNOPSIS

passwd [ -e login_shell ] [ username ]

DESCRIPTION

passwd changes (or sets) a user's password.

passwd prompts twice for the new password, without displaying

it. This is to allow for the possibility of typing mistakes.

Only the user and the super-user can change the user's password.

OPTIONS

-e Change the user's login shell.

Here we’ve paraphrased and truncated the output for space and copyright concerns.

Introduction to Unix 1998 University Technology Services, The Ohio State University 19

Page 20: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Getting Started

der the

vide

3.6 Directory Navigation and Control

The Unix file system is set up like a tree branching out from the root. The theroot directory of thesystem is symbolized by the forward slash (/). System and user directories are organized unroot. The user does not have a root directory in Unix; users generally log into their ownhomedirectory. Users can then create other directories under theirhome. The following table summarizessome directory navigation commands.

If you’re familiar with DOS the following table comparing similar commands might help to prothe proper reference frame.

TABLE 3.1 Navigation and Directory Control Commands

Command/Syntax What it will do

cd [directory] change directory

ls [options] [directory or file] list directory contents orfile permissions

mkdir [options]directory make adirectory

pwd print working (current) directory

rmdir [options]directory remove adirectory

TABLE 3.2 Unix vs DOS Navigation and Directory Control Commands

Command Unix DOS

list directory contents ls dir

make directory mkdir md & mkdir

change directory cd cd & chdir

delete (remove) directory rmdir rd & rmdir

return to user’s home directory cd cd\

location in path(present working directory)

pwd cd

20 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 21: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Directory Navigation and Control

lash)

ng

y

3.6.1 pwd - print working directory

At any time you can determine where you are in the file system hierarchy with thepwd, print workingdirectory, command, e.g.:

% pwd

/home/frank/src

3.6.2 cd - change directory

You can change to a new directory with thecd, change directory, command.cd will accept bothabsolute and relative path names.

Syntax

cd [directory]

Examples

cd (alsochdir in some shells) change directory

cd changes to user's home directory

cd / changes directory to the system's root

cd .. goes up one directory level

cd ../.. goes up two directory levels

cd /full/path/name/from/root changes directory to absolute path named (note the leading s

cdpath/from/current/location changes directory to path relative to current location (no leadislash)

cd ~username/directory changes directory to the named username's indicated director(Note: the~ is not valid in the Bourne shell; see Chapter 5.)

Introduction to Unix 1998 University Technology Services, The Ohio State University 21

Page 22: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Getting Started

ith thetory:

.

s first.e out

3.6.3 mkdir - make a directory

You extend your home hierarchy by making sub-directories underneath it. This is done wmkdir, make directory, command. Again, you specify either the full or relative path of the direc

Syntax

mkdir [options] directory

Common Options

-p create the intermediate (parent) directories, as needed

-m mode access permissions (SVR4). (We’ll look at modes later in this Chapter)

Examples

% mkdir /home/frank/data

or, if your present working directory is /home/frank the following would be equivalent:

% mkdir data

3.6.4 rmdir - remove directory

A directory needs to be empty before you can remove it. If it’s not, you need to remove the fileAlso, you can’t remove a directory if it is your present working directory; you must first changof it.

Syntax

rmdir directory

Examples

To remove the empty directory /home/frank/data while in /home/frank use:

% rmdir data

or

% rmdir /home/frank/data

22 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 23: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Directory Navigation and Control

e

usefulg the

Ifile.

e of

ix.

3.6.5 ls - list directory contents

The command to list your directories and files isls. With options it can provide information about thsize, type of file, permissions, dates of file creation, change and access.

Syntax

ls [options] [argument]

Common Options

When no argument is used, the listing will be of the current directory. There are many very options for the ls command. A listing of many of them follows. When using the command, strindesired options together preceded by "-".

-a lists all files, including those beginning with a dot (.).

-d lists only names of directories, not the files in the directory

-F indicates type of entry with a trailing symbol:

directories /

sockets =

symbolic links @

executables *

-g displays Unix group assigned to the file, requires the -l option (BSD only)

-or- on an SVR4 machine, e.g. Solaris, this option has the opposite effect

-L if the file is a symbolic link, lists the information for the file or directory the linkreferences, not the information for the link itself

-l long listing: lists the mode, link information, owner, size, last modification (time).the file is a symbolic link, an arrow (-->) precedes the pathname of the linked-to f

Themode field is given by the-l option and consists of 10 characters. The first character is onthe following:

CHARACTER IF ENTRY IS A

d directory

- plain file

b block-type special file

c character-type special file

l symbolic link

s socket

The next 9 characters are in 3 sets of 3 characters each. They indicate thefile access permissions:the first 3 characters refer to the permissions for theuser, the next three for the users in the Ungroup assigned to the file, and the last 3 to the permissions forother users on the systemDesignations are as follows:

Introduction to Unix 1998 University Technology Services, The Ohio State University 23

Page 24: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Getting Started

ese are

r read permissionw write permissionx execute permission- no permission

There are a few less commonly used permission designations for special circumstances. Thexplained in the man page forls.

Examples

To list the files in a directory:

% ls

demofiles frank linda

To list all files in a directory, including the hidden (dot) files try:

% ls -a

. .cshrc .history .plan .rhosts frank

.. .emacs .login .profile demofiles linda

To get a long listing:

% ls -al

total 24

drwxr-sr-x 5 workshop acs 512 Jun 7 11:12 .

drwxr-xr-x 6 root sys 512 May 29 09:59 ..

-rwxr-xr-x 1 workshop acs 532 May 20 15:31 .cshrc

-rw------- 1 workshop acs 525 May 20 21:29 .emacs

-rw------- 1 workshop acs 622 May 24 12:13 .history

-rwxr-xr-x 1 workshop acs 238 May 14 09:44 .login

-rw-r--r-- 1 workshop acs 273 May 22 23:53 .plan

-rwxr-xr-x 1 workshop acs 413 May 14 09:36 .profile

-rw------- 1 workshop acs 49 May 20 20:23 .rhosts

drwx------ 3 workshop acs 512 May 24 11:18 demofiles

drwx------ 2 workshop acs 512 May 21 10:48 frank

drwx------ 3 workshop acs 512 May 24 10:59 linda

24 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 25: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

File Maintenance Commands

s.

vide

3.7 File Maintenance Commands

To create, copy, remove and change permissions on files you can use the following command

If you’re familiar with DOS the following table comparing similar commands might help to prothe proper reference frame.

TABLE 3.3 File Maintenance Commands

Command/Syntax What it will do

chgrp [options]group file change the group of the file

chmod [options]file change file or directory access permissions

chown [options]owner file change the ownership of a file; can only be done by the superuser

cp [options]file1 file2 copyfile1 into file2; file2 shouldn't already exist. This command createsor overwritesfile2.

mv [options]file1 file2 movefile1 into file2

rm [options]file remove (delete) a file or directory (-r recursively deletes the directoryand its contents) (-i prompts before removing files)

TABLE 3.4 Unix vs DOS File Maintenance Commands

Command Unix DOS

copy file cp copy

move file mv move (not supported on all versions of DOS)

rename file mv rename & ren

delete (remove) file rm erase & del

display file to screen

entire file

one page at a time

cat

more, less, pg

type

type/p (not supported on all versions of DOS)

Introduction to Unix 1998 University Technology Services, The Ohio State University 25

Page 26: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Getting Started

ndentde, data

remain

3.7.1 cp - copy a file

Copy the contents of one file to another with thecp command.

Syntax

cp [options] old_filename new_filename

Common Options

-i interactive (prompt and wait for confirmation before proceeding)

-r recursively copy a directory

Examples

% cp old_filename new_filename

You now have two copies of the file, each with identical contents. They are completely indepeof each other and you can edit and modify either as needed. They each have their own inoblocks, and directory table entries.

3.7.2 mv - move a file

Rename a file with the move command,mv.

Syntax

mv [options] old_filename new_filename

Common Options

-i interactive (prompt and wait for confirmation before proceeding)

-f don’t prompt, even when copying over an existing target file (overrides-i)

Examples

% mv old_filename new_filename

You now have a file callednew_filename and the fileold_filename is gone. Actually all you’vedone is to update the directory table entry to give the file a new name. The contents of the filewhere they were.

26 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 27: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

File Maintenance Commands

is tohe disk, is nouserso

,he

dple, of

3.7.3 rm - remove a file

Remove a file with therm, remove, command.

Syntax

rm [options] filename

Common Options

-i interactive (prompt and wait for confirmation before proceeding)

-r recursively remove a directory, first removing the files and subdirectoriesbeneath it

-f don’t prompt for confirmation (overrides -i)

Examples

% rm old_filename

A listing of the directory will now show that the file no longer exists. Actually, all you’ve done remove the directory table entry and mark the inode as unused. The file contents are still on tbut the system now has no way of identifying those data blocks with a file name. Therecommand to "unremove" a file that has been removed in this way. For this reason many novice alias their remove command to be "rm -i", where the-i option prompts them to answer yes or nbefore the file is removed. Such aliases are normally placed in the.cshrc file for the C shell; seeChapter 5)

3.7.4 File Permissions

Each file, directory, and executable has permissions set for who canread, write, and/orexecute it.To find the permissions assigned to a file, thels command with the-l option should be used. Alsousing the-g option with "ls -l" will help when it is necessary to know the group for which tpermissions are set (BSD only).

When using the "ls -lg" command on a file (ls -l on SysV), the output will appear as follows:

-rwxr-x--- user unixgroup size Month nn hh:mm filename

The area above designated by letters and dashes (-rwxr-x--- ) is the area showing the file type anpermissions as defined in the previous Section. Therefore, a permission string, for exam-rwxr-x--- allows theuser (owner) of the file to read, write, and execute it; those in theunixgroup ofthe file can read and execute it;others cannot access it at all.

Introduction to Unix 1998 University Technology Services, The Ohio State University 27

Page 28: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Getting Started

ayr the

rite,ion of 6.

o and

ion

s

3.7.5 chmod - change file permissions

The command to change permissions on an item (file, directory, etc) ischmod (change mode). Thesyntax involves using the command with three digits (representing theuser (owner,u) permissions,thegroup (g) permissions, andother (o) user's permissions) followed by the argument (which mbe a file name or list of files and directories). Or by using symbolic representation fopermissions and who they apply to.

Each of the permission types is represented by either a numeric equivalent:

read=4, write=2, execute=1

or a single letter:

read=r, write=w, execute=x

A permission of4 or r would specifyread permissions. If the permissions desired are read and wthe 4 (representing read) and the 2 (representing write) are added together to make a permissTherefore, a permission setting of 6 would allow read and write permissions.

Alternatively, you could use symbolic notation which uses the one letter representation for whfor the permissions and an operator, where the operator can be:

+ add permissions

- remove permissions

= set permissions

So to set read and write for the owner we could use "u=rw" in symbolic notation.

Syntax

chmodnnn [argument list] numeric mode

chmod[who]op[perm] [argument list] symbolic mode

wherennn are the three numbers representinguser, group, andother permissions,who is any ofu, g,o, or a (all) and perm is any of r , w, x. In symbolic notation you can separate permissspecifications by commas, as shown in the example below.

Common Options

-f force (no error message is generated if the change is unsuccessful)

-R recursively descend through the directory structure and change the modeExamples

If the permission desired for file1 isuser: read, write, execute,group: read, execute,other: read,execute, the command to use would be

chmod 755 file1 or chmod u=rwx,go=rx file1

28 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 29: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

File Maintenance Commands

sty to do

n

Reminder: When giving permissions togroup andother to use a file, it is necessary to allow at leaexecute permission to the directories for the path in which the file is located. The easiest wathis is to be in the directory for which permissions need to be granted:

chmod 711 . or chmod u=rw,+x . or chmod u=rwx,go=x .

where the dot (.) indicatesthis directory.

3.7.6 chown - change ownership

Ownership of a file can be changed with thechown command. On most versions of Unix this caonly be done by the super-user, i.e. a normal user can’t give away ownership of their files.chown isused as below, where# represents the shell prompt for the super-user:

Syntax

chown [options] user[:group] file (SVR4)

chown [options] user[.group] file (BSD)

Common Options

-R recursively descend through the directory structure

-f force, and don’t report any errors

Examples

# chown new_owner file

3.7.7 chgrp - change group

Anyone can change the group of files they own, to another group they belong to, with thechgrpcommand.

Syntax

chgrp [options] group file

Common Options

-R recursively descend through the directory structure

-f force, and don’t report any errors

Examples

% chgrp new_group file

Introduction to Unix 1998 University Technology Services, The Ohio State University 29

Page 30: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Getting Started

slay a

output

cters.

3.8 Display Commands

There are a number of commands you can use todisplay or view a file. Some of these are editorwhich we will look at later. Here we will illustrate some of the commands normally used to dispfile.

3.8.1 echo - echo a statement

Theecho command is used to repeat, or echo, the argument you give it back to the standarddevice. It normally ends with a line-feed, but you can specify an option to prevent this.

Syntax

echo [string]

Common Options

-n don’t print <new-line> (BSD, shell built-in)

\c don’t print <new-line> (SVR4)

\0n wheren is the 8-bit ASCII character code (SVR4)

\t tab (SVR4)

\f form-feed (SVR4)

\n new-line (SVR4)

\v vertical tab (SVR4)

Examples

% echo Hello Class or echo "Hello Class"

To prevent the line feed:

% echo -n Hello Class or echo "Hello Class \c"

where the style to use in the last example depends on theecho command in use.

The \x options must be within pairs of single or double quotes, with or without other string chara

TABLE 3.5 Display Commands

Command/Syntax What it will do

cat [options]file concatenate (list) a file

echo [text string] echo the text string to stdout

head [-number]file display the first 10 (or number of) lines of a file

more (or less or pg) [options]file page through a text file

tail [options]file display the last few lines (or parts) of a file

30 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 31: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Display Commands

n,

ay nots and

3.8.2 cat - concatenate a file

Display the contents of a file with the concatenate command,cat.

Syntax

cat [options] [file]

Common Options

-n precede each line with a line number

-v display non-printing characters, except tabs, new-lines, and form-feeds

-e display $ at the end of each line (prior to new-line) (when used with-v option)

Examples

% cat filename

You can list a series of files on the command line, andcat will concatenate them, starting each in turimmediately after completing the previous one, e.g.:

% cat file1 file2 file3

3.8.3 more, less, and pg - page through a file

more, less, andpg let you page through the contents of a file one screenful at a time. These mall be available on your Unix system. They allow you to back up through the previous pagesearch for words, etc.

Syntax

more [options] [+/pattern] [filename]

less [options] [+/pattern] [filename]

pg [options] [+/pattern] [filename]

Options

more less pg Action-c -c -c clear display before displaying

-i ignore case

-w default default don’t exit at end of input, but prompt and wait

-lines -lines # of lines/screenful

+/pattern +/pattern +/pattern search for the pattern

Introduction to Unix 1998 University Technology Services, The Ohio State University 31

Page 32: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Getting Started

, or a

Internal Controls

more displays (one screen at a time) the file requested<space bar> to view next screen<return> or <CR> to view one more lineq to quit viewing the fileh helpb go back up one screenful/word search forword in the remainder of the file

See theman page for additional options

less similar tomore; see theman page for options

pg the SVR4 equivalent ofmore (page)

3.8.4 head - display the start of a file

head displays the head, or start, of the file.

Syntax

head [options] file

Common Options

-n number number of lines to display, counting from the top of the file

-number same as above

Examples

By default head displays the first 10 lines. You can display more with the "-n number", or"-number" options, e.g., to display the first 40 lines:

% head -40 filename or head -n 40 filename

3.8.5 tail - display the end of a file

tail displays the tail, or end, of the file.

Syntax

tail [options] file

Common Options

-number number of lines to display, counting from the bottom of the file

Examples

The default is to display the last 10 lines, but you can specify different line or byte numbersdifferent starting point within the file. To display the last 30 lines of a file use the-number style:

% tail -30 filename

32 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 33: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

System Resources

CHAPTER 4 System Resources &Printing

4.1 System Resources

Commands to report or manage system resources.

TABLE 4.1 System Resource Commands

Command/Syntax What it will do

chsh (passwd -e/-s) username login_shell change the user’s login shell (often only by the superuser)

date [options] report the current date and time

df [options] [resource] report the summary of disk blocks and inodes free and in use

du [options] [directory or file] report amount of disk space in use+

hostname/uname display or set (super-user only) the name of the current machine

kill [options] [-SIGNAL] [pid#] [%job] send a signal to the process with the process id number (pid#) or jobcontrol number (%n). The default signal is to kill the process.

man [options]command show the manual (man) page for a command

passwd [options] set or change your password

ps [options] show status of active processes

script file saves everything that appears on the screen to file untilexit is executed

stty [options] set or display terminal control options

whereis [options]command report the binary, source, and man page locations for the commandnamed

which command reports the path to the command or the shell alias in use

who or w report who is logged in and what processes are running

Introduction to Unix 1998 University Technology Services, The Ohio State University 33

Page 34: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

System Resources & Printing

. The

4.1.1 df - summarize disk block and file usage

df is used to report the number of disk blocks and inodes used and free for each file systemoutput format and valid options are very specific to the OS and program version in use.

Syntax

df [options] [resource]

Common Options

-l local file systems only (SVR4)

-k report in kilobytes (SVR4)

Examples

{unix prompt 1} df

Filesystem kbytes used avail capacity Mounted on

/dev/sd0a 20895 19224 0 102% /

/dev/sd0h 319055 131293 155857 46% /usr

/dev/sd1g 637726 348809 225145 61% /usr/local

/dev/sd1a 240111 165489 50611 77%/home/guardian

peri:/usr/local/backup

1952573 976558 780758 56%/usr/local/backup

peri:/home/peri 726884 391189 263007 60% /home/peri

peri:/usr/spool/mail 192383 1081 172064 1%/var/spool/mail

peri:/acs/peri/2 723934 521604 129937 80% /acs/peri/2

4.1.2 du - report disk space in use

du reports the amount of disk space in use for the files or directories you specify.

Syntax

du [options] [directory or file]

Common Options

-a display disk usage for each file, not just subdirectories

-s display a summary total only

-k report in kilobytes (SVR4)

34 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 35: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

System Resources

d valid

t the

Examples

{unix prompt 3} du

1 ./.elm

1 ./Mail

1 ./News

20 ./uc

86 .

{unix prompt 4} du -a uc

7 uc/unixgrep.txt

5 uc/editors.txt

1 uc/.emacs

1 uc/.exrc

4 uc/telnet.ftp

1 uc/uniq.tee.txt

20 uc

4.1.3 ps - show status of active processes

ps is used to report on processes currently running on the system. The output format anoptions are very specific to the OS and program version in use.

Syntax

ps [options]

Common Options

BSD SVR4

-a -e all processes, all users

-e environment/everything

-g process group leaders as well

-l -l long format

-u -u user user oriented report

-x -e even processes not executed from terminals

-f full listing

-w report first 132 characters per line

note -- Because theps command is highly system-specific, it is recommended that you consulman pages of your system for details of options and interpretation ofps output.

Introduction to Unix 1998 University Technology Services, The Ohio State University 35

Page 36: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

System Resources & Printing

nly

Examples

{unix prompt 5} ps

PID TT STAT TIME COMMAND

15549 p0 IW 0:00 -tcsh (tcsh)

15588 p0 IW 0:00 man nice

15594 p0 IW 0:00 sh -c less /tmp/man15588

15595 p0 IW 0:00 less /tmp/man15588

15486 p1 S 0:00 -tcsh (tcsh)

15599 p1 T 0:00 emacs unixgrep.txt

15600 p1 R 0:00 ps

4.1.4 kill - terminate a process

kill sends a signal to a process, usually to terminate it.

Syntax

kill [-signal] process-id

Common Options

-l displays the available kill signals:

Examples

{unix prompt 9} kill -l

HUP INT QUIT ILL TRAP IOT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM URG STOP

TSTP CONT CHLD TTIN TTOU IO XCPU XFSZ VTALRM PROF WINCH LOST USR1 USR2

The -KILL signal, also specified as-9 (because it is 9th on the above list), is the most commousedkill signal. Once seen, it can’t be ignored by the program whereas the other signals can.

{unix prompt 10} kill -9 15599

[1] + Killed emacs unixgrep.txt

36 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 37: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

System Resources

nd(s).

4.1.5 who - list current users

who reports who is logged in at the present time.

Syntax

who [am i]

Examples

beauty condron>who

wmtell ttyp1 Apr 21 20:15 (apple.acs.ohio-s)

fbwalk ttyp2 Apr 21 23:21 (worf.acs.ohio-st)

stwang ttyp3 Apr 21 23:22 (127.99.25.8)

david ttyp4 Apr 21 22:27 (slip1-61.acs.ohi)

tgardner ttyp5 Apr 21 23:07 (picard.acs.ohio-)

awallace ttyp6 Apr 21 23:00 (ts31-4.homenet.o)

gtl27 ttyp7 Apr 21 23:24 (data.acs.ohio-st)

ccchang ttyp8 Apr 21 23:32 (slip3-10.acs.ohi)

condron ttypc Apr 21 23:38 (lcondron-mac.acs)

dgildman ttype Apr 21 22:30 (slip3-36.acs.ohi)

fcbetz ttyq2 Apr 21 21:12 (ts24-10.homenet.)

beauty condron>who am i

beauty!condron ttypc Apr 21 23:38 (lcondron-mac.acs)

4.1.6 whereis - report program locations

whereis reports the filenames of source, binary, and manual page files associated with comma

Syntax

whereis [options] command(s)

Common Options

-b report binary files only

-m report manual sections only

-s report source files only

Examples

brigadier: condron [69]> whereis Mail

Mail: /usr/ucb/Mail /usr/lib/Mail.help /usr/lib/Mail.rc /usr/man/man1/Mail.1

Introduction to Unix 1998 University Technology Services, The Ohio State University 37

Page 38: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

System Resources & Printing

ill be

.:

rsion.

things

brigadier: condron [70]> whereis -b Mail

Mail: /usr/ucb/Mail /usr/lib/Mail.help /usr/lib/Mail.rc

brigadier: condron [71]> whereis -m Mail

Mail: /usr/man/man1/Mail.1

4.1.7 which - report the command found

which will report the name of the file that is be executed when the command is invoked. This wthe full path name or the alias that’s found first in your path.

Syntax

which command(s)

example--

brigadier: condron [73]> which Mail

/usr/ucb/Mail

4.1.8 hostname/uname - name of machine

hostname (uname -n on SysV) reports the host name of the machine the user is logged into, e.g

brigadier: condron [91]> hostname

brigadier

uname has additional options to print information about system hardware type and software ve

4.1.9 script - record your screen I/O

script creates a script of your session input and output. Using thescript command, you can captureall the data transmission from and to your terminal screen until youexit the script program. This canbe useful during the programming-and-debugging process, to document the combination ofyou have tried, or to get a printed copy of it all for later perusal.

Syntax

script [-a] [file] <. . .> exit

Common Options

-a append the output to file

typescript is the name of the default file used byscript.

You must remember to typeexit to end your script session and close your typescript file.

38 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 39: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

System Resources

Examples

beauty condron>script

Script started, file is typescript

beauty condron>ps

PID TT STAT TIME COMMAND

23323 p8 S 0:00 -h -i (tcsh)

23327 p8 R 0:00 ps

18706 pa S 0:00 -tcsh (tcsh)

23315 pa T 0:00 emacs

23321 pa S 0:00 script

23322 pa S 0:00 script

3400 pb I 0:00 -tcsh (tcsh)

beauty condron>kill -9 23315

beauty condron>date

Mon Apr 22 22:29:44 EDT 1996

beauty condron>exit

exit

Script done, file is typescript

[1] + Killed emacs

beauty condron>cat typescript

Script started on Mon Apr 22 22:28:36 1996

beauty condron>ps

PID TT STAT TIME COMMAND

23323 p8 S 0:00 -h -i (tcsh)

23327 p8 R 0:00 ps

18706 pa S 0:00 -tcsh (tcsh)

23315 pa T 0:00 emacs

23321 pa S 0:00 script

23322 pa S 0:00 script

3400 pb I 0:00 -tcsh (tcsh)

beauty condron>kill -9 23315

beauty condron>date

Mon Apr 22 22:29:44 EDT 1996

beauty condron>exit

exit

script done on Mon Apr 22 22:30:02 1996

beauty condron>

Introduction to Unix 1998 University Technology Services, The Ohio State University 39

Page 40: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

System Resources & Printing

4.1.10 date - current date and time

date displays the current data and time. A superuser can set the date and time.

Syntax

date [options] [+format]

Common Options

-u use Universal Time (or Greenwich Mean Time)

+format specify the output format

%a weekday abbreviation, Sun to Sat

%h month abbreviation, Jan to Dec

%j day of year, 001 to 366

%n <new-line>

%t <TAB>

%y last 2 digits of year, 00 to 99

%D MM/DD/YY date

%H hour, 00 to 23

%M minute, 00 to 59

%S second, 00 to 59

%T HH:MM:SS time

Examples

beauty condron>date

Mon Jun 10 09:01:05 EDT 1996

beauty condron>date -u

Mon Jun 10 13:01:33 GMT 1996

beauty condron>date +%a%t%D

Mon 06/10/96

beauty condron>date '+%y:%j'

96:162

40 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 41: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Print Commands

ons,

job is

4.2 Print Commands

The print commands allow us to print files to standard output (pr) or to a line printer (lp/lpr) whilefiltering the output. TheBSD andSysV printer commands use different names and different optito produce the same results:lpr, lprm, andlpq vs lp, cancel, andlpstat for the BSD and SysV submitcancel, and check the status of a print job, respectively.

4.2.1 lp/lpr - submit a print job

lp andlpr submit the specified file, or standard input, to the printer daemon to be printed. Eachgiven a unique request-id that can be used to follow or cancel the job while it’s in the queue.

Syntax

lp [options] filename

lpr [options] filename

Common Options

lp lpr function

-n number -#number number of copies

-t title -Ttitle title for job

-d destination -Pprinter printer name

-c (default) copy file to queue before printing

(default) -s don’t copy file to queue before printing

-o option additional options, e.g. nobanner

Files beginning with the string "%! " are assumed to contain PostScript commands.

Examples

To print the file ssh.ps:

% lp ssh.ps

request id is lp-153 (1 file(s))

This submits the job to the queue for the default printer,lp, with the request-id lp-153.

TABLE 4.2 Printing Commands

Command/Syntax What it will do

lpq (lpstat) [options] show the status of print jobs

lpr (lp) [options]file print to defined printer

lprm (cancel) [options] remove a print job from the print queue

pr [options][file] filter the file and print it on the terminal

Introduction to Unix 1998 University Technology Services, The Ohio State University 41

Page 42: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

System Resources & Printing

4.2.2 lpstat/lpq - check the status of a print job

You can check the status of your print job with lpstat or lpq.

Syntax

lpstat [options]

lpq [options] [job#] [username]

Common Options

lpstat lpq function

-d (defaults to lp) list system default destination

-s summarize print status

-t print all status information

-u [login-ID-list] user list

-v list printers known to the system

-p printer_dest -Pprinter_dest list status of printer, printer_dest

Examples

% lpstat

lp-153 frank 208068 Apr 29 15:14 on lp

4.2.3 cancel/lprm - cancel a print job

Any user can cancel only heir own print jobs.

Syntax

cancel [request-ID] [printer]

lprm [options] [job#] [username]

Common Options

cancel lprm function

-Pprinter specify printer

- all jobs for user

-u [login-ID-list] user list

Examples

To cancel the job submitted above:

% cancel lp-153

42 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 43: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Print Commands

mberd trailer

lines

4.2.4 pr - prepare files for printing

pr prints header and trailer information surrounding the formatted file. You can specify the nuof pages, lines per page, columns, line spacing, page width, etc. to print, along with header aninformation and how to treat<tab> characters.

Syntax

pr [options] file

Common Options

+page_number start printing with page page_number of the formatted input file

-column number of columns

-a modify -column option to fill columns in round-robin order

-d double spacing

-e[char][gap] tab spacing

-h header_string header for each page

-l lines lines per page

-t don’t print the header and trailer on each page

-w width width of page

Examples

The file containing the list of P. G. Wodehouse’s Lord Emsworth books could be printed, at 14per page (including 5 header and 5 (empty) trailer lines) below, where the-e option specifies the<tab> conversion style:

% pr -l 14 -e42 wodehouse

Apr 29 11:11 1996 wodehouse_emsworth_books Page 1

Something Fresh [1915] Uncle Dynamite [1948]

Leave it to Psmith [1923] Pigs Have Wings [1952]

Summer Lightning [1929] Cocktail Time [1958]

Heavy Weather [1933] Service with a Smile [1961]

Introduction to Unix 1998 University Technology Services, The Ohio State University 43

Page 44: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

System Resources & Printing

Apr 29 11:11 1996 wodehouse_emsworth_books Page 2

Blandings Castle and Elsewhere [1935] Galahad at Blandings [1965]

Uncle Fred in the Springtime [1939] A Pelican at Blandings [1969]

Full Moon [1947] Sunset at Blandings [1977]

44 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 45: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Print Commands

ds youralogous shellths, and

or but is

in, buth a jobyou to

ell

rh. Some

sed

CHAPTER 5 Shells

The shell sits between you and the operating system, acting as a command interpreter. It reaterminal input and translates the commands into actions taken by the system. The shell is anto command.com in DOS. When you log into the system you are given a default shell. When thestarts up it reads its startup files and may set environment variables, command search pacommand aliases, and executes any commands specified in these files.

The original shell was the Bourne shell,sh. Every Unix platform will either have the Bourne shell, a Bourne compatible shell available. It has very good features for controlling input and output,not well suited for the interactive user. To meet the latter need the C shell,csh, was written and is nowfound on most, but not all, Unix systems. It uses C type syntax, the language Unix is written has a more awkward input/output implementation. It has job control, so that you can reattacrunning in the background to the foreground. It also provides a history feature which allows modify and repeat previously executed commands.

The default prompt for the Bourne shell is$ (or#, for the root user). The default prompt for the C shis % .

Numerous other shells are available from the network. Almost all of them are based on eithesh orcsh with extensions to provide job control tosh, allow in-line editing of commands, page througpreviously executed commands, provide command name completion and custom prompt, etcof the more well known of these may be on your favorite Unix system: the Korn shell,ksh, by DavidKorn and the Bourne Again SHell,bash, from the Free Software Foundations GNU project, both baonsh, the T-C shell,tcsh, and the extended C shell,cshe, both based oncsh. Below we will describesome of the features ofsh andcsh so that you can get started.

Introduction to Unix 1998 University Technology Services, The Ohio State University 45

Page 46: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shells

tly inent for

5.1 Built-in Commands

The shells have a number ofbuilt-in , or native commands. These commands are executed directhe shell and don’t have to call another program to be run. These built-in commands are differthe different shells.

5.1.1 Sh

For the Bourne shell some of the more commonly used built-in commands are:

: null command

. source (read and execute) commands from a file

case case conditional loop

cd change the working directory (default is $HOME)

echo write a string to standard output

eval evaluate the given arguments and feed the result back to the shell

exec execute the given command, replacing the current shell

exit exit the current shell

export share the specified environment variable with subsequent shells

for for conditional loop

if if conditional loop

pwd print the current working directory

read read a line of input from stdin

set set variables for the shell

test evaluate an expression as true or false

trap trap for a typed signal and execute commands

umask set a default file permission mask for new files

unset unset shell variables

wait wait for a specified process to terminate

while while conditional loop

46 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 47: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Built-in Commands

ctory

5.1.2 Csh

For the C shell the more commonly used built-in functions are:

alias assign a name to a function

bg put a job into the background

cd change the current working directory

echo write a string to stdout

eval evaluate the given arguments and feed the result back to the shell

exec execute the given command, replacing the current shell

exit exit the current shell

fg bring a job to the foreground

foreach for conditional loop

glob do filename expansion on the list, but no "\" escapes are honored

history print the command history of the shell

if if conditional loop

jobs list or control active jobs

kill kill the specified process

limit set limits on system resources

logout terminate the login shell

nicecommand lower the scheduling priority of the process,command

nohup command do not terminatecommand when the shell exits

popd pop the directory stack and return to that directory

pushd change to the new directory specified and add the current one to the direstack

rehash recreate the hash table of paths to executable files

repeat repeat a command the specified number of times

set set a shell variable

setenv set an environment variable for this and subsequent shells

source source (read and execute) commands from a file

stop stop the specified background job

switch switch conditional loop

umask set a default file permission mask for new files

unalias remove the specified alias name

unset unset shell variables

unsetenv unset shell environment variables

wait wait for all background processes to terminate

while while conditional loop

Introduction to Unix 1998 University Technology Services, The Ohio State University 47

Page 48: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shells

ve bothinriablesot pass a

pace

l/bin

efinet wantlly.

ns

5.2 Environment Variables

Environmental variables are used to provide information to the programs you use. You can haglobal environment andlocal shell variables. Global environment variables are set by your logshell and new programs and shells inherit the environment of their parent shell. Local shell vaare used only by that shell and are not passed on to other processes. A child process cannvariable back to its parent process.

The current environment variables are displayed with the "env" or "printenv" commands. Somecommon ones are:

• DISPLAY The graphical display to use, e.g. nyssa:0.0

• EDITOR The path to your default editor, e.g. /usr/bin/vi

• GROUP Your login group, e.g. staff

• HOME Path to your home directory, e.g. /home/frank

• HOST The hostname of your system, e.g. nyssa

• IFS Internal field separators, usually any white space (defaults to tab, sand <newline>)

• LOGNAME The name you login with, e.g. frank

• PATH Paths to be searched for commands, e.g. /usr/bin:/usr/ucb:/usr/loca

• PS1 The primary prompt string, Bourne shell only (defaults to $)

• PS2 The secondary prompt string, Bourne shell only (defaults to >)

• SHELL The login shell you’re using, e.g. /usr/bin/csh

• TERM Your terminal type, e.g. xterm

• USER Your username, e.g. frankMany environment variables will be set automatically when you login. You can modify them or dothers with entries in your startup files or at anytime within the shell. Some variables you mighto change arePATH andDISPLAY . ThePATH variable specifies the directories to be automaticasearched for the command you specify. Examples of this are in the shell startup scripts below

You set aglobal environment variable with a command similar to the following for the C shell:

% setenv NAME valueand for Bourne shell:

$ NAME=value; export NAMEYou can list your global environmental variables with theenv orprintenv commands. You unset themwith theunsetenv (C shell) orunset (Bourne shell) commands.

To set alocal shell variable use theset command with the syntax below for C shell. Without optioset displays all the local variables.

% set name=valueFor the Bourne shell set the variable with the syntax:

$ name=value

The current value of the variable is accessed via the "$name", or "${name}", notation.

48 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 49: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

The Bourne Shell, sh

file,

sectory

am

ith

es it

ies

5.3 The Bourne Shell, sh

Sh uses the startup file.profile in your home directory. There may also be a system-wide startupe.g./etc/profile. If so, the system-wide one will be sourced (executed) before your local one.

A simple.profile could be the following:

PATH=/usr/bin:/usr/ucb:/usr/local/bin:. # set the PATH

export PATH # so that PATH is available to subshells

# Set a prompt

PS1="{`hostname` `whoami`} " # set the prompt, default is "$"

# functions

ls() { /bin/ls -sbF "$@";}

ll() { ls -al "$@";}

# Set the terminal type

stty erase ^H # set Control-H to be the erase key

eval `tset -Q -s -m ':?xterm'` # prompt for the terminal type, assume xterm

#

umask 077Whenever a# symbol is encountered the remainder of that line is treated as a comment. In thePATHvariable each directory is separated by a colon (:) and the dot (.) specifies that the current directory iin your path. If the latter is not set it’s a simple matter to execute a program in the current dirby typing:

./program_nameIt’s actually a good idea not to have dot (.) in your path, as you may inadvertently execute a progryou didn’t intend to when youcd to different directories.

A variable set in.profile is set only in the login shell unless you "export" it or source .profile fromanother shell. In the above examplePATH is exported to any subshells. You can source a file wthe built-in "." command ofsh, i.e.:

. ./.profileYou can make your own functions. In the above example the functionll results in an "ls -al" beingdone on the specified files or directories.

With stty the erase character is set to Control-H (^H), which is usually the Backspace key.

The tset command prompts for the terminal type, and assumes "xterm" if we just hit <CR>. Thiscommand is run with the shell built-in,eval, which takes the result from the tset command and usas an argument for the shell. In this case the "-s" option to tset sets theTERM andTERMCAPvariables and exports them.

The last line in the example runs theumask command with the option such that any files or directoryou create will not have read/write/execute permission forgroup andother.

For further information aboutsh type "man sh" at the shell prompt.

Introduction to Unix 1998 University Technology Services, The Ohio State University 49

Page 50: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shells

.g.

e

,

ng

ins

ve)

ge

5.4 The C Shell, csh

Csh uses the startup files.cshrc and .login. Some versions use a system-wide startup file, e/etc/csh.login. Your.login file is sourced (executed) only when you login. Your.cshrc file is sourcedevery time you start acsh, including when you login. It has many similar features to.profile, but adifferent style of doing things. Here we use theset or setenv commands to initialize a variable, wherset is used for this shell andsetenv for this and any subshells. The environment variables:USER,TERM , andPATH , are automatically imported to and exported from theuser, term, andpathvariables of thecsh. Sosetenv doesn’t need to be done for these. The C shell uses the symbol,~, toindicate the user’s home directory in a path, as in~/.cshrc, or to specify another user’s login directoryas in~username/.cshrc.

Predefined variables used by the C shell include:

• argv The list of arguments of the current shell

• cwd The current working directory

• history Sets the size of the history list to save

• home The home directory of the user; starts with $HOME

• ignoreeof When set ignore EOF (^D) from terminals

• noclobber When set prevent output redirection from overwriting existing files

• noglob When set prevent filename expansion with wildcard pattern matchi

• path The command search path; starts with $PATH

• prompt Set the command line prompt (default is %)

• savehist number of lines to save in the history list to save in the .history file

• shell The full pathname of the current shell; starts with $SHELL

• status The exit status of the last command (0=normal exit, 1=failedcommand)

• term Your terminal type, starts with $TERM

• user Your username, starts with $USERA simple.cshrc could be:

set path=(/usr/bin /usr/ucb /usr/local/bin ~/bin . ) # set the path

set prompt = "{‘hostname‘ ‘whoami‘ !} " # set the primary prompt; default is "%"

set noclobber # don’t redirect output to existing files

set ignoreeof # ignore EOF (^D) for this shell

set history=100 savehist=50 # keep a history list and save it between log

# aliases

alias h history # alias h to "history"

alias ls "/usr/bin/ls -sbF" # alias ls to "ls -sbF"

alias ll ls -al # alias ll to "ls -sbFal" (combining these options with those for "ls" abo

alias cd ’cd \!*;pwd’ # alias cd so that it prints the current working directory after the chan

umask 077

50 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 51: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Job Control

, with

hanged

t jobs

are noinal,direct

o the

Some new features here that we didn’t see in.profile are noclobber, ignoreeof, and history.Noclobber indicates that output will not be redirected to existing files, whileignoreeof specifies thatEOF (^D) will not cause the login shell to exit and log you off the system.

With the history feature you can recall previously executed commands and re-execute themchanges if desired.

An alias allows you to use the specifiedalias name instead of the full command. In the "ls" exampleabove, typing "ls" will result in "/usr/bin/ls -sbF" being executed. You can tell which "ls" commandis in your path with the built-inwhich command, i.e.:

which ls

ls: aliased to /usr/bin/ls -sbF

A simple.login could be:

# .login

stty erase ^H # set Control-H to be the erase key

set noglob # prevent wild card pattern matching

eval ‘tset -Q -s -m ’:?xterm’‘ # prompt for the terminal type, assume "xterm"

unset noglob # re-enable wild card pattern matching

Setting and unsettingnoglob aroundtset prevents it from being confused by anycsh filename wild cardpattern matching or expansion.

Should you make any changes to your startup files you can initiate the change by sourcing the cfile. Forcsh you do this with the built-insource command, i.e.:

source .cshrc

For further information about csh type "man csh" at the shell prompt.

5.5 Job Control

With the C shell,csh, and many newer shells including some newer Bourne shells, you can puinto the background at anytime by appending "& " to the command, as withsh. After submitting acommand you can also do this by typing^Z (Control-Z) to suspend the job and then "bg" to put it intothe background. To bring it back to the foreground type "fg".

You can have many jobs running in the background. When they are in the background theylonger connected to the keyboard for input, but they may still display output to the terminterspersing with whatever else is typed or displayed by your current job. You may want to reI/O to or from files for the job you intend to background. Your keyboard is connected only tcurrent, foreground, job.

The built-injobs command allows you to list your background jobs. You can use thekill command tokill a background job. With the%n notation you can reference thenth background job with either ofthese commands, replacingn with the job number from the output ofjobs. So kill the secondbackground job with "kill %2" and bring the third job to the foreground with "fg %3".

Introduction to Unix 1998 University Technology Services, The Ohio State University 51

Page 52: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shells

former. Here

nds put aough

5.6 History

The C shell, the Korn shell and some other more advanced shells, retain information about thecommands you’ve executed in the shell. How history is done will depend on the shell usedwe’ll describe the C shell history features.

You can use thehistory andsavehist variables to set the number of previously executed commato keep track of in this shell and how many to retain between logins, respectively. You couldline such as the following in.cshrc to save the last 100 commands in this shell and the last 50 thrthe next login.

set history=100 savehist=50The shell keeps track of the history list and saves it in~/.history between logins.

You can use the built-inhistory command to recall previous commands, e.g. to print the last 10:

% history 10

52 cd workshop

53 ls

54 cd unix_intro

55 ls

56 pwd

57 date

58 w

59 alias

60 history

61 history 10

You can repeat the last command by typing!! :

% !!

53 ls

54 cd unix_intro

55 ls

56 pwd

57 date

58 w

59 alias

60 history

61 history 10

62 history 10

52 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 53: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

History

with a

rpretsyou willitution

You can repeat any numbered command by prefacing the number with a!, e.g.:

% !57

date

Tue Apr 9 09:55:31 EDT 1996

Or repeat a command starting with any string by prefacing the starting unique part of the string!, e.g.:

% !da

date

Tue Apr 9 09:55:31 EDT 1996

When the shell evaluates the command line it first checks for history substitution before it inteanything else. Should you want to use one of these special characters in a shell command need to escape, or quote it first, with a \ before the character, i.e. \!. The history substcharacters are summarized in the following table.

Additional editing modifiers are described in theman page.

TABLE 5.1 C Shell History Substitution

Command Substitution Function

!! repeat last command

!n repeat command numbern

!-n repeat command n from last

!str repeat command that started with stringstr

!?str? repeat command withstr anywhere on the line

!?str?% select the first argument that hadstr in it

!: repeat the last command, generally used with a modifier

!:n select thenth argument from the last command (n=0 is the command name)

!:n-m select thenth throughmth arguments from the last command

!^ select the first argument from the last command (same as !:1)

!$ select the last argument from the last command

!* select all arguments to the previous command

!:n* select thenth through last arguments from the previous command

!:n- select thenth through next to last arguments from the previous command

^str1^str2^ replacestr1 with str2 in its first occurrence in the previous command

!n:s/str1/str2/ substitutestr1 with str2 in its first occurrence in thenth command, ending with agsubstitute globally

Introduction to Unix 1998 University Technology Services, The Ohio State University 53

Page 54: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shells

e

th your

ailableally,

nd tcsh.houldle on thelenly

e shell

5.7 Changing your Shell

To change your shell you can usually use the "chsh" or "passwd -e" commands. The option flag, her-e, may vary from system to system (-s on BSD based systems), so check theman page on your systemfor proper usage. Sometimes this feature is disabled. If you can’t change your shell check wiSystem Administrator.

The new shell must be the full path name for a valid shell on the system. Which shells are avto you will vary from system to system. The full path name of a shell may also vary. Normthough, the Bourne and C shells are standard, and available as:

/bin/sh

/bin/cshSome systems will also have the Korn shell standard, normally as:

/bin/kshSome shells that are quite popular, but not normally distributed by the OS vendors are bash aThese might be placed in /bin or a locally defined directory, e.g. /usr/local/bin or /opt/local/bin. Syou choose a shell not standard to the OS make sure that this shell, and all login shells availabsystem, are listed in the file/etc/shells. If this file exists and your shell is not listed in this file the fitransfer protocol daemon,ftpd, will not let you connect to this machine. If this file does not exist oaccounts with "standard" shells are allowed to connect viaftp .

You can always try out a shell before you set it as your default shell. To do this just type in thname as you would any other command.

54 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 55: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

File Descriptors

ion ofd way

rms: I/O

inalard.

e discuss

o the

9, and

rections.

CHAPTER 6 Special Unix Features

One of the most important contributions Unix has made to Operating Systems is the provismany utilities for doing common tasks or obtaining desired information. Another is the standarin which data is stored and transmitted in Unix systems. This allows data to be transmittedto a file,the terminal screen, or a program, orfrom a file, the keyboard, or a program; always in a unifomanner. The standardized handling of data supports two important features of Unix utilitieredirection and piping.

With output redirection, the output of a command is redirected to a file rather than to the termscreen. Withinput redirection , the input to a command is given via a file rather than the keyboOther tricks are possible with input and output redirection as well, as you will see. Withpiping, theoutput of a command can be used as input (piped) to a subsequent command. In this chapter wmany of the features and utilities available to Unix users.

6.1 File Descriptors

There are 3 standard file descriptors:

• stdin 0 Standard input to the program

• stdout 1 Standard output from the program

• stderr 2 Standard error output from the program

Normally input is from the keyboard or a file. Output, both stdout and stderr, normally go tterminal, but you can redirect one or both of these to one or more files.

You can also specify additional file descriptors, designating them by a number 3 through redirect I/O through them.

6.2 File Redirection

Output redirection takes the output of a command and places it into a named file. Input redireads the file as input to the command. The following table summarizes the redirection option

Introduction to Unix 1998 University Technology Services, The Ohio State University 55

Page 56: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Special Unix Features

re the

ll, as in:

.

An example of output redirection is:

cat file1 file2 > file3The above command concatenatesfile1 thenfile2 and redirects (sends) the output tofile3. If file3doesn't already exist it is created. If it does exist it will either be truncated to zero length befonew contents are inserted, or the command will be rejected, if thenoclobber option of thecsh is set.(See thecsh in Chapter 4). The original files,file1 andfile2, remain intact as separate entities.

Output is appended to a file in the form:

cat file1 >> file2This command appends the contents offile1 to the end of what already exists infile2. (Does notoverwritefile2).

Input is redirected from a file in the form:

program < fileThis command takes the input forprogramfrom file.

To pipe output to another command use the form:

command | command

This command makes the output of the first command the input of the second command.

6.2.1 Csh

>& file redirect stdout and stderr tofile

>>& append stdout and stderr tofile

|& command pipe stdout and stderr tocommand

To redirect stdout and stderr to two separate files you need to first redirect stdout in a sub-she

% (command > out_file) >& err_file

TABLE 6.1 File Redirection

Symbol Redirection

> output redirect

>! same as above, but overridesnoclobber option ofcsh

>> append output

>>! same as above, but overridesnoclobber option oncsh and creates the file ifit doesn’t already exist.

| pipe output to another command

< input redirection

<<String read from standard input until "String" is encountered as the only thing on the lineAlso known as a "here document" (see Chapter 8).

<<\String same as above, but don’t allow shell substitutions

56 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 57: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

File Redirection

rough

orll first

eversed:

6.2.2 Sh

2> file direct stderr tofile

> file 2>&1 direct both stdout and stderr tofile

>> file 2>&1 append both stdout and stderr tofile

2>&1 | command pipe stdout and stderr tocommand

To redirect stdout and stderr to two separate files you can do:

$ command 1> out_file 2> err_file

or, since the redirection defaults to stdout:

$ command > out_file 2> err_file

With the Bourne shell you can specify other file descriptors (3 through 9) and redirect output ththem. This is done with the form:

n>&m redirect file descriptorn to file descriptorm

We used the above to send stderr (2) to the same place as stdout (1),2>&1, when we wanted to haveerror messages and normal messages to go tofile instead of the terminal. If we wanted only the errmessages to go to the file we could do this by using a place holder file descriptor, 3. We’redirect 3 to 2, then redirect 2 to 1, and finally, we’ll redirect 1 to 3:

$ (command 3>&2 2>&1 1>&3) > file

This sends stderr to 3 then to 1, and stdout to 3, which is redirected to 2. So, in effect, we’ve rfile descriptors 1 and 2 from their normal meaning. We might use this in the following example

$ (cat file 3>&2 2>&1 1>&3) > errfile

So if file is read the information is discarded from the command output, but iffile can’t be read theerror message is put inerrfile for your later use.

You can close file descriptors when you’re done with them:

m<&- closes an input file descriptor

<&- closes stdin

m>&- closes an output file descriptor

>&- closes stdout

Introduction to Unix 1998 University Technology Services, The Ohio State University 57

Page 58: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Special Unix Features

e on a

te

utput

rs

e

ng

6.3 Other Special Command Symbols

In addition to file redirection symbols there are a number of other special symbols you can uscommand line. These include:

; command separator

& run the command in the background

&& run the command following this only if the previous command completessuccessfully, e.g.:grepstring file && cat file

|| run the command following only if the previous command did not complesuccessfully, e.g.:

grep string file || echo "String not found."

( ) the commands within the parentheses are executed in a subshell. The oof the subshell can be manipulated as above.

’ ’ literal quotation marks. Don’t allow any special meaning to any charactewithin these quotations.

\ escape the following character (take it literally)

" " regular quotation marks. Allow variable and command substitution withtheses quotations (does not disable$ and\ within the string).

‘command‘ take the output of this command and substitute it as an argument(s) on thcommand line

# everything following until <newline> is a comment

The \ character can also be used to escape the<newline> character so that you can continue a locommand on more than one physical line of text.

6.4 Wild Cards

The shell and some text processing programs will allowmeta-characters, orwild cards, and replacethem with pattern matches. For filenames thesemeta-characters and their uses are:

? match any single character at the indicated position

* match any string of zero or more characters

[abc...] match any of the enclosed characters

[a-e] match any characters in the range a,b,c,d,e

[!def] match any characters not one of the enclosed characters,sh only

{abc,bcd,cde} match any set of characters separated by comma (,) (no spaces),csh only

~ home directory of the current user,csh only

~user home directory of the specified user,csh only

58 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 59: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Regular Expression Syntax

s

y only

ning

ns on

rd.

s.

CHAPTER 7 Text Processing

7.1 Regular Expression Syntax

Some text processing programs, such asgrep, egrep, sed, awk andvi, let you search on patterninstead of fixed strings. These text patterns are known asregular expressions. You form a regularexpression by combining normal characters and special characters, also known asmeta-characters,with the rules below. With these regular expressions you can dopattern matching on text data.Regular expressions come in three different forms:

• Anchors which tie the pattern to a location on the line

• Character sets which match a character at a single position

• Modifiers which specify how many times to repeat the previous expression

Regular expression syntax is as follows. Some programs will accept all of these, others maaccept some.

. matchany single character except <newline>

* matchzero or more instances of the single character (or meta-character)immediately preceding it

[abc] match any of the characters enclosed

[a-d] match any character in the enclosed range

[^exp] match any characternot in the following expression

^abc the regular expression must start at thebeginning of the line(Anchor)

abc$ the regular expression must end at theend of the line(Anchor)

\ treat the next character literally. This is normally used to escape the meaof special characters such as "." and "* ".

\{n,m\} match the regular expression preceding this a minimum number ofn timesand a maximum ofm times (0 through 255 are allowed for n and m). The\{and\} sets should be thought of as single operators. In this case the\preceding the bracket does not escape its special meaning, but rather tura new one.

\<abc\> will match the enclosed regular expression as long as it is a separate woWord boundaries are defined as beginning with a <newline> or anythingexcept a letter, digit or underscore (_) or ending with the same or a end-of-linecharacter. Again the\< and\> sets should be thought of as single operator

Introduction to Unix 1998 University Technology Services, The Ohio State University 59

Page 60: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Text Processing

for

\(abc\) saves the enclosed pattern in a buffer. Up to nine patterns can be savedeach line. You can reference these latter with the\n character set. Again the\( and\) sets should be thought of as single operators.

\n wheren is between 1 and 9. This matches the nth expression previouslysaved for this line. Expressions are numbered starting from the left. The\nshould be thought of as a single operator.

& print the previous search pattern (used in the replacement string)

There are a few meta-characters used only byawk andegrep. These are:

+ match one or more of the preceding expression

? match zero or more of the preceding expression

| separator. Match either the preceding or following expression.

( ) group the regular expressions within and apply the match to the set.

Some examples of the more commonly usedregular expressions are:

regular

expression matches

cat the stringcat

.at any occurrence of a letter, followed byat, such as cat, rat, mat, bat, fat, hat

xy*z any occurrence of anx, followed by zero or morey's, followed by az.

^cat cat at the beginning of the line

cat$ cat at the end of the line

\* any occurrence of an asterisk

[cC]at cat or Cat

[^a-zA-Z] any occurrence of a non-alphabetic character

[0-9]$ any line ending with a number

[A-Z][A-Z]* one or more upper case letters

[A-Z]* zero or more upper case letters (In other words, anything.)

60 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 61: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Text Processing Commands

gulares) whent of are

s

e

7.2 Text Processing Commands

7.2.1 grep

This section provides an introduction to the use ofregular expressions andgrep.

Thegrep utility is used to search for generalized regular expressions occurring in Unix files. Reexpressions, such as those shown above, are best specified in apostrophes (or single quotspecified in thegrep utility. Theegrep utility provides searching capability using an extended semeta-characters. The syntax of thegrep utility, some of the available options, and a few examplesshown below.

Syntax

grep [options] regexp [file[s]]

Common Options

-i ignore case

-c report only a count of the number of lines containing matches, not thematches themselves

-v invert the search, displaying only lines that do not match

-n display the line number along with the line on which a match was found

-s work silently, reporting only the final status:

0, for match(es) found

1, for no matches

2, for errors

-l list filenames, but not lines, in which matches were found

TABLE 7.1 Text Processing Commands

Command/Syntax What it will do

awk/nawk [options]file scan for patterns in a file and process the results

grep/egrep/fgrep [options] 'search string'file search the argument (in this case probably a file) for all occurrenceof the search string, and list them.

sed [options]file stream editor for editing files from a script or from the command lin

Introduction to Unix 1998 University Technology Services, The Ohio State University 61

Page 62: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Text Processing

cter

Examples

Consider the following file:

{unix prompt 5} cat num.list

1 15 fifteen

2 14 fourteen

3 13 thirteen

4 12 twelve

5 11 eleven

6 10 ten

7 9 nine

8 8 eight

9 7 seven

10 6 six

11 5 five

12 4 four

13 3 three

14 2 two

15 1 one

Here are somegrep examples using this file. In the first we’ll search for the number15:

{unix prompt 6} grep '15' num.list

1 15 fifteen

15 1 one

Now we’ll use the "-c" option to count the number of lines matching the search criterion:

{unix prompt 7} grep -c '15' num.list

2

Here we’ll be a little more general in our search, selecting for all lines containing the chara1followed by either of1, 2 or 5:

{unix prompt 8} grep '1[125]' num.list

1 15 fifteen

4 12 twelve

5 11 eleven

11 5 five

12 4 four

15 1 one

62 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 63: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Text Processing Commands

Now we’ll search for all lines thatbegin with aspace:

{unix prompt 9} grep '^ ' num.list

1 15 fifteen

2 14 fourteen

3 13 thirteen

4 12 twelve

5 11 eleven

6 10 ten

7 9 nine

8 8 eight

9 7 seven

Or all lines thatdon’t begin with aspace:

{unix prompt 10} grep '^[^ ]' num.list

10 6 six

11 5 five

12 4 four

13 3 three

14 2 two

15 1 one

The latter could also be done by using the-v option with the original search string, e.g.:

{unix prompt 11} grep -v '^ ' num.list

10 6 six

11 5 five

12 4 four

13 3 three

14 2 two

15 1 one

Here we search for all lines thatbegin with the characters1 through 9:

{unix prompt 12} grep '^[1-9]' num.list

10 6 six

11 5 five

12 4 four

13 3 three

14 2 two

15 1 one

Introduction to Unix 1998 University Technology Services, The Ohio State University 63

Page 64: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Text Processing

tput by

This example will search for any instances oft followed byzero or more occurrences ofe:

{unix prompt 13} grep 'te*' num.list

1 15 fifteen

2 14 fourteen

3 13 thirteen

4 12 twelve

6 10 ten

8 8 eight

13 3 three

14 2 two

This example will search for any instances oft followed byone or more occurrences ofe:

{unix prompt 14} grep 'tee*' num.list

1 15 fifteen

2 14 fourteen

3 13 thirteen

6 10 ten

We can also take our input from a program, rather than a file. Here we report on any lines outhewho program that begin with the letterl.

{unix prompt 15} who | grep '^l'

lcondron ttyp0 Dec 1 02:41 (lcondron-pc.acs.)

64 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 65: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Text Processing Commands

ied

bs. The use.

nted files.

neent

Theseshes (/),line>.eanings

d in

.

7.2.2 sed

The non-interactive, stream editor,sed, edits the input stream, line by line, making the specifchanges, and sends the result to standard output.

Syntax

sed [options] edit_command [file]

The format for the editing commands are:

[address1[,address2]][function][arguments]

where the addresses are optional and can be separated from the function by spaces or tafunction is required. The arguments may be optional or required, depending on the function in

Line-number Addresses are decimal line numbers, starting from the first input line and incremeby one for each. If multiple input files are given the counter continues cumulatively through theThe last input line can be specified with the "$" character.

Context Addresses are the regular expression patterns enclosed in slashes (/).

Commands can have 0, 1, or 2 comma-separated addresses with the following affects:

# of addresses lines affected

0 every line of input

1 only lines matching the address

2 first line matching the first address and all lines until, and including, the limatching the second address. The process is then repeated on subsequlines.

Substitution functions allow context searches and are specified in the form:

s/regular_expression_pattern/replacement_string/flag

and should be quoted with single quotes (’) if additional options or functions are specified. patterns are identical to context addresses, except that while they are normally enclosed in slaany normal character is allowed to function as the delimiter, other than <space> and <newThe replacement string is not a regular expression pattern; characters do not have special mhere, except:

& substitute the string specified by regular_expression_pattern

\n substitute the nth string matched by regular_expression_pattern enclose’ \(’, ’ \)’ pairs.

These special characters can be escaped with a backslash (\) to remove their special meaning

Introduction to Unix 1998 University Technology Services, The Ohio State University 65

Page 66: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Text Processing

these

one

Common Options

-e script edit script

-n don’t print the default output, but only those lines specified by p or s///pfunctions

-f script_file take the edit scripts from the file, script_file

Valid flags on the substitution functions include:

d delete the pattern

g globally substitute the pattern

p print the line

Examples

This example changes all incidents of a comma (,) into a comma followed by a space (, ) when doingoutput:

% cat filey | sed s/,/,\ /g

The following example removes all incidents ofJr preceded by a space ( Jr) in filey:

% cat filey | sed s/\ Jr//g

To perform multiple operations on the input precede each operation with the-e (edit) option andquote the strings. For example, to filter for lines containing "Date: " and "From: " and replacewithout the colon (:), try:

sed -e ’s/Date: /Date /’ -e ’s/From: /From /’

To print only those lines of the file from the one beginning with "Date:" up to, and including, thebeginning with "Name:" try:

sed -n ’/^Date:/,/^Name:/p’

To print only the first 10 lines of the input (a replacement forhead):

sed -n 1,10p

66 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 67: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Text Processing Commands

he three

is afor all

of the

on aked.

to to

read,

ssions,ith the

e

7.2.3 awk, nawk, gawk

awk is a pattern scanning and processing language. Its name comes from the last initials of tauthors: Alfred. V. Aho, Brian. W. Kernighan, and Peter. J. Weinberger.nawk is new awk, a newerversion of the program, andgawk is gnu awk, from the Free Software Foundation. Each version little different. Here we’ll confine ourselves to simple examples which should be the same versions. On some OSsawk is reallynawk.

awk searches its input for patterns and performs the specified operation on each line, or fieldsline, that contain those patterns. You can specify the pattern matching statements forawk either onthe command line, or by putting them in a file and using the-f program_file option.

Syntax

awk program [file]

whereprogram is composed of one or more:

pattern { action }

fields. Each input line is checked for a pattern match with the indicated action being takenmatch. This continues through the full sequence of patterns, then the next line of input is chec

Input is divided intorecords andfields. The defaultrecord separator is<newline>, and the variableNR keeps the record count. The defaultfield separator is whitespace,spaces and tabs, and thevariableNF keeps the field count. Input field,FS, and record,RS, separators can be set at any timematch any single character. Output field,OFS, and record,ORS, separators can also be changedany single character, as desired.$n, wheren is an integer, is used to represent thenth field of theinput record, while$0 represents the entire input record.

BEGIN andEND are special patterns matching the beginning of input, before the first field is and the end of input, after the last field is read, respectively.

Printing is allowed through theprint, and formatted print,printf , statements.

Patterns may be regular expressions, arithmetic relational expressions, string-valued expreand boolean combinations of any of these. For the latter the patterns can be combined wboolean operators below, using parentheses to define the combination:

|| or

&& and

! not

Comma separated patterns define therange for which the pattern is applicable, e.g.:

/first/,/last/

selects all lines starting with the one containingfirst , and continuing inclusively, through the oncontaininglast.

Introduction to Unix 1998 University Technology Services, The Ohio State University 67

Page 68: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Text Processing

ith the

field

to be In the

To select lines 15 through 20 use the pattern range:

NR == 15, NR == 20

Regular expressions must be enclosed with slashes (/) and meta-characters can be escaped wbackslash (\). Regular expressions can be grouped with the operators:

| or, to separate alternatives

+ one or more

? zero or one

A regular expression match can be either of:

~ contains the expression

!~ does not contain the expression

So the program:

$1 ~ /[Ff]rank/

is true if the first field, $1, contains "Frank" or "frank" anywhere within the field. To match a identical to "Frank" or "frank" use:

$1 ~ /^[Ff]rank$/

Relational expressionsare allowed using the relational operators:

< less than

<= less than or equal to

== equal to

>= greater than or equal to

!= not equal to

> greater than

Offhand you don’t know if variables are strings or numbers. If neither operand is known numeric, than string comparisons are performed. Otherwise, a numeric comparison is done.absence of any information to the contrary, a string comparison is done, so that:

$1 > $2

will compare the string values. To ensure a numerical comparison do something similar to:

( $1 + 0 ) > $2

Themathematical functions: exp, log and sqrt are built-in.

68 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 69: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Text Processing Commands

Some otherbuilt-in functions include:

index(s,t) returns the position of strings wheret first occurs, or 0 if it doesn’t

length(s) returns the length of strings

substr(s,m,n) returns then-character substring ofs, beginning at positionm

Arrays are declared automatically when they are used, e.g.:

arr[i] = $1

assigns the first field of the current input record to the ith element of the array.

Flow control statements usingif-else, while, andfor are allowed withC type syntax:

for (i=1; i <= NF; i++) {actions}

while (i<=NF) {actions}

if (i<NF) {actions}

Common Options

-f program_file read the commands from program_file

-Fc use characterc as the field separator character

Examples

% cat filex | tr a-z A-Z | awk -F: '{printf ("7R %-6s %-9s %-24s \n",$1,$2,$3)}'>upload.file

cats filex, which is formatted as follows:

nfb791:99999999:smith

7ax791:999999999:jones

8ab792:99999999:chen

8aa791:999999999:mcnulty

changes all lower case characters to upper case with thetr utility, and formats the file into thefollowing which is written into the fileupload.file:

7R NFB791 99999999 SMITH

7R 7AX791 999999999 JONES

7R 8AB792 99999999 CHEN

7R 8AA791 999999999 MCNULTY

Introduction to Unix 1998 University Technology Services, The Ohio State University 69

Page 70: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

and

or

ut

CHAPTER 8 Other Useful Commands

8.1 Working With Files

This section will describe a number of commands that you might find useful in examiningmanipulating the contents of your files.

TABLE 8.1 File utilities

Command/Syntax What it will do

cmp [options]file1 file2 compare two files and list where differences occur (text or binary files)

cut [options] [file(s)] cut specified field(s)/character(s) from lines in file(s)

diff [options]file1 file2 compare the two files and display the differences (text files only)

file [options]file classify the file type

find directory [options] [actions] find files matching a type or pattern

ln [options]source_file target link thesource_file to thetarget

paste [options]file paste field(s) onto the lines infile

sort [options]file sort the lines of thefile according to the options chosen

strings [options]file report any sequence of 4 or more printable characters ending in <NL> <NULL>. Usually used to search binary files for ASCII strings.

tee [options]file copy stdout to one or more files

touch [options] [date]file create an empty file, or update the access time of an existing file

tr [options]string1 string2 translate the characters in string1 from stdin into those in string2 in stdo

uniq [options]file remove repeated lines in a file

wc [options] [file(s)] display word (or character or line) count forfile(s)

70 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 71: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Working With Files

firstoes a

e than

8.1.1 cmp - compare file contents

The cmp command compares two files, and (without options) reports the location of thedifference between them. It can deal with both binary and ASCII file comparisons. It dbyte-by-byte comparison.

Syntax

cmp [options] file1 file2 [skip1] [skip2]

Theskip numbers are the number of bytes to skip in each file before starting the comparison.

Common Options

-l report on each difference

-s report exit status only, not byte differences

Examples

Given the files mon.logins:and tues.logins:

ageorge ageorge

bsmith cbetts

cbetts jchen

jchen jdoe

jmarsch jmarsch

lkeres lkeres

mschmidt proy

sphillip sphillip

wyepp wyepp

The comparison of the two files yields:

% cmp mon.logins tues.logins

mon.logins tues.logins differ: char 9, line 2

The default it to report only the first difference found.

This command is useful in determining which version of a file should be kept when there is morone version.

Introduction to Unix 1998 University Technology Services, The Ohio State University 71

Page 72: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

two. Itonvert

in the

8.1.2 diff - differences in files

Thediff command compares two files, directories, etc, and reports all differences between the deals only with ASCII files. It’s output format is designed to report the changes necessary to cthe first file into the second.

Syntax

diff [options] file1 file2

Common Options

-b ignore trailing blanks

-i ignore the case of letters

-w ignore <space> and <tab> characters

-e produce an output formatted for use with the editor,ed

-r apply diff recursively through common sub-directories

Examples

For the mon.logins and tues.logins files above,the difference between them is given by:

% diff mon.logins tues.logins

2d1

< bsmith

4a4

> jdoe

7c7

< mschmidt

---

> proy

Note that the output lists the differences as well as in which file the difference exists. Linesfirst file are preceded by "< ", and those in the second file are preceded by "> ".

72 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 73: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Working With Files

paratedsith theust be to the

ly

8.1.3 cut - select parts of a line

Thecut command allows a portion of a file to be extracted for another use.

Syntax

cut [options] file

Common Options

-c character_list character positions to select (first character is 1)

-d delimiter field delimiter (defaults to <TAB>)

-f field_list fields to select (first field is 1)

Both the character and field lists may contain comma-separated or blank-character-senumbers (in increasing order), and may contain a hyphen (-) to indicate a range. Any numbermissing at either before (e.g. -5) or after (e.g. 5-) the hyphen indicates the full range starting wfirst, or ending with the last character or field, respectively. Blank-character-separated lists menclosed in quotes. The field delimiter should be enclosed in quotes if it has special meaningshell, e.g. when specifying a <space> or <TAB> character.

Examples

In these examples we will use the fileusers:

jdoe John Doe 4/15/96

lsmith Laura Smith 3/12/96

pchen Paul Chen 1/5/96

jhsu Jake Hsu 4/17/96

sphilip Sue Phillip 4/2/96

If you only wanted the username and the user's real name, thecut command could be used to get onthat information:

% cut -f 1,2 users

jdoe John Doe

lsmith Laura Smith

pchen Paul Chen

jhsu Jake Hsu

sphilip Sue Phillip

Introduction to Unix 1998 University Technology Services, The Ohio State University 73

Page 74: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

e

n the

h as a

ut.

The cut command can also be used with other options. The-c option allows characters to be thselected cut. To select the first 4 characters:

% cut -c 1-4 users

This yields:

jdoe

lsmi

pche

jhsu

sphi

thus cutting out only the first 4 characters of each line.

8.1.4 paste - merge files

Thepaste command allows two files to be combined side-by-side. The default delimiter betweecolumns in a paste is a tab, but options allow other delimiters to be used.

Syntax

paste [options] file1 file2

Common Options

-d list list of delimiting characters

-s concatenate lines

The list ofdelimiters may include a single character such as a comma; a quoted string, sucspace; or any of the following escape sequences:

\n <newline> character

\t <tab> character

\\ backslash character

\0 empty string (non-null character)

It may be necessary to quote delimiters with special meaning to the shell.

A hyphen (-) in place of a file name is used to indicate that field should come from standard inp

74 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 75: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Working With Files

ract the

ly,

Examples

Given the fileusers:

jdoe John Doe 4/15/96

lsmith Laura Smith 3/12/96

pchen Paul Chen 1/5/96

jhsu Jake Hsu 4/17/96

sphilip Sue Phillip 4/2/96

and the filephone:

John Doe 555-6634

Laura Smith 555-3382

Paul Chen 555-0987

Jake Hsu 555-1235

Sue Phillip 555-7623

thepaste command can be used in conjunction with thecut command to create a new file,listing, thatincludes the username, real name, last login, and phone number of all the users. First, extphone numbers into a temporary file,temp.file:

% cut -f2 phone > temp.file

555-6634

555-3382

555-0987

555-1235

555-7623

The result can then be pasted to the end of each line inusers and directed to the new file,listing:

% paste users temp.file > listing

jdoe John Doe 4/15/96 237-6634

lsmith Laura Smith 3/12/96 878-3382

pchen Paul Chen 1/5/96 888-0987

jhsu Jake Hsu 4/17/96 545-1235

sphilip Sue Phillip 4/2/96 656-7623

This could also have been done on one line without the temporary file as:

% cut -f2 phone | paste users - > listing

with the same results. In this case the hyphen (-) is acting as a placeholder for an input field (namethe output of thecut command).

Introduction to Unix 1998 University Technology Services, The Ohio State University 75

Page 76: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

e/time onile (forime a

8.1.5 touch - create a file

The touch command can be used to create a new (empty) file or to update the last access datan existing file. The command is used primarily when a script requires the pre-existence of a fexample, to which to append information) or when the script is checking for last date or tfunction was performed.

Syntax

touch [options] [date_time] file

touch [options] [-t time] file

Common Options

-a change the access time of the file (SVR4 only)

-c don’t create the file if it doesn’t already exist

-f force the touch, regardless of read/write permissions

-m change the modification time of the file (SVR4 only)

-t time use the time specified, not the current time (SVR4 only)

When setting the "-t time" option it should be in the form:

[[CC]YY]MMDDhhmm[.SS]

where:

CC first two digits of the year

YY second two digits of the year

MM month, 01-12

DD day of month, 01-31

hh hour of day, 00-23

mm minute, 00-59

SS second, 00-61

The date_time options has the form:

MMDDhhmm[YY]

where these have the same meanings as above.

The date cannot be set to be before 1969 or after January 18, 2038.

Examples

To create a file:

% touch filename

76 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 77: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Working With Files

ters, or

ber of

8.1.6 wc - count words in a file

wc stands for "word count"; the command can be used to count the number of lines, characwords in a file.

Syntax

wc [options] file

Common Options

-c count bytes

-m count characters (SVR4)

-l count lines

-w count words

If no options are specified it defaults to "-lwc".

Examples

Given the fileusers:

jdoe John Doe 4/15/96

lsmith Laura Smith 3/12/96

pchen Paul Chen 1/5/96

jhsu Jake Hsu 4/17/96

sphilip Sue Phillip 4/2/96

the result of using awc command is as follows:

% wc users

5 20 121 users

The first number indicates the number of lines in the file, the second number indicates the numwords in the file, and the third number indicates the number of characters.

Using thewc command with one of the options (-l, lines;-w, words; or-c, characters) would result inonly one of the above. For example, "wc -l users" yields the following result:

5 users

Introduction to Unix 1998 University Technology Services, The Ohio State University 77

Page 78: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

e to)

ly)

s, fornd

ept byoriginal until:

8.1.7 ln - link to another file

The ln command creates a "link" or an additional way to access (or gives an additional namanother file.

Syntax

ln [options] source [target]

If not specifiedtarget defaults to a file of the same name in the present working directory.

Common Options

-f force a link regardless of target permissions; don’t report errors (SVR4 on

-s make a symbolic link

Examples

A symbolic link is used to create a new path to another file or directory. If a group of userexample, is accustomed to using a command calledchkmag, but the command has been rewritten ais now calledchkit, creating a symbolic link so the users will automatically executechkit when theyenter the commandchkmag will ease transition to the new command.

A symbolic link would be done in the following way:

% ln -s chkit chkmag

The long listing for these two files is now as follows:

16 -rwxr-x--- 1 lindadb acs 15927 Apr 23 04:10 chkit

1 lrwxrwxrwx 1 lindadb acs 5 Apr 23 04:11 chkmag -> chkit

Note that while the permissions forchkmag are open to all, since it is linked tochkit, the permissions,group and owner characteristics forchkit will be enforced whenchkmag is run.

With a symbolic link, the link can exist without the file or directory it is linked to existing first.

A hard link can only be done to another file on the same file system, but not to a directory (excthe superuser). A hard link creates a new directory entry pointing to the same inode as the file. The file linked to must exist before the hard link can be created. The file will not be deletedall the hard links to it are removed. To link the two files above with a hard link to each other do

% ln chkit chkmag

Then a long listing shows that theinode number (742) is the same for each:

% ls -il chkit chkmag

742 -rwxr-x--- 2 lindadb acs 15927 Apr 23 04:10 chkit

742 -rwxr-x--- 2 lindadb acs 15927 Apr 23 04:10 chkmag

78 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 79: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Working With Files

e order in the).

os1

)

8.1.8 sort - sort file contents

Thesort command is used to order the lines of a file. Various options can be used to choose thas well as the field on which a file is sorted. Without any options, the sort compares entire linesfile and outputs them in ASCII order (numbers first, upper case letters, then lower case letters

Syntax

sort [options] [+pos1 [ -pos2 ]] file

Common Options

-b ignore leading blanks (<space> & <tab>) when determining starting andending characters for the sort key

-d dictionary order, only letters, digits, <space> and <tab> are significant

-f fold upper case to lower case

-k keydef sort on the defined keys (not available on all systems)

-i ignore non-printable characters

-n numeric sort

-o outfile output file

-r reverse the sort

-t char use char as the field separator character

-u unique; omit multiple copies of the same line (after the sort)

+pos1 [-pos2] (old style) provides functionality similar to the "-k keydef" option.

For the+/-position entriespos1 is the starting word number, beginning with0 andpos2 is the endingword number. When-pos2 is omitted the sort field continues through the end of the line. Both pandpos2 can be written in the formw.c, wherew is the word number andc is the character within theword. Forc 0 specifies the delimiter preceding the first character, and1 is the first character of theword. These entries can be followed by type modifiers, e.g.n for numeric,b to skip blanks, etc.

Thekeydef field of the "-k" option has the syntax:

start_field [type] [ ,end_field [type] ]

where:

start_field, end_field define the keys to restrict the sort to a portion of the line

type modifies the sort, valid modifiers are given the single characters (bdfiMnrfrom the similar sort options, e.g. a typeb is equivalent to "-b", but appliesonly to the specified field

Introduction to Unix 1998 University Technology Services, The Ohio State University 79

Page 80: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

ort on

Examples

In the fileusers:

jdoe John Doe 4/15/96

lsmith Laura Smith 3/12/96

pchen Paul Chen 1/5/96

jhsu Jake Hsu 4/17/96

sphilip Sue Phillip 4/2/96

sort users yields the following:

jdoe John Doe 4/15/96

jhsu Jake Hsu 4/17/96

lsmith Laura Smith 3/12/96

pchen Paul Chen 1/5/96

sphilip Sue Phillip 4/2/96

If, however, a listing sorted by last name is desired, use the option to specify which field to s(fields are numbered starting at 0):

% sort +2 users:

pchen Paul Chen 1/5/96

jdoe John Doe 4/15/96

jhsu Jake Hsu 4/17/96

sphilip Sue Phillip 4/2/96

lsmith Laura Smith 3/12/96

To sort in reverse order:

% sort -r users:

sphilip Sue Phillip 4/2/96

pchen Paul Chen 1/5/96

lsmith Laura Smith 3/12/96

jhsu Jake Hsu 4/17/96

jdoe John Doe 4/15/96

80 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 81: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Working With Files

ile

many.

A particularly usefulsort option is the-u option, which eliminates any duplicate entries in a file whordering the file. For example, the file todays.logins:

sphillip

jchen

jdoe

lkeres

jmarsch

ageorge

lkeres

proy

jchen

shows a listing of each username that logged into the system today. If we want to know howunique users logged into the system today, using sort with the-u option will list each user only once(The command can then be piped into "wc -l" to get a number):

% sort -u todays.logins

ageorge

jchen

jdoe

jmarsch

lkeres

proy

sphillip

Introduction to Unix 1998 University Technology Services, The Ohio State University 81

Page 82: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

pelines.

8.1.9 tee - copy command output

tee sends standard in to specified files and also to standard out. It’s often used in command pi

Syntax

tee [options] [file[s]]

Common Options

-a append the output to the files

-i ignore interrupts

Examples

In this first example the output ofwho is displayed on the screen and stored in the fileusers.file:

brigadier: condron [55]> who | tee users.file

condron ttyp0 Apr 22 14:10 (lcondron-pc.acs.)

frank ttyp1 Apr 22 16:19 (nyssa)

condron ttyp9 Apr 22 15:52 (lcondron-mac.acs)

brigadier: condron [56]> cat users.file

condron ttyp0 Apr 22 14:10 (lcondron-pc.acs.)

frank ttyp1 Apr 22 16:19 (nyssa)

condron ttyp9 Apr 22 15:52 (lcondron-mac.acs)

In this next example the output ofwho is sent to the filesusers.a andusers.b. It is also piped to thewc command, which reports the line count.

brigadier: condron [57]> who | tee users.a users.b | wc -l

3

brigadier: condron [58]> cat users.a

condron ttyp0 Apr 22 14:10 (lcondron-pc.acs.)

frank ttyp1 Apr 22 16:19 (nyssa)

condron ttyp9 Apr 22 15:52 (lcondron-mac.acs)

brigadier: condron [59]> cat users.b

condron ttyp0 Apr 22 14:10 (lcondron-pc.acs.)

frank ttyp1 Apr 22 16:19 (nyssa)

condron ttyp9 Apr 22 15:52 (lcondron-mac.acs)

82 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 83: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Working With Files

In the following example a long directory listing is sent to the filefiles.long. It is also piped to thegrep command which reports which files were last modified in August.

brigadier: condron [60]> ls -l | tee files.long |grep Aug

1 drwxr-sr-x 2 condron 512 Aug 8 1995 News/

2 -rw-r--r-- 1 condron 1076 Aug 8 1995 magnus.cshrc

2 -rw-r--r-- 1 condron 1252 Aug 8 1995 magnus.login

brigadier: condron [63]> cat files.long

total 34

2 -rw-r--r-- 1 condron 1253 Oct 10 1995 #.login#

1 drwx------ 2 condron 512 Oct 17 1995 Mail/

1 drwxr-sr-x 2 condron 512 Aug 8 1995 News/

5 -rw-r--r-- 1 condron 4299 Apr 21 00:18 editors.txt

2 -rw-r--r-- 1 condron 1076 Aug 8 1995 magnus.cshrc

2 -rw-r--r-- 1 condron 1252 Aug 8 1995 magnus.login

7 -rw-r--r-- 1 condron 6436 Apr 21 23:50 resources.txt

4 -rw-r--r-- 1 condron 3094 Apr 18 18:24 telnet.ftp

1 drwxr-sr-x 2 condron 512 Apr 21 23:56 uc/

1 -rw-r--r-- 1 condron 1002 Apr 22 00:14 uniq.tee.txt

1 -rw-r--r-- 1 condron 1001 Apr 20 15:05 uniq.tee.txt~

7 -rw-r--r-- 1 condron 6194 Apr 15 20:18 unixgrep.txt

Introduction to Unix 1998 University Technology Services, The Ohio State University 83

Page 84: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

8.1.10 uniq - remove duplicate lines

uniq filters duplicate adjacent lines from a file.

Syntax

uniq [options] [+|-n] file [file.new]

Common Options

-d one copy of only the repeated lines

-u select only the lines not repeated

+n ignore the firstn characters

-s n same as above (SVR4 only)

-n skip the firstn fields, including any blanks (<space> & <tab>)

-f fields same as above (SVR4 only)

Examples

Consider the following file and example, in whichuniq removes the 4th line fromfile and places theresult in a file calledfile.new.

{unix prompt 1} cat file

1 2 3 6

4 5 3 6

7 8 9 0

7 8 9 0

{unix prompt 2} uniq file file.new

{unix prompt 3} cat file.new

1 2 3 6

4 5 3 6

7 8 9 0

Below, the-n option of theuniq command is used to skip the first 2 fields infile, and filter out lineswhich are duplicates from the 3rd field onward.

{unix prompt 4} uniq -2 file

1 2 3 6

7 8 9 0

84 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 85: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Working With Files

y

ograms

mat

8.1.11 strings - find ASCII strings

To search a binary file for printable, ASCII, strings use thestrings command. It searches for ansequence of 4 or more ASCII characters terminated by a <newline> or null character. I find thiscommand useful for searching for file names and possible error messages within compiled prthat I don’t have source code for.

Syntax

strings [options] file

Common Options

-n number use number as the minimum string length, rather than 4 (SVR4 only)

-number same as above

-t format precede the string with the byte offset from the start of the file, where foris one of:d = decimal,o = octal,x = hexadecimal (SVR4 only)

-o precede the string with the byte offset in decimal (BSD only)

Examples

% strings /bin/cut

SUNW_OST_OSCMD

no delimiter specified

invalid delimiter

b:c:d:f:ns

cut: -n may only be used with -b

cut: -d may only be used with -f

cut: -s may only be used with -f

no list specified

cut: cannot open %s

invalid range specifier

too many ranges specified

ranges must be increasing

invalid character in range

Internal error processing input

invalid multibyte character

unable to allocate enough memory

unable to allocate enough memory

cut:

usage: cut -b list [-n] [filename ...]

cut -c list [filename ...]

cut -f list [-d delim] [-s] [filename]

Introduction to Unix 1998 University Technology Services, The Ohio State University 85

Page 86: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

s this

ed

n

cters of

8.1.12 file - file type

This program,file, examines the selected file and tries to determine what type of file it is. It doeby reading the first few bytes of the file and comparing them with the table in/etc/magic. It candetermine ASCII text files, tar formatted files, compressed files, etc.

Syntax

file [options] [-m magic_file] [-f file_list] file

Common Options

-c check the magic file for errors in format

-f file_list file_list contains a list of files to examine

-h don’t follow symbolic links (SVR4 only)

-L follow symbolic links (BSD only)

-m magic_file usemagic_file as the magic file instead of /etc/magic

Examples

Below we list the output from the command "file filename" for some representative files.

/etc/magic: ascii text

/usr/local/bin/gzip: Sun demand paged SPARC executable dynamically linked

/usr/bin/cut: ELF 32-bit MSB executable SPARC Version 1, dynamically linked, stripp

source.tar: USTAR tar archive

source.tar.Z: compressed data block compressed 16 bits

8.1.13 tr - translate characters

Thetr command translates characters from stdin to stdout.

Syntax

tr [options] string1 [string2]

With no options the characters instring1 are translated into the characters instring2, character bycharacter in the string arrays. The first character instring1 is translated into the first character istring2, etc.

A range of characters in a string is specified with a hyphen between the upper and lower charathe range, e.g. to specify all lower case alphabetic characters use ’[a-z]’.

Repeated characters instring2 can be represented with the ’[x*n] ’ notation, where characterx isrepeatedn times. Ifn is 0 or absent it is assumed to be as large as needed to matchstring1.

86 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 87: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Working With Files

ter and

books.

Characters can include\octal (BSD and SVR4) and\character (SVR4 only) notation. Here "octal"is replaced by the one, two, or three octal integer sequence encoding the ASCII charac"character" can be one of:

b back space

f form feed

n new line

r carriage return

t tab

v vertical tab

The SVR4 version oftr allows the operand ":class:" in the string field whereclass can take oncharacter classification values, including:

alpha alphabetic characters

lower lower case alphabetic characters

upper upper case alphabetic characters

Common Options

-c complement the character set instring1

-d delete the characters instring1

-s squeeze a string of repeated characters instring1 to a single character

Examples

The following examples will use as input the file, a list of P. G. Wodehouse Jeeves & Wooster

The Inimitable Jeeves [1923] The Mating Season [1949]

Carry On, Jeeves [1925] Ring for Jeeves [1953]

Very Good, Jeeves [1930] Jeeves and the Feudal Spirit [1954]

Thank You, Jeeves [1934] Jeeves in the Offing [1960]

Right Ho, Jeeves [1934] Stiff Upper Lip, Jeeves [1963]

The Code of the Woosters [1938] Much Obliged, Jeeves [1971]

Joy in the Morning [1946] Aunts Aren't Gentlemen [1974]

To translate all lower case alphabetic characters to upper case we could use either of:

tr ’[a-z]’ ’[A-Z]’ or tr ’[:lower:]’ ’[:upper:]’

Introduction to Unix 1998 University Technology Services, The Ohio State University 87

Page 88: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

Sincetr reads from stdin we firstcat the file and pipe the output totr, as in:

% cat wodehouse | tr ’[a-z]’ ’[A-Z]’

THE INIMITABLE JEEVES [1923] THE MATING SEASON [1949]

CARRY ON, JEEVES [1925] RING FOR JEEVES [1953]

VERY GOOD, JEEVES [1930] JEEVES AND THE FEUDAL SPIRIT [1954]

THANK YOU, JEEVES [1934] JEEVES IN THE OFFING [1960]

RIGHT HO, JEEVES [1934] STIFF UPPER LIP, JEEVES [1963]

THE CODE OF THE WOOSTERS [1938] MUCH OBLIGED, JEEVES [1971]

JOY IN THE MORNING [1946] AUNTS AREN'T GENTLEMEN [1974]

We could delete all numbers with:

% cat wodehouse | tr -d ’[0-9]’

The Inimitable Jeeves [] The Mating Season []

Carry On, Jeeves [] Ring for Jeeves []

Very Good, Jeeves [] Jeeves and the Feudal Spirit []

Thank You, Jeeves [] Jeeves in the Offing []

Right Ho, Jeeves [] Stiff Upper Lip, Jeeves []

The Code of the Woosters [] Much Obliged, Jeeves []

Joy in the Morning [] Aunts Aren't Gentlemen []

To squeeze all multiple occurrences of the characters e, r, and f:

% cat wodehouse | tr -s ’erf’

The Inimitable Jeves [1923] The Mating Season [1949]

Cary On, Jeves [1925] Ring for Jeves [1953]

Very Good, Jeves [1930] Jeves and the Feudal Spirit [1954]

Thank You, Jeves [1934] Jeves in the Ofing [1960]

Right Ho, Jeves [1934] Stif Upper Lip, Jeves [1963]

The Code of the Woosters [1938] Much Obliged, Jeves [1971]

Joy in the Morning [1946] Aunts Aren't Gentlemen [1974]

88 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 89: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Working With Files

pe orlts.

set

.

8.1.14 find - find files

Thefind command will recursively search the indicated directory tree to find files matching a typattern you specify.find can then list the files or execute arbitrary commands based on the resu

Syntax

find directory [search options] [actions]

Common Options

For the time search options the notation in days,n is:

+n more thann days

n exactlyn days

-n less thann days

Some file characteristics thatfind can search for are:

time that the file was last accessed or changed

-atime n access time, true if accessedn days ago

-ctime n change time, true if the files status was changedn days ago

-mtime n modified time, true if the files data was modifiedn days ago

-newer filename true if newer thanfilename

-type type type of file, wheretype can be:

b block special file

c character special file

d directory

l symbolic link

p named pipe (fifo)

f regular file

-fstype type type of file system, wheretype can be any valid file system type, e.g.:ufs(Unix File System) andnfs (Network File System)

-user username true if the file belongs to the userusername

-group groupname true if the file belongs to the groupgroupname

-perm [-]mode permissions on the file, wheremode is the octal modes for thechmodcommand. Whenmode is precede by the minus sign only the bits that are are compared.

-exec command executecommand. The end ofcommand is indicated by and escapedsemicolon (\;). The command argument,{} , replaces the current path name

-name filename true if the file is namedfilename. Wildcard pattern matches are allowed ifthe meta-character is escaped from the shell with a backslash (\).

-ls always true. It prints a long listing of the current pathname.

-print print the pathnames found (default for SVR4, not for BSD)

Introduction to Unix 1998 University Technology Services, The Ohio State University 89

Page 90: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

ping thebol (

e will

ions on

hell fromevious

Complex expressions are allowed. Expressions should be grouped within parenthesis (escaparenthesis with a backslash to prevent the shell from interpreting them). The exclamation sym!)can be used tonegate an expression. The operators:-a (and) and -o (or) are used to groupexpressions.

Examples

find will recursively search through sub-directories, but for the purpose of these examples wjust use the following files:

14 -rw-r--r-- 1 frank staff 6682 Feb 5 10:04 library

6 -r--r----- 1 frank staff 3034 Mar 16 1995 netfile

34 -rw-r--r-- 1 frank staff 17351 Feb 5 10:04 standard

2 -rwxr-xr-x 1 frank staff 386 Apr 26 09:51 tr25*

To find all files newer than the file, library:

% find . -newer library -print

./tr25

./standard

To find all files with general read or execute permission set, and then to change the permissthose files to disallow this:

% find . \( -perm -004 -o -perm -001 \) -exec chmod o-rx {} \; -exec ls -al {} \;

-rw-r----- 1 frank staff 6682 Feb 5 10:04 ./library

-rwxr-x--- 1 frank staff 386 Apr 26 09:51 ./tr25

-rw-r----- 1 frank staff 17351 Feb 5 10:04 ./standard

In this example the parentheses and semicolons are escaped with a backslash to prevent the sinterpreting them. The curly brackets are automatically replaced by the results from the prsearch and the semicolon ends the command.

We could search for any file name containing the string "ar" with:

% find . -name \*ar\* -ls

326584 7 -rw-r----- 1 frank staff 6682 Feb 5 10:04 ./library

326585 17 -rw-r----- 1 frank staff 17351 Feb 5 10:04 ./standard

where the-ls option prints out a long listing, including the inode numbers.

90 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 91: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

File Archiving, Compression and Conversion

ile hase

if

.Z

a

d.

8.2 File Archiving, Compression andConversion

8.2.1 File Compression

Thecompress command is used to reduce the amount of disk space utilized by a file. When a fbeen compressed using thecompress command, a suffix of.Z is appended to the file name. Thownership modes and access and modification times of the original file are preserved.uncompressrestores the files originally compressed bycompress.

Syntax

compress [options] [file]

uncompress [options] [file.Z]

zcat [file.Z]

Common Options

-c write to standard output and don’t create or change any files

-f force compression of a file, even if it doesn’t reduce the size of the file orthe target file (file.Z) already exists.

-v verbose. Report on the percentage reduction for the file.

zcat writes to standard output. It is equivalent to "uncompress -c".

TABLE 8.2 File Archiving, Compression and Conversion Commands

Command/Syntax What it will do

compress/uncompress/zcat [options]file[.Z] compress or uncompress a file. Compressed files are stored with aending.

dd [if=infile] [of=outfile] [operand=value] copy a file, converting between ASCII and EBCDIC or swappingbyte order, as specified

gzip/gunzip/zcat [options]file[.gz] compress or uncompress a file. Compressed files are stored with .gz ending

od [options]file octal dump a binary file, in octal, ASCII, hex, decimal, or charactermode.

tar key[options] [file(s)] tape archiver--refer to man pages for details on creating, listing, anretrieving from archive files. Tar files can be stored on tape or disk

uudecode [file] decode a uuencoded file, recreating the original file

uuencode [file] new_name encode binary file to 7-bit ASCII, useful when sending via email, tobe decoded as new_name at destination

Introduction to Unix 1998 University Technology Services, The Ohio State University 91

Page 92: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

Examples

Given the files:

96 -rw-r--r-- 1 lindadb acs 45452 Apr 24 09:13 logins.beauty

184 -rw-r--r-- 1 lindadb acs 90957 Apr 24 09:13 logins.bottom

152 -rw-r--r-- 1 lindadb acs 75218 Apr 24 09:13 logins.photon

168 -rw-r--r-- 1 lindadb acs 85970 Apr 24 09:13 logins.top

These can be compressed with:

% compress logins.*

which creates the files:

24 -rw-r--r-- 1 lindadb acs 8486 Apr 24 09:13 logins.beauty.Z

40 -rw-r--r-- 1 lindadb acs 16407 Apr 24 09:13 logins.bottom.Z

24 -rw-r--r-- 1 lindadb acs 10909 Apr 24 09:13 logins.photon.Z

32 -rw-r--r-- 1 lindadb acs 16049 Apr 24 09:13 logins.top.Z

The original files are lost.

To display a compressed file, thezcat command is used:

% zcat logins.beauty.Z | head

beauty:01/22/94:#total logins,4338:#different UIDs,2290

beauty:01/23/94:#total logins,1864:#different UIDs,1074

beauty:01/24/94:#total logins,2317:#different UIDs,1242

beauty:01/25/94:#total logins,3673:#different UIDs,2215

beauty:01/26/94:#total logins,3532:#different UIDs,2216

beauty:01/27/94:#total logins,3096:#different UIDs,1984

beauty:01/28/94:#total logins,3724:#different UIDs,2212

beauty:01/29/94:#total logins,3460:#different UIDs,2161

beauty:01/30/94:#total logins,1408:#different UIDs,922

beauty:01/31/94:#total logins,2175:#different UIDs,1194

A display of the file using commands other thanzcat yields an unreadable, binary, output.

Theuncompress command is used to return the file to its original format:

% uncompress logins.*.Z ; ls -als logins.*

96 -rw-r--r-- 1 lindadb acs 45452 Apr 24 09:13 logins.beauty

184 -rw-r--r-- 1 lindadb acs 90957 Apr 24 09:13 logins.bottom

152 -rw-r--r-- 1 lindadb acs 75218 Apr 24 09:13 logins.photon

168 -rw-r--r-- 1 lindadb acs 85970 Apr 24 09:13 logins.top

92 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 93: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

File Archiving, Compression and Conversion

. The

eble.

rmat, soss

In addition to the standard Unixcompress, uncompress, zcat utilities there are a set ofGNU onesfreely available. These do an even better job of compression using a more efficient algorithmGNU programs to provide similar functions to those above are often installed asgzip, gunzip, andzcat, respectively. Files compressed with gzip are given the endings.z or .gz. GNU software can beobtained via anonymous ftp fromftp://ftp.gnu.org/pub/gnu .

8.2.2 tar - archive files

The tar command combines files into one device or filename for archiving purposes. Thtarcommand does not compress the files; it merely makes a large quantity of files more managea

Syntax

tar [options] [directory file]

Common Options

c create an archive (begin writting at the start of the file)

t table of contents list

x extract from an archive

v verbose

f archive file name

b archive block size

tar will accept its options either with or without a preceding hyphen (-). The archive file can be a diskfile, a tape device, or standard input/output. The latter are represented by a hyphen.

Examples

Given the files and size indications below:

45 logs.beauty

89 logs.bottom

74 logs.photon

84 logs.top

tar can combine these into one file,logfile.tar:

% tar -cf logfile.tar logs.* ; ls -s logfile.tar

304 logfile.tar

Many anonymous FTP archive sites on the Internet store their packages in compressed tar fothe files will end in.tar.Z or .tar.gz. To extract the files from these files you would first uncomprethem, or use the appropriate zcat command and pipe the output into tar, e.g.:

% zcat archive.tar.Z | tar -xvf -

where the hyphen at the end of thetar command indicates that the file is taken fromstdin.

Introduction to Unix 1998 University Technology Services, The Ohio State University 93

Page 94: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

othe

t 8-bit eitherIMEd.

8.2.3 uuencode/uudecode - encode a file

To encode a binary file into 7-bit ASCII use theuuencode command. To decode the file back tbinary use theuudecode command. Theuu in the names comes because they are part of Unix-to-Unix CoPy (UUCP) set of commands. Theuuencode and uudecode commands arecommonly used when sending binary files through e-mail. In e-mail there’s no guarantee thabinary files will be transferred cleanly. So to ensure delivery you should encode the binary file,directly, on the command line and then include the encoded file, or indirectly, by letting your Mmailer program do it for you. In a similar manner, the user decodes the file on the receiving en

Syntax

uuencode [ source_file ] pathname_to_uudecode_to [ > new_file ]

uudecode [ -p ] encoded_fileCommon Options

-p send output to standard output, rather than to the default file

Examples

The first line of encoded file includes the permissionmodes andname thatuudecode will use whendecoding the file. The file begins and ends with thebegin andend keywords, respectively, e.g.:

begin 555 binary_filename

M?T5,1@$" 0 " ( ! %"W #0 5"< T "

M!0 H !4 % 8 T $ - "@ H 4 P

M -0 !$ ! ! ! %"

M%P !0A< % $ $ 4(8 -"& W& W% < 0

M @ !0B T(@ )@ !P O=7-R+VQI8B]L9"YS

M;RXQ ?< 'Y VP "O !VP )8 &6 !G0

M %[ U0 %G !3 ;< #Q %Q !

MEP :P !_ '@ !PP (P

M N0 =H _0 $D Y < #F /L

M01 $' $ & ! P #0A@ 4(8

M" ! 0 !E !@ , T(@ %"( )@ $

M 0 ( ;@ $ ' -"N !0K@ /H

M $ # ', ! P #1J 4:@ #8 !

M !Y 0 , TH %* !=X 0

M@ @ # -/X !3^ "E, $ (4 !

M 4_> )0 0 ". P

; %0 P )@ $

end

94 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 95: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

File Archiving, Compression and Conversion

e input

c.

output,

8.2.4 dd - block copy and convert

Thedd command allows you to copy from raw devices, such as disks and tapes, specifying thand output block sizes.dd was originally known as the disk-to-disk copy program. Withdd you canalso convert between different formats, for example, EBCDIC to ASCII, or swap byte order, et

Syntax

dd [if=input_device] [of=output_device] [operand=value]

Common Options

if=input_device the input file or device

of=output_device the output file or device

If the input or output devices are not specified they default to standard input and standard respectively.

Operands can include:

ibs=n input block size (defaults to 512 byte blocks)

obs=n output block size (defaults to 512 byte blocks)

bs=n sets both input and output block sizes

files=n copyn input files

skip=n skipn input blocks before starting to copy

count=n only copy n input blocks

conv=value[,value] wherevalue can include:

ascii convert EBCDIC to ASCII

ebcdic convert from ASCII to EBCDIC

lcase convert upper case characters to lower case

ucase convert lower case characters to upper case

swab swap every pair of bytes of input data

noerror don’t stop processing on an input error

sync pad every input block to the size ofibs, appending null bytes as needed

Block sizes are specified in bytes and may end ink, b, or w to indicate 1024 (kilo), 512 (block), or 2(word), respectively.

Introduction to Unix 1998 University Technology Services, The Ohio State University 95

Page 96: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

locks,o swap

and

and

Examples

To copy files from one tape drive to another:

% dd if=/dev/rmt/0 of=/dev/rmt/1

20+0 records in

20+0 records out

To copy files written on a tape drive on a big endian machine, written with a block size of 20 bto a file on a little endian machine that now has the tape inserted in its drive, we would need tpairs of bytes, as in:

% dd if=/dev/rmt/0 of=new_file ibs=20b conv=swab

1072+0 records in

21440+0 records out

Upon completiondd reports the number of whole blocks and partial blocks for both the inputoutput files.

8.2.5 od - octal dump of a file

od dumps a file to stdout in different formats, including octal, decimal, floating point, hex,character format.

Syntax

od [options] file

Common Options

-b octal dump

-d|-D decimal (-d) or long decimal (-D) dump

-s|-S signed decimal (-s) and signed long decimal (-S) dump

-f|-F floating point (-f) or long (double) floating point (-F) dump

-x|-X hex (-x) or long hex (-X) dump

-c|-C character (single byte) or long character dump (single or multi-bytecharacters, as determined by locale settings) dump

-v verbose mode

96 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 97: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

File Archiving, Compression and Conversion

orth

Examples

To look at the actual contents of the following file, a list of P. G. Wodehouse’s Lord Emswnovels.

Something Fresh [1915] Uncle Dynamite [1948]

Leave it to Psmith [1923] Pigs Have Wings [1952]

Summer Lightning [1929] Cocktail Time [1958]

Heavy Weather [1933] Service with a Smile [1961]

Blandings Castle and Elsewhere [1935] Galahad at Blandings [1965]

Uncle Fred in the Springtime [1939] A Pelican at Blandings [1969]

Full Moon [1947] Sunset at Blandings [1977]

we could do:

% od -c wodehouse

0000000 S o m e t h i n g F r e s h

0000020 [ 1 9 1 5 ] \t U n c l e D y n

0000040 a m i t e [ 1 9 4 8 ] \n L e a

0000060 v e i t t o P s m i t h

0000100 [ 1 9 2 3 ] \t P i g s H a v e

0000120 W i n g s [ 1 9 5 2 ] \n S u

0000140 m m e r L i g h t n i n g [

0000160 1 9 2 9 ] \t C o c k t a i l T

0000200 i m e [ 1 9 5 8 ] \n H e a v y

0000220 W e a t h e r [ 1 9 3 3 ] \t

0000240 S e r v i c e w i t h a S

0000260 m i l e [ 1 9 6 1 ] \n B l a n

0000300 d i n g s C a s t l e a n d

0000320 E l s e w h e r e [ 1 9 3 5

0000340 ] \t G a l a h a d a t B l a

0000360 n d i n g s [ 1 9 6 5 ] \n U n

0000400 c l e F r e d i n t h e

0000420 S p r i n g t i m e [ 1 9 3 9

0000440 ] \t A P e l i c a n a t B

0000460 l a n d i n g s [ 1 9 6 9 ] \n

0000500 F u l l M o o n [ 1 9 4 7 ]

0000520 \t S u n s e t a t B l a n d

0000540 i n g s [ 1 9 7 7 ] \n

0000554

Introduction to Unix 1998 University Technology Services, The Ohio State University 97

Page 98: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

colter for

l

t

hicyationtween

NIX.terctionsf this

ia

nd theons, as

8.3 Remote Connections

8.3.1 TELNET and FTP - remote login and file transfer protocols

TELNET and FTP are Application Level Internet protocols. The TELNET and FTP protospecifications have been implemented by many different sources, including The National CenSupercomputer Applications (NCSA), and many other public domain and shareware sources.

The programs implementing theTELNET protocol are usually calledtelnet, but not always. Somenotable exceptions aretn3270, WinQVT, and QWS3270, which are also TELNET protocoimplementations. TELNET is used for remote login to other computers on the Internet.

The programs implementing theFTP protocol are usually calledftp, but there are exceptions to thatoo. A program calledFetch, distributed by Dartmouth College,WS_FTP, written and distributed byJohn Junod, andFtptool, written by a Mike Sullivan, are FTP protocol implementations with grapuser interfaces. There’s an enhanced FTP version,ncftp, that allows additional features, written bMike Gleason. Also, FTP protocol implementations are often included in TELNET implementprograms, such as the ones distributed by NCSA. FTP is used for transferring files becomputers on the Internet.

rlogin is a remote login service that was at one time exclusive to Berkeley 4.3 BSD UEssentially, it offers the same functionality astelnet, except that it passes to the remote compuinformation about the user's login environment. Machines can be configured to allow connefrom trusted hosts without prompting for the users’ passwords. A more secure version oprotocol is the Secure SHell,SSH, software written by Tatu Ylonen and available vftp://ftp.net.ohio-state.edu/pub/security/ssh.

From a Unix prompt, these programs are invoked by typing the command (program name) a(Internet) name of the remote machine to which to connect. You can also specify various optiallowed, for these commands.

TABLE 8.3 Remote Connection Commands

Command/Syntax What it will do

finger [options]user[@hostname] report information about users on local and remote machines

ftp [options]host transfer file(s) using file transfer protocol

rcp [options]hostname remotely copy files from this machine to another machine

rlogin [options]hostname login remotely to another machine

rsh [options]hostname remote shell to run on another machine

telnet [host [port]] communicate with another host using telnet protocol

98 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 99: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Remote Connections

host.ugging

set upu

at

Syntax

telnet [options] [ remote_host [ port_number ] ]

tn3270 [options] [ remote_host [ port_number ] ]

ftp [options] [ remote_host ]

Common Options

ftp telnet Action

-d set debugging mode on

-d same as above (SVR4 only)

-i turn off interactive prompting

-n don’t attempt auto-login on connection

-v verbose mode on

-l user connect with username,user, on the remote host (SVR4 only)

-8 8-bit data path (SVR4 only)

telnet andtn3270 allow you the option of specifying a port number to connect to on the remote For both commands it defaults to port number 23, the telnet port. Other ports are used for debof network services and for specialized resources.

Examples

telnet oscar.us.ohio-state.edu

tn3270 ohstmvsa.acs.ohio-state.edu

ftp magnus.acs.ohio-state.edu

The remote machine will query you for your login identification and your password. Machines as archives for software or information distribution often allow anonymous ftp connections. Yoftpto the remote machine and login asanonymous (the loginftp is equivalent on many machines), this, when asked for your "login" you would typeanonymous.

Once you have successfully connected to a remote computer withtelnet andrlogin (and assumingterminal emulation is appropriate) you will be able to use the machine as you always do.

Once you have successfully connected to a remote computer withftp, you will be able to transfer afile "up" to that computer with theput command, or "down" from that computer with thegetcommand. The syntax is as follows:

put local-file-name remote-file-name

get local-file-name remote-file-name

Introduction to Unix 1998 University Technology Services, The Ohio State University 99

Page 100: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

TP

below:

The

Other commands are available inftp as well, depending on the specific "local" and "remote" Fimplementations. Thehelp command will display a list of available commands. Thehelp commandwill also display the purpose of a specific command. Examples of valid commands are shown

help display list of available commands

help mget display the purpose of the mget command ("get multiple files")

pwd present working directory

ls or dir directory list

cd change directory

lcd local change directory

open specify the machine you wish to connect with

user specify your login id (in cases where you are not prompted)

quit quit out of the FTP program

8.3.2 finger - get information about users

finger displays the.plan file of a specific user, or reports who is logged into a specific machine. user must allow general read permission on the.plan file.

Syntax

finger [options] [user[@hostname]]

Common Options

-l force long output format

-m match username only, not first or last names

-s force short output format

Examples

brigadier: condron [77]> finger workshop@nyssa

This is a sample .plan file for the nyssa id, workshop.

This id is being used this week by Frank Fiamingo, Linda

DeBula, and Linda Condron, while we teach a pilot version

of the new Unix workshop we developed for UTS.

Hope yer learnin' somethin'.

Frank, Linda, & Linda

brigadier: condron [77]> finger

Login Name TTY Idle When Where

condron Linda S Condron p0 Sun 18:13 lcondron-mac.acs

frank Frank G. Fiamingo p1 Mon 16:19 nyssa

100 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 101: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Remote Connections

is is thee otherrovidecus on letter

nally)

rk forscape"

local

ve on each

connect or from

8.3.3 Remote commands

A number of Unix machines can be connected together to form a local area network. When thcase, it often happens that a user of one machine has valid login access to several of thmachines in the local network. There are Unix commands available to such users which pconvenience in carrying out certain common operations. Because these commands focommunications with remote hosts in the local network, the command names begin with the"r ": rlogin, rsh, andrcp. The remote access capability of these commands is supported (optioby the dotfile,~/.rhosts, for individual users and by the system-wide file/etc/hosts.equiv. Forsecurity reasons these may be restricted on some hosts.

The rlogin command allows remote login access to another host in the local network.rlogin passesinformation about the local environment, including the value of theTERM environment variable, tothe remote host.

Thersh command provides the ability to invoke a Unix shell on a remote host in the local netwothe purpose of executing a shell command there. This capability is similar to the "shell efunction commonly available from within such Unix software systems as editors and email.

The rcp command provides the ability to copy files from the local host to a remote host in thenetwork.

Syntax

rlogin [ -l username ] remote_host

rsh [ -l username ] remote_host [ command ]

rcp [ [user1]@host1:]original_filename [ [user2]@host2:]new_filename

where the parts in brackets ([]) are optional.rcp does not prompt for passwords, so you must hapermission to execute remote commands on the specified machines as the selected usermachine.

Common Options

-l username connect as the user,username, on the remote host (rlogin & rsh)

The.rhosts file, if it exists in the user's home directory on the remote host, permitsrlogin, rsh, or rcpaccess to that remote host without prompting for a password for that account. The.rhosts filecontains an entry for each remote host and username from which the owner of the.rhosts file maywish to connect. Each entry in the.rhosts file is of the form:

remote_host remote_user

where listing the remote_user is optional. For instance, if Heather Jones wants to be able to to machine1 (where her username is heather) from machine2 (where her username is jones),machine 3 (where her username is heather, the same as for machine1), she could create a.rhosts filein her home directory on machine1. The contents of this file could be:

Introduction to Unix 1998 University Technology Services, The Ohio State University 101

Page 102: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Other Useful Commands

the

, as the

d

machine2 jones

machine3

--or--

machine2 jones

machine3 heather

On a system-wide basis the file/etc/hosts.equiv serves the same purpose for all users, exceptsuper-user. Such a file with the contents:

remote_machine

allows any user from remote_machine to remote connect to this machine without a passwordsame username on this machine.

An /etc/hosts.equiv file with the contents:

remote_machine remote_user

allows remote_user, on remote_machine, to remote connect to this machine asany local user, exceptthe super-user.

/etc/hosts.equiv and~/.rhosts files should be used with caution.

The Secure SHell (SSH) versions of thercp, rsh, and rlogin programs are freely available anprovide much greater security.

102 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 103: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shell Scripts

rst lineble.ecute,ore onuld be:

run a

CHAPTER 9 Shell Programming

9.1 Shell Scripts

You can write shell programs by creating scripts containing a series of shell commands. The fiof the script should start with#! which indicates to the kernel that the script is directly executaYou immediately follow this with the name of the shell, or program (spaces are allowed), to exusing the full path name. Generally you can count on having up to 32 characters, possibly msome systems, and can include one option. So to set up a Bourne shell script the first line wo

#! /bin/sh

or for the C shell:

#! /bin/csh -f

where the "-f" option indicates that it should not read your.cshrc. Any blanks following the magicsymbols,#!, are optional.

You also need to specify that the script is executable by setting the proper bits on the file withchmod,e.g.:

% chmod +x shell_script

Within the scripts# indicates a comment from that point until the end of the line, with#! being aspecial case if found as the first characters of the file.

9.2 Setting Parameter Values

Parameter values, e.g.param, are assigned as:

Bourne shell C shellparam=value set param = value

wherevalue is any valid string, and can be enclosed within quotations, either single (’value) ordouble ("value" ), to allow spaces within the string value. When enclosed with backquotes (‘value‘ )the string is first evaluated by the shell and the result is substituted. This is often used tocommand, substituting the command output forvalue, e.g.:

Introduction to Unix 1998 University Technology Services, The Ohio State University 103

Page 104: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shell Programming

using the

string. butn. A

haractersed to

ult

ariable

erwise,

$ day=‘date +%a‘

$ echo $day

Wed

After the parameter values has been assigned the current value of the parameter is accessed$param, or${param}, notation.

9.3 Quoting

We quote strings to control the way the shell interprets any parameters or variables within theWe can use single (’ ) and double (" ) quotes around strings. Double quotes define the string,allow variable substitution. Single quotes define the string and prevent variable substitutiobackslash (\) before a character is said to escape it, meaning that the system should take the cliterally, without assigning any special meaning to it. These quoting techniques can be useparate a variable from a fixed string. As an example lets use the variable,var, that has beenassigned the valuebat, and the constant string,man. If I wanted to combine these to get the res"batman" I might try:

$varman

but this doesn’t work, because the shell will be trying to evaluate a variable calledvarman, whichdoesn’t exist. To get the desired result we need to separate it by quoting, or by isolating the vwith curly braces ({} ), as in:

"$var"man - quote the variable

$var""man - separate the parameters

$var"man" - quote the constant

$var''man - separate the parameters

$var'man' - quote the constant

$var\man - separate the parameters

${var}man - isolate the variable

These all work because" , ’ , \, {, and} are not valid characters in a variable name.

We could not use either of

’$var’man

\$varman

because it would prevent the variable substitution from taking place.

When using the curly braces they should surround the variable only, and not include the $, oththey will be included as part of the resulting string, e.g.:

% echo {$var}man

{bat}man

104 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 105: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Variables

you to

9.4 Variables

There are a number of variables automatically set by the shell when it starts. These allowreference arguments on the command line.

Theseshell variables are:

We can illustrate these with some simple scripts. First for the Bourne shell the script will be:

#!/bin/sh

echo "$#:" $#

echo '$#:' $#

echo '$-:' $-

echo '$?:' $?

echo '$$:' $$

echo '$!:' $!

echo '$3:' $3

echo '$0:' $0

echo '$*:' $*

echo '$@:' $@

TABLE 9.1 Shell Variables

Variable Usage sh csh

$# number of arguments on the command line x

$- options supplied to the shell x

$? exit value of the last command executed x

$$ process number of the current process x x

$! process number of the last command done in background x

$n argument on the command line, where n is from 1 through 9, reading left to right x x

$0 the name of the current shell or program x x

$* all arguments on the command line ("$1 $2 ... $9") x x

$@ all arguments on the command line, each separately quoted ("$1" "$2" ... "$9") x

$argv[n] selects thenth word from the input list x

${argv[n]} same as above x

$#argv report the number of words in the input list x

Introduction to Unix 1998 University Technology Services, The Ohio State University 105

Page 106: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shell Programming

aning to

When executed with some arguments it displays the values for the shell variables, e.g.:

$ ./variables.sh one two three four five

5: 5

$#: 5

$-:

$?: 0

$$: 12417

$!:

$3: three

$0: ./variables.sh

$*: one two three four five

$@: one two three four five

As you can see, we needed to use single quotes to prevent the shell from assigning special me$. The double quotes, as in the first echo statement, allowed substitution to take place.

Similarly, for the C shell variables we illustrate variable substitution with the script:

#!/bin/csh -f

echo '$$:' $$

echo '$3:' $3

echo '$0:' $0

echo '$*:' $*

echo '$argv[2]:' $argv[2]

echo '${argv[4]}:' ${argv[4]}

echo '$#argv:' $#argv

which when executed with some arguments displays the following:

% ./variables.csh one two three four five

$$: 12419

$3: three

$0: ./variables.csh

$*: one two three four five

$argv[2]: two

${argv[4]}: four

$#argv: 5

106 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 107: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Parameter Substitution

settings

d

9.5 Parameter Substitution

You can reference parameters abstractly and substitute values for them based on conditionalusing the operators defined below. Again we will use the curly braces ({} ) to isolate the variable andits operators.

$parameter substitute the value ofparameter for this string

${parameter} same as above. The brackets are helpful if there’s no separationbetween this parameter and a neighboring string.

$parameter= setsparameter to null .

${parameter-default} if parameter is not set, then usedefault as the value here. Theparameter is not reset.

${parameter=default} if parameter is not set, then set it todefault and use the new value

${parameter+newval) if parameter is set, then usenewval, otherwise use nothing here.The parameter is not reset.

${parameter?message} if parameter is not set, then displaymessage. If parameter is set,then use its current value.

There are no spaces in the above operators. If a colon (:) is inserted before the-, =, +, or? then a testif first performed to see if the parameter has anon-null setting.

The C shell has a few additional ways of substituting parameters:

$list[n] selects thenth word from list

${list[n]} same as above

$#list report the number of words in list

$?parameter return 1 if parameter is set, 0 otherwise

${?parameter} same as above

$< read a line from stdin

The C shell also defines the array,$argv[n] to contain then arguments on the command line an$#argv to be the number of arguments, as noted in Table 9.1.

Introduction to Unix 1998 University Technology Services, The Ohio State University 107

Page 108: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shell Programming

ow this

To illustrate some of these features we’ll use the test script below.

#!/bin/sh

param0=$0

test -n "$1" && param1=$1

test -n "$2" && param2=$2

test -n "$3" && param3=$3

echo 0: $param0

echo "1: ${param1-1}: \c" ;echo $param1

echo "2: ${param2=2}: \c" ;echo $param2

echo "3: ${param3+3}: \c" ;echo $param3

In the script we first test to see if the variable exists, if so we set a parameter to its value. Belwe report the values, allowing substitution.

In the first run through the script we won’t provide any arguments:

$ ./parameter.sh

0: ./parameter.sh # always finds $0

1: 1: # substitute 1, but don’t assign this value

2: 2: 2 # substitute 2 and assign this value

3: : # don’t substitute

In the second run through the script we’ll provide the arguments:

$ ./parameter one two three

0: ./parameter.sh # always finds $0

1: one: one # don’t substitute, it already has a value

2: two: two # don’t substitute, it already has a value

3: 3: three # substitute 3, but don’t assign this value

108 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 109: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Here Document

forme

hell

rt

9.6 Here Document

A here document is a form of quoting that allows shell variables to be substituted. It’s a special of redirection that starts with<<WORD and ends withWORD as the only contents of a line. In thBourne shell you can prevent shell substitution by escapingWORD by putting a\ in front of it on theredirection line, i.e.<<\WORD, but not on the ending line. To have the same effect the C sexpects the\ in front ofWORD at both locations.

The following scripts illustrate this,

for theBourne shell: and for theC shell:

#!/bin/sh #!/bin/csh -f

does=does set does = does

not="" set not = ""

cat << EOF cat << EOF

This here document This here document

$does $not $does $not

do variable substitution do variable substitution

EOF EOF

cat << \EOF cat << \EOF

This here document This here document

$does $not $does $not

do variable substitution do variable substitution

EOF \EOF

Both produce the output:

This here document

does

do variable substitution

This here document

$does $not

do variable substitution

In the top part of the example the shell variables$does and$not are substituted. In the bottom pathey are treated as simple text strings without substitution.

Introduction to Unix 1998 University Technology Services, The Ohio State University 109

Page 110: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shell Programming

is.

9.7 Interactive Input

Shell scripts will accept interactive input to set parameters within the script.

9.7.1 Sh

Sh uses the built-in command,read, to read in a line, e.g.:

read param

We can illustrate this with the simple script:

#!/bin/sh

echo "Input a phrase \c" # This is /bin/echo which requires"\c" to prevent <newline>

read param

echo param=$param

When we run this script it prompts for input and then echoes the results:

$ ./read.sh

Input a phrasehello frank # I type in hello frank <return>

param=hello frank

9.7.2 Csh

Csh uses the$< symbol to read a line from stdin, e.g.:

set param = $<

The spaces around the equal sign are important. The following script illustrates how to use th

#!/bin/csh -f

echo -n "Input a phrase " # This built-in echo requires-n to prevent <newline>

set param = $<

echo param=$param

Again, it prompts for input and echoes the results:

% ./read.csh

Input a phrasehello frank # I type in hello frank <return>

param=hello frank

110 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 111: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Functions

ow you

if aples

se to

9.8 Functions

The Bourne shell has functions. These are somewhat similar to aliases in the C shell, but allmore flexibility. A function has the form:

fcn () { command; }

where the space after{, and the semicolon (;) are both required; the latter can be dispensed with <newline> precedes the}. Additional spaces and <newline>’s are allowed. We saw a few examof this in the sample.profile in an earlier chapter, where we had functions forls andll :

ls() { /bin/ls -sbF "$@";}

ll() { ls -al "$@";}

The first one redefinesls so that the options-sbF are always supplied to the standard/bin/lscommand, and acts on the supplied input,"$@" . The second one takes the current value forls (theprevious function) and tacks on the-al options.

Functions are very useful in shell scripts. The following is a simplified version of one I uautomatically backup up system partitions to tape.

#!/bin/sh

# Cron script to do a complete backup of the system

HOST=`/bin/uname -n`

admin=frank

Mt=/bin/mt

Dump=/usr/sbin/ufsdump

Mail=/bin/mailx

device=/dev/rmt/0n

Rewind="$Mt -f $device rewind"

Offline="$Mt -f $device rewoffl"

# Failure - exit

failure () {

$Mail -s "Backup Failure - $HOST" $admin << EOF_failure

$HOST

Cron backup script failed. Apparently there was no tape in the device.

EOF_failure

exit 1

}

# Dump failure - exit

dumpfail () {

Introduction to Unix 1998 University Technology Services, The Ohio State University 111

Page 112: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shell Programming

etting

ccessive,nctions

n

ed file

n turn,e

can

$Mail -s "Backup Failure - $HOST" $admin << EOF_dumpfail

$HOST

Cron backup script failed. Initial tape access was okay, but dump failed.

EOF_dumpfail

exit 1

}

# Success

success () {

$Mail -s "Backup completed successfully - $HOST" $admin << EOF_success

$HOST

Cron backup script was apparently successful. The /etc/dumpdates file is:

`/bin/cat /etc/dumpdates`

EOF_success

}

# Confirm that the tape is in the device

$Rewind || failure

$Dump 0uf $device / || dumpfail

$Dump 0uf $device /usr || dumpfail

$Dump 0uf $device /home || dumpfail

$Dump 0uf $device /var || dumpfail

($Dump 0uf $device /var/spool/mail || dumpfail) && success

$Offline

This script illustrates a number of topics that we’ve looked at in this document. It starts by svarious parameter values.HOST is set from the output of a command,admin is the administrator ofthe system,Mt , Dump, andMail are program names, device is the special device file used to athe tape drive,Rewind andOffline contain the commands to rewind and off-load the tape drrespectively, using the previously referenced Mt and the necessary options. There are three fudefined:failure , dumpfail , andsuccess. The functions in this script all use ahere document to formthe contents of the function. We also introduce the logicalOR (||) andAND (&& ) operators here;each is position between a pair of commands. For theOR operator, the second command will be ruonly if the first command does not complete successfully. For theAND operator, the secondcommand will be run only if the first command does complete successfully.

The main purpose of the script is done with the Dump commands, i.e. backup the specifisystems. First an attempt is made to rewind the tape. Should this fail,|| failure, thefailure functionis run and we exit the program. If it succeeds we proceed with the backup of each partition ieach time checking for successful completion (|| dumpfail). Should it not complete successfully wrun thedumpfail subroutine and then exit. If the last backup succeeds we proceed with thesuccessfunction ((...) && success). Lastly, we rewind the tape and take it offline so that no other useraccidently write over our backup tape.

112 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 113: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Control Commands

e

on the

9.9 Control Commands

9.9.1 Conditional if

Theconditional if statement is available in both shells, but has a different syntax in each.

9.9.1.1 Sh

if condition1

thencommand list if condition1 is true

[elif condition2

then command list if condition2 is true]

[elsecommand list if condition1 is false]

fiThe conditions to be tested for are usually done with thetest, or [] command (see Section 8.9.6). Thif andthen must be separated, either with a <newline> or a semicolon (;).

#!/bin/sh

if [ $# -ge 2 ]

then

echo $2

elif [ $# -eq 1 ]; then

echo $1

else

echo No input

fi

There are required spaces in the format of the conditional test, one after[ and one before]. This scriptshould respond differently depending upon whether there are zero, one or more argumentscommand line. First with no arguments:

$ ./if.sh

No input

Now with one argument:

$ ./if.sh one

one

And now with two arguments:

$ ./if.sh one two

two

Introduction to Unix 1998 University Technology Services, The Ohio State University 113

Page 114: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shell Programming

more

9.9.1.2 Csh

if (condition) command

-or-

if (condition1)then

command list if condition1 is true

[else if (condition2)then

command list if condition2 is true]

[else

command list if condition1 is false]

endif

The if andthen must be on the same line.

#!/bin/csh -f

if ( $#argv >= 2 ) then

echo $2

else if ( $#argv == 1 ) then

echo $1

else

echo No input

endif

Again, this script should respond differently depending upon whether I have zero, one orarguments on the command line. First with no arguments:

% ./if.csh

No input

Now with one argument:

% ./if.csh one

one

And now with two arguments:

% ./if.csh one two

two

114 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 115: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Control Commands

choice.al bar,

fixed

9.9.2 Conditional switch and case

To choose between a set of string values for a parameter usecase in the Bourne shell andswitch inthe C shell.

9.9.2.1 Sh

case parameterinpattern1[|pattern1a]) command list1;;

pattern2) command list2

command list2a;;

pattern3) command list3;;

*) ;;

esac

You can use any valid filename meta-characters within the patterns to be matched. The;; ends eachchoice and can be on the same line, or following a <newline>, as the last command for the Additional alternative patterns to be selected for a particular case are separated by the vertic|,as in the first pattern line in the example above. The wildcard symbols,:? to indicate any onecharacter and* to match any number of characters, can be used either alone or adjacent tostrings.

This simple example illustrates how to use the conditional case statement.

#!/bin/sh

case $1 in

aa|ab) echo A

;;

b?) echo "B \c"

echo $1;;

c*) echo C;;

*) echo D;;

esac

So when running the script with the arguments on the left, it will respond as on the right:

aa A

ab A

ac D

bb B bb

bbb D

c C

cc C

fff D

Introduction to Unix 1998 University Technology Services, The Ohio State University 115

Page 116: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shell Programming

of theralsame

9.9.2.2 Csh

switch (parameter)

case pattern1:

command list1

[breaksw]case pattern2:

command list2

[breaksw]default:

command list for default behavior

[breaksw]

endsw

breaksw is optional and can be used to break out of the switch after a match to the string valueparameter is made.Switch doesn’t accept "|" in the pattern list, but it will allow you to string sevecase statements together to provide a similar result. The following C shell script has the behavior as the Bourne shellcase example above.

#!/bin/csh -f

switch ($1)

case aa:

case ab:

echo A

breaksw

case b?:

echo -n "B "

echo $1

breaksw

case c*:

echo C

breaksw

default:

echo D

endsw

116 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 117: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Control Commands

isliedg in

f

9.9.3 for and foreach

One way to loop through a list of string values is with thefor andforeach commands.

9.9.3.1 Sh

for variable [in list_of_values]

do

command list

done

The list_of_values is optional, with$@ assumed if nothing is specified. Each value in this listsequentially substituted forvariable until the list is emptied. Wildcards can be used and are appto file names in the current directory. Below we illustrate the for loop in copying all files endin.old to similar names ending in.new. In these examples thebasename utility extracts the base part othe name so that we can exchange the endings.

#!/bin/sh

for file in *.old

do

newf=`basename $file .old`

cp $file $newf.new

done

9.9.3.2 Csh

foreach variable (list_of_values)

command list

end

The equivalent C shell script to copy all files ending in.old to .new is:

#!/bin/csh -f

foreach file (*.old)

set newf = `basename $file .old`

cp $file $newf.new

end

Introduction to Unix 1998 University Technology Services, The Ohio State University 117

Page 118: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shell Programming

ing the

9.9.4 while

Thewhile commands let you loop as long as the condition is true.

9.9.4.1 Sh

while condition

do

command list

[break][continue]

done

A simple script to illustrate awhile loop is:

#!/bin/sh

while [ $# -gt 0 ]

do

echo $1

shift

done

This script takes the list of arguments, echoes the first one, then shifts the list to the left, losoriginal first entry. It loops through until it has shifted all the arguments off the argument list.

$ ./while.sh one two three

one

two

three

118 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 119: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Control Commands

is true.

n is

9.9.4.2 Csh

while (condition)

command list

[break][continue]

end

If you want the condition to always be true specify 1 within the conditional test.

A C shell script equivalent to the one above is:

#!/bin/csh -f

while ($#argv != 0 )

echo $argv[1]

shift

end

9.9.5 until

This looping feature is only allowed in the Bourne shell.

until condition

docommand list while condition is false

done

The condition is tested at the start of each loop and the loop is terminated when the conditionA script equivalent to thewhile examples above is:

#!/bin/sh

until [ $# -le 0 ]

do

echo $1

shift

done

Notice, though, that here we’re testing forless than or equal, rather thangreater than or equal,because theuntil loop is looking for afalse condition.

Both theuntil and while loops are only executed if the condition is satisfied. The conditioevaluated before the commands are executed.

Introduction to Unix 1998 University Technology Services, The Ohio State University 119

Page 120: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shell Programming

tus is

)

ssed

cks

ird

sixth

inal

9.9.6 test

Conditional statements are evaluated fortrue or false values. This is done with thetest, or itsequivalent, the[] operators. It the condition evaluates to true, a zero (TRUE) exit status is set,otherwise a non-zero (FALSE) exit status is set. If there are no arguments a non-zero exit staset. The operators used by the Bourne shell conditional statements are given below.

For filenames the options totest are given with the syntax:

-option filename

The options available for thetest operator forfiles include:

-r true if it exists and is readable

-w true if it exists and is writable

-x true if it exists and is executable

-f true if it exists and is a regular file (or for csh, exists and is not a directory

-d true if it exists and is a directory

-h or -L true if it exists and is a symbolic link

-c true if it exists and is a character special file (i.e. the special device is acceone character at a time)

-b true if it exists and is a block special file (i.e. the device is accessed in bloof data)

-p true if it exists and is a named pipe (fifo)

-u true if it exists and is setuid (i.e. has the set-user-id bit set, s or S in the thbit)

-g true if it exists and is setgid (i.e. has the set-group-id bit set, s or S in the bit)

-k true if it exists and the sticky bit is set (a t in bit 9)

-s true if it exists and is greater than zero in size

There is a test forfile descriptors:

-t [file_descriptor] true if the open file descriptor (default is 1, stdin) is associated with a term

There are tests forstrings:

-z string true if the string length is zero

-n string true if the string length is non-zero

string1 = string2 true if string1 is identical to string2

string1 != string2 true if string1 is non identical to string2

string true if string is not NULL

120 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 121: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Control Commands

he ()

There areinteger comparisons:

n1 -eq n2 true if integers n1 and n2 are equal

n1 -ne n2 true if integers n1 and n2 are not equal

n1 -gt n2 true if integer n1 is greater than integer n2

n1 -ge n2 true if integer n1 is greater than or equal to integer n2

n1 -lt n2 true if integer n1 is less than integer n2

n1 -le n2 true if integer n1 is less than or equal to integer n2

The followinglogical operators are also available:

! negation (unary)

-a and (binary)

-o or (binary)

() expressions within the () are grouped together. You may need to quote tto prevent the shell from interpreting them.

Introduction to Unix 1998 University Technology Services, The Ohio State University 121

Page 122: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Shell Programming

g order

.

)

9.9.7 C Shell Logical and Relational Operators

The C shell has its own set of built-in logical and relational expression operators. In descendinof precedence they are:

(...) group expressions with ()

~ inversion (one’s complement)

! logical negation

*, /, % multiply, divide, modulus

+, - add, subtract

<<, >> bitwise shift left, bitwise shift right

<= less than or equal

>= greater than or equal

< less than

> greater than

== equal

!= not equal

=~ match a string

!~ don’t match the string

& bitwise AND

^ bitwise XOR (exclusive or)

| bitwise OR

&& logical AND

|| logical OR

{command} true (1) if command terminates with a zero exit status, false (0) otherwise

The C shell also allows file type and permission inquiries with the operators:

-r return true (1) if it exists and is readable, otherwise return false (0)

-w true if it exists and is writable

-x true if it exists and is executable

-f true if it exists and is a regular file (or for csh, exists and is not a directory

-d true if it exists and is a directory

-e true if the file exists

-o true if the user owns the file

-z true if the file has zero length (empty)

122 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 123: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

t this

n, and

nts, is,

mostser of

sharingicensend was

ntrol

CHAPTER 10 Editors

There are numerous text processing utilities available with Unix, as is noted throughoudocument (e.g.,ed, ex, sed, awk, the grep family, and theroff family). Among the editors, thestandard "visual" (or fullscreen) editor on Unix isvi. It comprises a super-set, so to speak, ofed andex (the Unix line editors) capabilities.

Vi is a modal editor. This means that it has specific modes that allow text insertion, text deletiocommand entering. You leave the insert mode by typing the<escape> key. This brings you back tocommand mode. The line editor,ex, is incorporated withinvi. You can switch back and forthbetween full-screen and line mode as desired. Invi mode typeQ to go toex mode. Inex mode at the: prompt typevi to return tovi mode. There is also a read-only mode ofvi, which you can invoke asview.

Another editor that is common on Unix systems, especially in college and university environmeemacs (which stands for "editing macros"). Whilevi usually comes with the Unix operating systememacs usually does not. It is distributed by The Free Software Foundation. It is arguably thepowerful editor available for Unix. It is also a very large software system, and is a heavy ucomputer system resources.

The Free Software Foundation and the GNU Project (of whichemacs is a part) were founded byRichard Stallman and his associates, who believe (as stated in the GNU Manifesto) that software is the "fundamental act of friendship among programmers." Their General Public Lguarantees your rights to use, modify, and distribute emacs (including its source code), aspecifically designed to prevent anyone from hoarding or turning a financial profit fromemacs or anysoftware obtained through the Free Software Foundation. Most of their software, includingemacs, isavailable at ftp://ftp.gnu.org/pub/gnu/ and http://www.gnu.org/.

Both vi andemacs allow you to create start-up files that you can populate with macros to cosettings and functions in the editors.

Introduction to Unix 1998 University Technology Services, The Ohio State University 123

Page 124: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Editors

g thesnsvery

10.1 Configuring Your vi Session

To configure thevi environment certain options can be set with the line editor command:set during avi editing session. Alternatively, frequently used options can beset automatically whenvi is invoked,by use of the.exrc file. This file can also contain macros to map keystrokes into functions usinmap function. Withinvi these macros can be defined with the:map command. Control charactercan be inserted by first typing<control>-V (^V ), then the desired control character. The optioavailable invi include, but are not limited to, the following. Some options are not available on eUnix system.

:set all display all option settings

:set ignorecase ignore the case of a character in a search

:set list display tabs and carriage returns

:set nolist turn off list option

:set number display line numbers

:set nonumber turn off line numbers

:set showmode display indication that insert mode is on

:set noshowmode turn off showmode option

:set wrapmargin=n turn on word-wrap n spaces from the right margin

:set wrapmargin=0 turn off wrapmargin option

:set warn display "No write since last change"

:set nowarn turn off "write" warning

The following is a sample.exrc file:

set wrapmargin=10

set number

set list

set warn

set ignorecase

map K {!}fmt -80 # reformat this paragraph,{!} , usingfmt to 80 characters per line

map ^Z :!spell # invokespell, :! , to check a word spelling (return tovi with ^D )

124 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 125: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Configuring Your emacs Session

SP

this

.

10.2 Configuring Your emacs Session

Configuring theemacs environment amounts to making calls to LISP functions.Emacs is infinitelycustomizable by means ofemacs variables and built-in functions and by using Emacs LIprogramming. Settings can be specified from the minibuffer (or command line) during anemacssession. Alternatively, frequently used settings can be established automatically whenemacs isinvoked, by use of a.emacs file. Though a discussion of Emacs LISP is beyond the scope ofdocument, a few examples of commonemacs configurations follow.

To set or toggleemacs variables, or to useemacs built-in functions, use the<escape> key ("Meta" ishowemacs refers to it), followed by the letterx, then by the variable or function and its arguments

M-x what-line what line is the cursor on?

M-x auto-fill-mode turn on word-wrap

M-x auto-fill-mode turn off word-wrap

M-x set-variable<return>

fill-column<return> set line-length to

45 45 characters

M-x set-variable<return>

auto-save-interval<return> save the file automatically after every

300 300 keystrokes

M-x goto-line<return>16 move the cursor to line 16

M-x help-for-help invoke emacs help when C-h has been bound to thebackspace key

The following is a sample.emacs file:

(message "Loading ~/.emacs...")

; Comments begin with semi-colons and continue to the end of the line.

(setq text-mode-hook 'turn-on-auto-fill) ;turn on word-wrap

(setq fill-column 45) ;line-length=45 chars

(setq auto-save-interval 300) ;save after every 300 keystrokes

; Bind (or map) the rubout (control-h) function to the backspace key

(global-set-key "\C-h" 'backward-delete-char-untabify)

; Bind the emacs help function to the keystroke sequence "C-x ?".

(global-set-key "\C-x?" 'help-for-help)

; To jump to line 16, type M-#<return>16

(global-set-key "\M-#" 'goto-line)

; To find out what line you are on, type M-n

(global-set-key "\M-n" 'what-line)

(message "~/.emacs loaded.")

(message "")

Introduction to Unix 1998 University Technology Services, The Ohio State University 125

Page 126: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

vi Quick R

eference Guide

In 10.3 vi Quick Reference Guidehanges the case of the current character

ins the current line and the next line

ndo the last command just done on thisne

epeats last change

ubstitutes text for current character

ubstitutes text for current line

ubstitutes new word(s) for oldline nos effected> s/old/new/g

epeats last substitution (:s) command.

anks (n) lines to buffer

yanks (n) words to buffer

uts yanked or deleted text after cursor

uts yanked or deleted text before cursor

File Manipulation:

writes changes to file (default iscurrent file)

writes changes to current fileand quits edit session

overwrites file (default is cur-rent file)

quits edit session w/no changesmade

quits edit session and discardschanges

edits next file in argument list

) changes name of current file to(name)

reads contents of file into cur-rent edit at the current cursorposition (insert a file)

and) shell escape

and) inserts result of shell commandat cursor position

rite changes to current file and exit

troduction to Unix

1998

University T

echnology Services, T

he Ohio S

tate University

126

All commands invi are preceded by pressing theescape key. Each time a different command is tobe entered, the escape key needs to be used.Except where indicated,vi is case sensitive.

Cursor Movement Commands:

(n) indicates a number, and is optional

(n)h left (n) space(s)

(n)j down (n) space(s)

(n)k up (n) space(s)

(n)l right (n) space(s)

(The arrow keys usually work also)

^F forward one screen

^B back one screen

^D down half screen

^U up half screen

(^ indicates control key; case does not matter)

H beginning of top line of screen

M beginning of middle line of screen

L beginning of last line of screen

G beginning of last line of file

(n)G move to beginning of line (n)

0 (zero) beginning of line

$ end of line

(n)w forward (n) word(s)

(n)b back (n) word(s)

e end of word

Inserting Text:

i insert text before the cursor

a append text after the cursor (does notoverwrite other text)

I insert text at the beginning of the line

A append text to the end of the line

r replace the character under the cursorwith the next character typed

R Overwrite characters until the end of theline (or until escape is pressed to changecommand)

o (alpha o) open new line after the currentline to type text

O (alpha O) open new line before the cur-rent line to type text

Deleting Text:

dd deletes current line

(n)dd deletes (n) line(s)

(n)dw deletes (n) word(s)

D deletes from cursor to end of line

x deletes current character

(n)x deletes (n) character(s)

X deletes previous character

Change Commands:

(n)cc changes (n) characters on line(s) untilend of the line (or until escape is pressed)

cw changes characters of word until end ofthe word (or until escape is pressed)

(n)cw changes characters of the next(n) words

c$ changes text to the end of the line

ct(x) changes text to the letter (x)

C changes remaining text on the currentline (until stopped by escape key)

~ c

J jo

u uli

. r

s s

S s

:s s:<

& r

(n)yy y

y(n)w

p p

P p

:w (file)

:wq

:w! (file)

:q

:q!

:n

:f (name

:r (file)

:! (comm

:r! (comm

ZZ w

Page 127: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

emacs Q

uick Reference G

uide

127 10.4 emacs Quick Reference GuideOther Important Functions

ext command (n) times

racter

rd

e

ard

verse

ce

ord

word

ord

haracters

ords

ning of region

t everything from mark to point

deleted text into current location

ragraph

ch paragraph in region

turn on word wrap

lumn <return> 45

set length of lines to 45 characters

move cursor to line 16

marked

read it

d alternate file

cursor position

to a different file

and be prompted to save

1998

University T

echnology Services, T

he Ohio S

tate University

Introduction to Unix

Emacs commands are accompanied either by simultaneously holding downthe control key (indicated byC-) or by first hitting the escape key(indicated byM- ).

Essential Commands

C-h help

C-x u undo

C-x C-g get out of current operation or command

C-x C-s save the file

C-x C-c close Emacs

Cursor movement

C-f forward one character

C-b back one character

C-p previous line

C-n next line

C-a beginning of line

C-e end of line

C-l center current line on screen

C-v scroll forward

M-v scroll backward

M-f forward one word

M-b back one word

M-a beginning of sentence

M-e end of sentence

M-{ beginning of paragraph

M-} end of paragraph

M-< beginning of buffer

M-> end of buffer

M- (n) repeat the n

C-d delete a cha

M-d delete a wo

C-k kill line

M-k kill sentenc

C-s search forw

C-r search in re

M-% query repla

M-c capitalize w

M-u uppercase

M-l lowercase w

C-t transpose c

M-t transpose w

C-@ mark begin

C-w cut--wipe ou

C-y paste--yank

M-q reformat pa

M-g reformat ea

M-x auto-fill-mode

M-x set-variable <return> fill-co

M-x goto-line <return> 16

M-w copy region

C-x C-f find file and

C-x C-v find and rea

C-x i insert file at

C-x C-s save file

C-x C-w write buffer

C-x C-c exit emacs,

Page 128: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Unix Command Summary

In thiss,

CHAPTER 11 Unix Command Summary

11.1 Unix Commands

In the table below we summarize the more frequently used commands on a Unix system. table, as in general, for most Unix commands,file, could be an actual file name, or a list of file nameor input/output could be redirected to or from the command.

TABLE 11.1 Unix Commands

Command/Syntax What it will do

awk/nawk [options]file scan for patterns in a file and process the results

cat [options]file concatenate (list) a file

cd [directory] change directory

chgrp [options]group file change the group of the file

chmod [options]file change file or directory access permissions

chown [options]owner file change the ownership of a file; can only be done by the superuser

chsh (passwd -e/-s) username login_shell change the user’s login shell (often only by the superuser)

cmp [options]file1 file2 compare two files and list where differences occur (text or binary files)

compress [options]file compress file and save it asfile.Z

cp [options]file1 file2 copyfile1 into file2; file2 shouldn't already exist. This command createsor overwritesfile2.

cut (options) [file(s)] cut specified field(s)/character(s) from lines in file(s)

date [options] report the current date and time

dd [if=infile] [of=outfile] [oper-and=value]

copy a file, converting between ASCII and EBCDIC or swapping byteorder, as specified

diff [options]file1 file2 compare the two files and display the differences (text files only)

df [options] [resource] report the summary of disk blocks and inodes free and in use

du [options] [directory or file] report amount of disk space in use

echo [text string] echo the text string to stdout

ed or ex [options]file Unix line editors

emacs [options]file full-screen editor

exprarguments evaluate the arguments. Used to do arithmetic, etc. in the shell.

file [options]file classify the file type

128 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 129: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Unix Commands

on-

t

find directory [options] [actions] find files matching a type or pattern

finger [options]user[@hostname] report information about users on local and remote machines

ftp [options]host transfer file(s) using file transfer protocol

grep [options] 'search string'argument

egrep[options] 'search string'argument

fgrep [options] 'search string'argument

search the argument (in this case probably a file) for all occurrences ofthe search string, and list them.

gzip [options]file

gunzip[options]file

zcat[options]file

compress or uncompress a file. Compressed files are stored with a .gzending

head [-number]file display the first 10 (or number of) lines of a file

hostname display or set (super-user only) the name of the current machine

kill [options] [-SIGNAL] [pid#] [%job] send a signal to the process with the process id number (pid#) or job ctrol number (%n). The default signal is to kill the process.

ln [options]source_file target link thesource_file to thetarget

lpq [options]

lpstat[options]

show the status of print jobs

lpr [options]file

lp [options]file

print to defined printer

lprm [options]

cancel[options]

remove a print job from the print queue

ls [options] [directory or file] list directory contents orfile permissions

mail [options] [user]

mailx [options] [user]

Mail [options] [user]

simple email utility available on Unix systems. Type a period as the firscharacter on a new line to send message out, question mark for help.

man [options]command show the manual (man) page for a command

mkdir [options]directory make adirectory

more [options]file

less[options]file

pg [options]file

page through a text file

mv [options]file1 file2 movefile1 into file2

od [options]file octal dump a binary file, in octal, ASCII, hex, decimal, or charactermode.

passwd [options] set or change your password

paste [options]file paste field(s) onto the lines infile

pr [options]file filter the file and print it on the terminal

ps [options] show status of active processes

TABLE 11.1 Unix Commands

Command/Syntax What it will do

Introduction to Unix 1998 University Technology Services, The Ohio State University 129

Page 130: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Unix Command Summary

or

ut

on

pwd print working (current) directory

rcp [options]hostname remotely copy files from this machine to another machine

rlogin [options]hostname login remotely to another machine

rm [options]file remove (delete) a file or directory (-r recursively deletes the directoryand its contents) (-i prompts before removing files)

rmdir [options]directory remove adirectory

rsh [options]hostname remote shell to run on another machine

script file saves everything that appears on the screen to file untilexit is executed

sed [options]file stream editor for editing files from a script or from the command line

sort [options]file sort the lines of thefile according to the options chosen

sourcefile

. file

read commands from thefile and execute them in the current shell.source: C shell,.: Bourne shell.

strings [options]file report any sequence of 4 or more printable characters ending in <NL> <NULL>. Usually used to search binary files for ASCII strings.

stty [options] set or display terminal control options

tail [options]file display the last few lines (or parts) of a file

tar key[options] [file(s)] tape archiver--refer to man pages for details on creating, listing, andretrieving from archive files. Tar files can be stored on tape or disk.

tee [options]file copy stdout to one or more files

telnet [host [port]] communicate with another host using telnet protocol

touch [options] [date]file create an empty file, or update the access time of an existing file

tr [options]string1 string2 translate the characters in string1 from stdin into those in string2 in stdo

uncompressfile.Z uncompressfile.Z and save it as a file

uniq [options]file remove repeated lines in a file

uudecode [file] decode a uuencoded file, recreating the original file

uuencode [file] new_name encode binary file to 7-bit ASCII, useful when sending via email, to bedecoded as new_name at destination

vi [options]file visual, full-screen editor

wc [options] [file(s)] display word (or character or line) count forfile(s)

whereis [options]command report the binary, source, and man page locations for the commandnamed

which command reports the path to the command or the shell alias in use

who or w report who is logged in and what processes are running

zcatfile.Z concatenate (list) uncompressed file to screen, leaving file compresseddisk

TABLE 11.1 Unix Commands

Command/Syntax What it will do

130 1998 University Technology Services, The Ohio State University Introduction to Unix

Page 131: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

Highly Recommended

ingrtment

A

3,

n,

of

BN

93,

g

nc.,

CHAPTER 12 A Short Unix Bibliography

12.1 Highly Recommended

UNIX for the Impatient, Paul W. Abrahams & Bruce R. Larson (Addison-Wesley PublishCompany, 1992, ISBN 0-201-55703-7). (A current favorite. Recommended in the CIS Depafor Unix beginners.)

UNIX in a Nutshell for BSD 4.3: A Desktop Quick Reference For Berkeley (O’Reilly & Associates,Inc., 1990, ISBN 0-937175-20-X). (A handy reference for BSD.)

UNIX in a Nutshell: A Desktop Quick Reference for System V & Solaris 2.0 (O’Reilly & Associates,Inc., 1992, ISBN 0-56592-001-5). (A handy reference for SysV and Solaris 2.)

The UNIX Programming Environment, Brian W. Kernighan & Rob Pike (Prentice Hall, 1984). (classic. For serious folks.)

When You Can’t Find Your UNIX System Administrator, Linda Mui (O’Reilly & Associates, Inc.,1995, ISBN 1-56592-104-6).

UNIX Power Tools, Jerry Peek, Tim O’Reilly, and Mike Loukides (O’Reilly & Associates, 199ISBN 0-679-79073-X). (Includes a CDROM of useful software for various OSs.)

12.2 Assorted Others

Understanding UNIX: A Conceptual Guide, James R. Groff & Paul N. Weinberg (Que Corporatio1983).

Exploring the UNIX System, Stephen G. Kochan & Patrick H. Wood (SAMS, a division Macmillan Computer Publishing, 1989, ISBN 0-8104-6268-0).

Learning GNU Emacs, Debra Cameron and Bill Rosenblatt (O’Reilly & Associates, 1992, IS0-937175-84-6).

UNIX for Dummies, John R. Levine & Margaret Levine Young (IDG Books Worldwide, Inc., 19ISBN 0-878058-58-4).

A Practical Guide to UNIX System V, Mark G. Sobell (The Benjamin/Cummings PublishinCompany, Inc., 1985, ISBN 0-80-530243-3).

UNIX Primer Plus, Mitchell Waite, Donald Martin, & Stephen Prata, (Howard W. Sams & Co., I1983, ISBN 0-672-30194-6).

An Introduction to Berkeley UNIX, Paul Wang, (Wadsworth Publishing Company, 1988).

Introduction to Unix 1998 University Technology Services, The Ohio State University 131

Page 132: Introduction to Unix - cibiv.at€¦ · 8 1998University Technology Services, The Ohio State University Introduction to Unix 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS,

A Short Unix Bibliography

BN

BN

BN

Unix Shell Programming, Stephen G. Kochan & Patrick H. Wood (Hayden Book Co., 1990, IS0-8104-6309-1).

The Unix C Shell Field Guide, Gail Anderson and Paul Anderson (Prentice Hall, 1986, IS0-13-937468-X).

A Student’s Guide to UNIX, Harley Hahn. (McGraw-Hill, 1993, ISBN 0-07-025511-3).

Tricks of the UNIX Masters, Russell G. Sage (Howard W. Sams & Co., Inc., 1987, IS0-672-22449-6).

132 1998 University Technology Services, The Ohio State University Introduction to Unix