Unix Shell Scripting

download Unix Shell Scripting

If you can't read please download the document

Transcript of Unix Shell Scripting

  • 1

    UNIX SHELL SCRIPTING

    Perhaps the most important achievement of UNIX is to demonstrate that apowerful operating system for interactive use need not be expensiveeither in equipment or in human effort: UNIX can run on hardware costingas little as $40,000, and less than two man-years were spent on the mainsystem software.

    The UNIX Time-Sharing System (1974)Dennis M. Ritchie and Ken Thompson

  • 2

    Tabel of Contents

    Module 1. Introduction to Operating System 01

    Module 2 Exploring the UNIX Shell 20

    Module 3 Processes 41

    Module 4 A Shell Script 45

    Module 5 A Overview 49

    Module 6 The vi Editor 54

    Module 7 The variable 57

    Module 8 Parameters 68

    Module 9 Regular Expressions 71

    Module 10 A Sample Shell Script 80

    Module 11 Useful Utilities of Shell 93

    Module 12 Arithmetic on Shell 103

    Module 13 Functions 104

    Module 14 Sed and AWK 107

    Module 15 Database Using Shell Script 117

    Module 16 Overview of Perl 123

    Exercise for Lab Experience

    Appendix List of UNIX Commands 126

  • 3

    Module 1Introduction to Operating System:

    In simple terms, an operating system is a manager. It manages all theavailable resources on a computer. These resources can be the hard disk, aprinter, or the monitor screen. Even memory is a resource that needs to bemanaged. Within an operating system are the management functions thatdetermine who gets to read data from the hard disk, what file is going to beprinted next, what characters appear on the screen, and how much memory acertain program gets.

    Note: An operating system (OS) is a collection of system programs thattogether control the operation of a computer system.

    Operating systems may be classified by both how many tasks they canperform s imultaneously and by how many users can be using the systems imultaneously. That is: single-user or multi-user and single-task or multi-tasking. A multi-user system must clearly be multi-tasking.

    Single User Operating System

    MS D O S /P C D O S w as desig ned specifica lly to su it a sing le users

    requirements. The user can run only one program at a time. At any instance of time there is only one process going on in the CPU

    Multi User Operating System

    Here the system is such that many users can work at a time. There isone large CPU and high capacity storage medium enclosed into whatis called as the system unit and different terminals are attached to it.E ach user w orks on a separate term ina l and utilizes the C PU sresources.

    Each users program and other files are stored in th e system u n itsstorage media. Thus the CPU is one and many users are using it.Therefore there is a need of such an OS that will effectively divide theresources of the CPU among all users. Such an OS is called a multiuser OS.

  • 4

    Features of Multi User OS

    1. Multi Processing

    As many users are working at a time, every user will run theirown program. When one program is run by a user it is aprocess. When the same program is run by another user it isanother process. If there are different users running differentprograms there are many processes undergoing execution. Auser shou ld n ot w a it un tilo th e r use rsprogra m s fin ish execution.

    Same program can share by many users at a time and run thattogether. This ability of the OS to run several processingtogether is called multi-processing.

    2. Time Sharing

    The CPU can execute only one instruction at a time. Since thereare several users running their programs the OS divides the CPUtime for each user. It allots a definite time interval called time slicew ith in w h ich that users program is executed. O nce th e tim e sliceisover the C P U sw itches to th e next u se r and executes tha tusers p rogra m . A fter the tim e slice of the user is over the next users program is executed.

    Thus every users programis constantly bein g interrupted b y anoth er users program but no user rea lizes th is because the C PUis very fast. Thus the OS effectively divides the CPU time betweenseveral users.

    3. Memory Management

    A program can run only if it is loaded into the internal memory. Sowhen many users are running their programs that all programs haveto be loaded into the memory. So the CPU memory is dividedlogically such that all users programs get their share of the CPUm em ory. A lso w he n a users p rogram finishes execution it has tobeeliminated from the internal memory and that part of the memoryshould be utilized for storing other use rs program .

    4. Multi Tasking

    Many users work on a multi user environment each running theirown process. Thus there is more than one process executingtogether. But a user can run more than one process or program forhim self if his requirement demands. Such an activity when anumber of processes are running for one user is called Multi-

  • 5

    tasking.PARTS OF Operating System:

    Any Operating System consists of two parts.o The Shell ando The Kernel

    The Shell: The shell acts as an interface between the user and themachine and effectively interprets every command given by the user andadvices the kernel to act accordingly.

    A single user OS will have only one shell devoted entirely to the userwhereas in a multi user OS every user will have a separate shell.

    Kernel: The Kernel is the part of OS that interacts directly with thehardware of the Computer system.

    Why is UNIX Important?

    During the past 25 years the UNIX Operating System has evolved into apowerful, flexible, and versatile operating system. It serves as the OperatingSystem for all types of computers, including single user personal computersand engineering workstations, multi-user microcomputers, minicomputers,mainframes and supercomputers, as well as special purpose devices, withapproximately 20 million computers now running UNIX and more than 100million people using these systems. This rapid growth is expected to continue.The success of UNIX is due to many factors, including its portability to a widerange of machines, its adaptability and simplicity, the wide range of tasks thatit can perform, its multi-user and multi tasking nature, and its suitability fornetworking, which has become increasingly important as the Internet hasblossomed. What follows is a description of the features that have made theUNIX system so popular.

    Understanding UNIX:

    The UNIX operating system was designed to let a number of programmersaccess the computer at the same time and share its resources.The operating system coordinates the use of the computers resources,allowing one person, for example, to run a spell check program while anothercreates a document, lets another edit a document while another createsgraphics, and lets another user format a document -- all at the same time, witheach user oblivious to the activities of the others.The operating system controls all of the commands from all of the keyboardsand all of the data being generated, and permits each user to believe he orshe is the only person working on the computer.

    This real-time sharing of resources makes UNIX one of the most powerful

  • 6

    operating systems ever.Although UNIX was developed by programmers for programmers, it providesan environment so powerful and flexible that it is found in businesses,sciences, academia, and industry. Many telecommunications switches andtransmission systems also are controlled by administration and maintenancesystems based on UNIX.While initially designed for medium-sized minicomputers, the operatingsystem was soon moved to larger, more powerful mainframe computers. Aspersonal computers grew in popularity, versions of UNIX found their way intothese boxes, and a number of companies produce UNIX-based machines forthe scientific and programming communities.

    The uniqueness of UNIX

    The features that made UNIX a hit from the start are:

    Multitasking capability Multi-user capability Portability Cooperative Tools and Utilities Excellent Networking capability Open Source Code

    MultitaskingMany computers do just one thing at a time, as anyone who uses a PC orlaptop can attest. Try logging onto your companys network while openingyour browser while opening a word processing program. Chances are theprocessor will freeze for a few seconds while it sorts out the multipleinstructions.UNIX, on the other hand, lets a computer do several things at once, such asprinting out one file while the user edits another file. This is a major feature forusers, since users dont have to wait for one application to end before startinganother one.

    Multi-userThe same design that permits multitasking permits multiple users to use thecomputer. The computer can take the commands of a number of users --determined by the design of the computer -- to run programs, access files,and print documents at the same time.The computer cant tell the printer to print all the requests at once, but it doesprioritize the requests to keep everything orderly. It also lets several usersaccess the same document by compartmentalizing the document so that thechanges of one user dont override the changes of another user.

    PortabilityA major contribution of the UNIX system was its portability, permitting it tomove from one brand of computer to another with a minimum of code

  • 7

    changes. At a time when different computer lines of the same vendor didnttalk to each other -- yet alone machines of multiple vendors -- that meant agreat savings in both hardware and software upgrades.It also meant that the operating system could be upgraded without having allthe customers data inputted again. And new versions of UNIX were backwardcompatible with older versions, making it easier for companies to upgrade inan orderly manner.

    Cooperative Tools and Utilities

    UNIX comes with hundreds of programs that are divided into two classes: Integral utilities that are absolutely necessary for the operation of the

    computer, such as the command interpreter, and

    Tools that arent necessary for the operation of UNIX but provide theuser with additional capabilities, such as typesetting capabilities and e-mail.

    DC Mail

    Man

    fsck nroff viCalendar

    Fig 1.1 UNIX Tools

    Tools can be added or removed from a UNIX system, depending upon theapplications required.

    Excellent Networking Capability:

    The UNIX system provides an excellent environment for networking. It offersprograms and utilities that provide the services needed to build networkedapplications-the basis for distributed, networked computing. With networkedcomputing, information and processing is shared among different computersin a network. The UNIX system has proved to be useful in client/servercomputing. The UNIX system also has been the base system for thedevelopment of Internet Services. UNIX provides an excellent platform forWeb Servers.

  • 8

    Open Source Code:

    UNIX has provision for protecting data and communicating with other users.The source code (Open Source) for the UNIX system has been madeavailable to users and programmers.

    History of UNIX:

    1965 Bell Laboratories joins with MIT and General Electric in thedevelopment effort for the new operating system, Multics, which wouldprovide multi-user, multi-processor, and multi-level (hierarchical) filesystem, among its many forward-looking features.

    1969 AT&T was unhappy with the progress and drops out of the Multicsproject. Some of the Bell Labs programmers who had worked on this project,Ken Thompson, Dennis Ritchie, Rudd Canaday, and Doug McIlroydesigned and implemented the first version of the Unix File System on a PDP-7 along with a few utilities. It was given the name UNIX by Brian Kernighan asa pun on Multics.

    1971 The system now runs on a PDP-11, with 16Kbytes of memory, including8Kbytes for user programs and a 512Kbyte disk.

    Its first real use is as a text processing tool for the patent department at BellLabs. That utilization justified further research and development by theprogramming group. UNIX caught on among programmers because it wasdesigned 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 using C, a new language developed by DennisRitchie. Being written in this high-level language greatly decreased the effortneeded to port it to new machines.

    1974 Thompson and Ritchie publish a paper in the Communications of theACM describing the new Unix OS. This generates enthusiasm in theAcademic community which sees a potentially great teaching tool for studyingprogramming systems development. Since AT&T is prevented from marketingthe product due to the 1956 Consent Decree they license it to Universities foreducational purposes and to commercial entities.

    By 1977, the fifth and sixth editions had been released; these contained manynew tools and utilities. The number of machines running the UNIX System,

  • 9

    primarily at Bell laboratories and Universities, increased to more than 600 by1978. The seventh edition, the direct ancestor of the UNIX OperatingSystem available today, was released in 1979.

    UNIX System III, based on the Seventh edition, became A T & T s firstcommercial release of the UNUX System in 1982. However, after System IIIwas released, AT&T, through its Western Electric manufacturing subsidiary,continued to sell versions of the UNIX system. UNIX System III, the variousresearch editions, and experimental versions were distributed to colleagues atuniversities and other research laboratories.

    A UNIX System Timeline

    The following timeline summarizes the development of UNIX from itsbeginning -

    Year

    1969

    1973

    1975

    1978

    1979

    1980

    1980

    1982

    1983

    1983

    1984

    UNIX Variant or Standard

    UNICS (later called UNIX)

    Fourth Edition

    Sixth Edition

    3BSD

    Seventh Edition

    Xenix

    4BSD

    System III

    System V Release 1

    4.1BSD

    4.2BSD

    Comments

    A new operating system invented byKen Thompson and Dennis Ritchiefor the PDP-7

    Written in C programming language;widely used inside Bell Laboratories

    First version widely availableoutside of Bell Labs; more than 600machines ran it

    Virtual memory

    Included the Bourne shell, UUCP,and C; the direct ancestor ormodern UNIX

    Introduced by Microsoft

    Introduced by UC Berkeley

    First public release outside of BellLabs

    First supported release

    UC Berkeley release withperformance enhancements

  • 10

    1984

    1986

    1987

    1987

    1988

    1989

    1990

    1990

    1991

    1992

    1992

    1993

    1993

    1993

    1994

    1994

    1995

    1995

    1995

    1996

    System V Release 2

    HP-UX

    System V Release 3

    4.3BSD

    POSIX

    System V Release 4

    XPG3

    OSF/1

    Linux 0.01

    SVR4.2

    HP-UX 9.0

    Solaris 2.3

    4.4BSD

    SVR4.2MP

    Linux 1.0

    Solaris 2.4

    UNIX 95

    Solaris 2.5

    HP-UX 10.0

    Linux 2.0

    UC Berkeley release with manynetworking capabilities

    Protection and locking of files,enhanced system administration,and job control features added

    First version of HP-UX released forHP Precision Architecture

    STREAMS, RFS, TLI added

    Minor enhancements to 4.2BSD

    POSIX.l published

    Unified System V, BSD, and Xenix

    X/Open specification set

    Open Software Foundation releasedesigned to compete with SVR4

    Linus Torvalds started developmentof Linux

    USL developed version of SVR4 forthe desktop

    Supported workstations including aGUI

    POSIX compliant

    Final Berkeley release

    Last version of UNIX developed byUSL

    First version of Linux not considereda "beta"

    Motif supported

    X/Open mark for systems registeredunder the Single UNIX Specification

    CDE supported

    Conformed to the Single UNIX

  • 11

    1997

    1997

    1997

    1997

    1997

    1998

    1998

    1999

    Solaris 2.6

    Single UNIX Specification, Ver2

    System V Release 5 (SVR5)(SCO)

    UnixWare 7

    HP-UX 11.0

    UNIX 98

    Solaris 7

    Linux 2.2

    Specification and the CommonDesktop Environment (CDE)

    Performance improvements andnetworking software added

    UNIX 95 compliant, JAVA supported

    Open Group specification set

    Enhanced SV kernel, including 64-bit support, increased reliability, andperformance enhancements

    SCO UNIX based on SVR5 kernel

    64-bit operating system

    Open Group mark for systemsregistered under the Single UNIXSpecification, Version 2

    Support for 64-bit applications, freefor noncommercial users

    Device drivers added

    Versions of UNIX Today

    With most things in life, where there is active competition the best willultimately survive and triumph. This is the case with several different versionsor flavors of UNIX. Although many different versions exist, a common designand/or code base is present in most of them. Also, two major kinds of UNIXoperating system software markets exist today. The commercial market iswhere customers generally have to pay for the operating system software andgenerally may not get any source code (well, not for free anyway!). The othermarket is also commercial, but is considered open source. Open sourcemeans that you get full access to the source code of the system or programsand can make changes or modifications to that source code as long as youmaintain the rights of the original software owner.

    Today, the UNIX leaders include Solaris, Linux, HP-UX, AIX, and SCO.

  • 12

    Why UNIX Is Popular?

    Many people ask why UNIX is so popular or why it is used so much, in somany different ways and in so many computing environments. The answerlies with the very nature of UNIX and the model that was used to design,build, and continuously improve the operating system.

    Availability of Source Code

    One of the most significant points of UNIX is the availability of source code forthe system. (For those new to software, source code contains theprogramming elements that, when passed through a compiler, will produce abinary program which can be executed.) The binary program containsspecific com puter instructions, w hich te lls the system w h at to d o . W hen th e source code is available, it means that the system (or anysubcomponent) canbe modified without consulting the original author ofthe program. Access tothe source code is a very positive thing and can result in many benefits. Forexample, if software defects (bugs) are found within the source code, they canbe fixed right away without perhaps waiting for the author to do so.

    Another great reason is that new software functions can be integrated into thesource code, thereby increasing the usefulness and the overall functionality ofthe softw are. H aving the ability to extend the softw are to the u s e rsrequirements is a massive gain for the end user and the software industry asa whole. Over time, the software can become much more useful. Onedownside to having access to the source code is that it can become hard tomanage, because it is possible that many different people could havemodified the code in unpredictable (and perhaps negative) ways. However,th is prob lem is typ ica lly ad dressed by having a source cod e m aintaine r,which reviews the source code changes before the modifications areincorporated into the original version.

    Another downside to source code access is that individuals may use thisinformation with the goal in mind of compromising system or componentsecurity. The Internet Worm of 1988 is one such popular example. Theauthor, who was a graduate student at Cornell University at the time, was ableto exploit known security problems within the UNIX system to launch asoftware program that gained unauthorized access to systems and was ableto replicate itself to many networked computers. The Worm was so successfulin attaching and attacking systems that it caused many of the computers tocrash due to the amount of resources needed to replicate. Although the Wormd id n t actu a lly ca use sig n ifican t p e rm a ne n t d a m a g e to th e syste m s it in fected,it opened the eyes of the UNIX community about the dangers of source codeaccess and security on the Internet as a whole.

    Flexible Design

  • 13

    UNIX was designed to be modular, which makes it a very flexible architecture.The modularity helps provide a framework that makes it much easier tointroduce new operating system tools, applications, and utilities, or to help inthe migration of the operating system to new computer platforms or otherdevices. A lthough so m e m ight argum e nt that U N IX isnt flexib le enough fo r their needs, it is quite adaptable and can handle most requirements.This is evidenced by the fact that UNIX runs on more general computerplatforms and devices than any other operating system.

    GNU

    The GNU project, started in the early 1980s, was intended to act as acounterbalance to the widespread activity of corporate greed and adoption oflicense a gree m e n ts fo r com p u te r so ftw a re . T h e GNU is not UNIXpro jectw as resp onsib le for producing som e of the w orlds m ost popular UN IXsoftware.

    This includes the Emacs editor and the gcc compiler. They are thecornerstones of the many tools that a significant number of developers useevery day.

    Open Software

    UNIX is open, which basically means that no single company, institution, orindividual owns UNIX nor can it be controlled by a centralauthority. However, the UNIX name remains a trademark. Anyone usingthe Internet may obtain open source software, install it, and modify it, andthen redistribute the software without ever having to shell out any money inthe process. The open source movement has gained great

    advances and has clearly demonstrated thatquality software can, in fact, be free. Granted, it is quite true that certainversions of UNIX are not open, and you do indeed need to pay to usethese operating systems in the form of an end-user licensing agreement.Generally speaking, vendors that charge for UNIX represent onlya portion of the total number of UNIX releases available within the UNIXcommunity.

    Programming Environment

    UNIX provides one ofthe best development environments available byproviding many of the important tools software developers need. Also, thereare software tools such as compilers and interpreters for just about everymajor programming language known in the world. Not only can one writeprograms in just about any computer language, UNIX also provides additionaldevelopment tools such as text editors, debuggers, linkers, and relatedsoftware. UNIX was conceived and developed by programmers forprogrammers, and it stands to reason that it will continue to be theprogram m ers developm ent platform of choice now and in the future.

  • 14

    Availability of Many Tools

    UNIX comes with a large number of useful applications, utilities, andprogram s, w hich m an y peo ple con sider to b e on e of U N IXs g re a te st strengths. They are collectively known or commonly referredto as UNIX too ls, and th e y cover a w id e rang e o f functions an d purposes. O ne o f th e most significant aspects of UNIX is the availability of software toaccomplish one or more very specific tasks. You will find throughout thistext that the concept of tools is quite universal and is used repeatedly. Thisbook not only discusses the subject of system administration but alsoprovides detailed descriptions of UNIX-based tools. As a systemadministrator, you will come to depend on certain tools to help you do yourjob. Just as construction workersrely on the tools they use, so too will the administrator rely on the softwarethat permits them to handle a wide range of functions, tasks, issues, andproblems.

    There are tools to handle many system administration tasks that you mightencounter. Also, there are tools for development, graphics manipulation, textprocessing, database operations just about any user- or system-related req u ire m en t. If the basic op era tin g system version doesnt p rovid ea p a rticular tool that you need, chances are that someone has alreadydeveloped the tool and it would be available via the Internet.

    System Libraries

    A system library is a collection of software that programmers use to augmenttheir applications. UNIX comes with quite a large collection of functions orroutines that can be accessed from several different languages to aid theapplication writer with a variety of tasks. For example, should the need ariseto sort data, UNIX provides several different sort functions.

    Well Documented

    UNIX is well documented with both online manuals and with many referencebooks and user guides from publishers. Unlike some operating systems, UNIXprovides online main page documentation of all tools that ship with thesystem.

    Also, it is quite customary that open source tools provide gooddocumentation.

    Further, the UNIX community provides journals and magazine articles aboutUNIX, tools, and related topics of interest.

  • 15

    ARCHITECTURE OF UNIX SYSTEM:To understand how the UNIX System works, you need to understand itsstructure. The UNIX Operating System is made up of several majorcomponents. Those components include the Kernel, the shell, the filesystem, and the commands or user programs.

    UNIX is a layered operating system. The innermost layer is the hardware thatprovides the services for the OS. The operating system, referred to in UNIXas the kernel, interacts directly with the hardware and provides the servicesto the user pro gram s. T hese user p rogram s d ont n eed to know an ything about the hardware. They just need to know how to interact withthe kernel and its up to the ke rn e lto provide the desire d service . O ne of the b ig app ea lsof UNIX to programmers has been that most well written user programs areindependent of the underlying hardware, making them readily portable to newsystems.

    Note: The core of the UNIX system is the Kernel. The kernel controls theco m p u ters reso u rces,allo ttin g th em to d ifferen t u sers an d to d ifferen ttasks.

    User programs interact with the kernel through a set of standard systemcalls. These system calls request services to be provided by the kernel. Suchservices would include accessing a file: open close, read, write, link, orexecute a file; starting or updating accounting records; changing ownership ofa file or directory; changing to a new directory; creating, suspending, or killinga process; enabling access to hardware devices; and setting limits on systemresources.

    UNIX is a multi-user, multi-tasking operating system. You can have manyusers logg ed into a system sim ultane ously, each running m an y programs. Its the kernels jo b to keep each proce ss and user separa te and to

    regu la te access to system hardware, including CPU, memory, disk andother I/O

    devices.

    UNIX utilities or commands are a collection of about 200 programs thatservice the day-to-day processing requirements. These programs are invokedthrough the shell, which is itself another utility.

    Apart from the utilities that are provided as part of the UNIX operating system,more than a thousand UNIX based application programs, like databasemanagement systems, word processors, accounting software etc.,

    The basic unit used to organize information in the UNIX System is called afile. The UNIX file system provides a logical method for organizing, storing,retrieving, manipulating, and managing information.

  • 16

    UNIX SHELLS

    The Shell reads your commands and interprets them as requests to executea program or programs, which it then arranges to have carried out. Becausethe shell plays this role, it is called a command interpreter. Besides being acommand interpreter, the shell is also a programming language. As aprogramming language, it permits you to control how and when commandsare carried out. For each user working with UNIX at any time different shellprograms are raining. There may be several shells running in memory, butonly one kernel.

    UNIX shell, including three major variants of the shell.

    1. The Bourne shell

    2. The C Shell

    3. The Korn shell

  • 17

    The original UNIX system shell, sh, was written by Steve Bourne, and as aresult it is known as the Bourne shell.

    The C shell, csh, was originally developed as part of BSD UNIX. cshintroduced a number of important enhancement to sh, including the conceptof a command history list and job control.

    The Korn shell, ksh, builds on the sh and extends it by adding many featuresfrom the C shell.

    Each of these shells has their own respective prompts. The Bourne shell hasthe $ prompt. So when you login it is the bourn shell that is established foryou and the stage is set for you to work on the machine.

    Features of Shell:

    Interactive Processing: It acts as an interface and providescommunication between the users and the system.

    Background Processing: Time consuming; non-interactive tasks canproceed while the user continues with other processing.

    Input/Output redirection: Programs, which can interact with a user,can be made to take their input from another source, such as a file andsend their output to another destination, such as printers.

    Shell Scripts: A frequently used sequence of shell commands can bestored in a file. The name of the file can be later used to execute thestored sequence with a single command.

    Shell Variables: The user can control the behavior of the shell, as wellas other programs utilities by storing data in variables.

  • 18

    The File System

    The UNIX file system looks like an inverted tree structure. You start with theroot directory, denoted by /, at the top and work down through sub-directoriesunderneath it.

    Sreedhar Solo STUD

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

    /home/Sreedhar/source/xntp

    A relative path name specifies the path relative to another, usually the currentworking directory that you are at. Two special directory entries should beintroduced now:

    the current directory the parent of the current directory

    S o if Im at /hom e/frank and w ish to specify the path ab ove in a re la tivefashion I could use:

    /Sreedhar/source/xntp

  • 19

    This indicates that I should first go up one directory level, then come downthrough the Sreedhar directory, followed by the source directory and then toxntp.

    Unix Directories, Files and Inodes

    Every directory and file is listed in its parent directory. In the case of the rootdirectory, that parent is itself. A directory is a file that contains a table listingthe files contained within it, giving file names to the inode numbers in the list.An inode is a special file designed to be read by the kernel to learn theinformation about each file. It specifies the permissions on the file, ownership,date of creation and of last access and change, and the physical location ofthe data blocks on the disk containing the file.

    The system does not require any particular structure for the data in the fileitself. The file can be ASCII or binary or a combination, and may representtext data, a shell script, compiled object code for a program, directory table,junk, or anything you would like.

    T heres no header, tra iler, la bel in form ation or EOF character as part of thefile.

    Unix Programs

    A program, or command, interacts with the kernel to provide theenvironment and perform the functions called for by the user. A program canbe: an executable shell file, known as a shell script; a built-in shell command;or a source compiled, object code file.

    The shell is a command line interpreter. The user interacts with the kernelthrough the shell. You can write ASCII (text) scripts to be acted upon by ashell.

    System programs are usually binary, having been compiled from C sourcecode. These are located in places like /bin, /usr/bin, /usr/local/bin, /usr/ucb,etc.

  • 20

    Module 2Exploring the UNIX Shell:

    The shell is a rather unique component of the UNIX operating system since itis one of the primary ways to interact with the system. It is typically throughthe shell that users execute other commands or invoke additional functions.

    The shell is commonly referred to as a command interpreter and isresponsible for executing tasks on behalf of the user. Figure 2-1 shows apictorial view of how the shell fits with the UNIX system. As you can see, theshell operates within the framework just like any other program. It provides aninterface between the user, the operating system functions, and ultimately thesystem Kernel.

    The UNIX Shell

    Another powerful feature of the UNIX shell is the ability to support thedevelopment and execution of custom shell scripts. The shell contains a miniprogramming language that provides a lightweight way to develop new toolsand utilities without having to be a heavyweight software programmer. A UNIXshell script is a combination of internal shell commands, regular UNIXcommands, and some shell programming rules.

    UNIX supports a large number of different shells, and also many of thepopular ones are freely available on the Internet. Also, many versions of UNIXcome with one or more shells and as the system administrator, you can install

  • 21

    additional shells when necessary and configure the users of the system to usedifferent shells, depending on specific preferences or requirements. The tablebelow lists many of the popular shells and a general description of each.

    Once a user has logged into the system, the default shell prompt appears andthe shell simply waits for input from the user. Thus, logging into a Solarissystem as the root user for example, the standard Bourne shell prompt will be

    #

    The system echoes this prompt to signal that it is ready to receive input fromthe keyboard. At this point, this user is free to type in any standard UNIXcommand, application, or custom script name and the system will attempt toexecute or run the command. The shell assumes that the first argument given

    Shell Name General Descriptionsh Standard Bourne shell, which is one of the most popular shells

    around.

    csh Standard shell with C like language support

    bash GNU Bourne-Again shell that includes elements from the Kornshell and C shell.

    tcsh Standard C shell with command-line editing and filenamecompletion capabilities.

    ksh The Korn shell combines the best features of the Bourne and Cshells and includes powerful programming tools

    zsh Korn shell like, but also provides many more features such asbuilt-in spell correction and programmable command completion.

    Accessing a UNIX System

    The configuration you use to access your UNIX System can be based on oneof two basic models: using multi-user computer or single user computer.

    On a multi-user system, you use your own terminal device to access the UNIXsystem. The computer you access can be a workstation, a microcomputer, amainframe computer, or even a super computer.

    Single user systems are direct personal computer. In this you can directly runUNIX OS. (UnixWare 7.1 by SCO, Solaris 7 from SunSoft, Public domainVersion of UNIX, and popular variant of UNIX known as Linux can use onsingle user system).

  • 22

    Your display can be character-based, or it can be bit mapped. It may display asingle window or multiple windows, as in the X-Windows system.

    Before You Start

    UNIX System from a PC: Many different application packages, calledterminal emulators, run on a PC and enable you to connect to a UNIX system.Terminal emulators all function the same basic way, in that they act asterminal attached to the UNIX machine. This allows you to enter commandsthe same way that you would if you were using a terminal.

    UNIX System from a Terminal: If your terminal has not been set to work witha UNIX System, you must have its options set appropriately. Setting options isdone in different ways on different terminals.

    Selecting a LOGIN : Every UNIX System has at least one person, called theSystem Administrator, whose job is to maintain the system, and make itavailable to its users. The system administrator is also responsible for addingnew users to the system and setting up their initial work environment on thecomputer.

    Login name should created by the system administrator. In general, loginname (logname) can be almost any combination of letters and numbers, butthe UNIX System places some constraints on logname selections:

    Login name must be more than two characters long, and if it is longerthan eight, only the first eight characters are relevant.

    It can contain any combination of lowercase letters and numbers andmust begin with a lowercase letter. If you log in using uppercaseletters, a UNIX system will assume that your terminal can only receiveuppercase letters, and will only send uppercase letters for the entiresession.

    Your logname should not have any symbols or spaces in it, and it mustbe unique for each user. Some lognames are reserved customarily forcertain uses. For example, the root normally refers to the systemadministrator or superuser who is responsible for the whole system.

    Connecting to a UNIX System:

    Direct Connect: With single user workstations and personal computers, andwith the primary administration terminal on a multi-user system (console), acable permanently connects the terminal with the computer. After bootingyour PC and invoking your terminal emulator or turning on your terminal, hitthe carriage return and you should see the UNIX System prompt that says

  • 23

    login:

    Dial in Access: You may have to dial into the computer using a modembefore you are connected. Use your emulator or dial function to dial the UNIXSystem access number. When the system answers the call, you will hear ahigh-pitched tone you should see some characters appear on screen. Thenyou getting UNIX system login prompt.

    Local Area Network: Another means of connecting your PC or terminal to theUNIX System is via a local I network. A local area network (LAN) is a set ofcommunication devices and cables t connects several PCs or terminals andcomputers. A number of LAN environments are in use today, such as LANManager and NetWare. Each LAN environment provides a set of software thatcan be used in conjunction with a specialized hardware card at each end ofthe network, called a NIC (network interface card) or a LAN card; that enablesyou to connect a client machine to a server machine. The clients and serversmay be running Windows or UNIX, or both. The protocol most frequently usedto connect a client machine to a UNIX server is TCP/IP, with other protocolssuch as IPX and SPX also widely used on LANs.

    An example of this environment would be a group of Windows PCs connectedto a common UNIX server running a UNIX operating system such asUnixWare 7, Solaris, or Linux. This type of environment usually is maintainedby a LAN administrator, a person who knows how local area networks work.This is often the same person like system administrator.

    In accessing a UNIX System on a LAN, you first need to configure your PC tobe able to recognize the system you wish to connect to.

    IP Network: If PC is connected to an IP network, such as the Internet or anintranet, you can use the telnet command to access any computer on thisnetwork that allows such connections. The computer you access may be aUNIX computer, or a computer running some other Operating system, and itmay be a local computer or one located thousands of miles away. A variety oftelnet commands can help you manage a telnet session with the computeryou accessing.

    Logging In:

    As a multi-user system, the UNIX System first requires that you identifyyourself before you access to the system.

    login:

  • 24

    Changing Your Password:

    When you first log into a UNIX System, you will have either no password at all(a null password) or an arbitrary password assigned by the systemadministrator. These are only intended for temporary use. Neither offers anyreal security. A null password gives anyone access to your account; oneassigned by the system administrator is likely to be easily guessed bysomeone. Officially assigned passwords often consist of simple combinationsof your initials and your student, employee, or social security number. If yourpassword is simply your employee number and the letter X, anyone withaccess to this information has access to all of your computer files. Sometimesrandom combinations of letters and numbers are used. Such passwords aredifficult to remember, and consequently users will be tempted to write themdown in a convenient place. (Resist this temptation!)

    The passwd Command :

    You change your password by using the passwd command. When you issuethis command, the system checks to see if you are the owner of the login.This prevents someone from changing your password and locking you out ofyour own account. passwd first announces that it is changing the password,and then it asks for your (current) old password, like this:

    $ passwd

    passwd: changing password

    Old password:

    New password:

    Re-enter new password:

    $

    The system asks for a new password and asks for the password to be verified(you do this by retyping it). The next time you log in, the new password iseffective. Although you can ordinarily change your password whenever youwant, on some systems after you change your password you must wait aspecific period of time before you can change it again.

    How to pick a password?

    When choosing a password, it is important that it be something that could notbe guessed -- either by somebody unknown to you trying to break in, or by anacquaintance who knows you. Suggestions for choosing and using apassword follow:

  • 25

    Dont . Use a word (or words) in any languageUse a proper nameUse information that can be found in your walletUse information commonly known about you (car license, pet

    name, etc)Use control characters. Some systems cant handle themWrite your password anywhereEver give your password to *anybody*

    Do . Use a mixture of character types (alphabetic, numeric, special)Use a mixture of upper case and lower caseUse at least 6 charactersChoose a password you can rememberChange your password oftenMake sure nobody is looking over your shoulder when you

    are entering your password

    Caution: If you do forget your password, there is no way to retrieve it.Because it is encrypted, even your system administrator cannot lookupyour password. If you cannot remember it administrator will have to giveyou a new password.

    Changing a Password at Initial Login

    On some systems, you will be required to change your password the first timeyou log in. This will work as described previously and will look like this:

    login: sreedharPassword:Your password has expired.Choose a new one.

    Old password:New password:

    Re-enter new password:

    Password Aging

    To ensure the secrecy of your password, you will not be allowed to use thesame password for long stretches of time. On UNIX Systems, passwords age.When yours gets to the end of its lifespan, you will be asked to change it. Thelength of time your password will be valid is determined by your systemadministrator. However, you can view the status of your password on mostUNIX systems. Generally, the s option to the passwd command shows youthe status of your password, like this:

  • 26

    $ passwd -srayjay PW 04/01/99 7 30 5

    name

    passwd status

    date last changed

    min days between changes

    max days between changes

    days before user will be warned to change password

    The first field contains your login name; the next fields list the status of yourpassword, the date it was last changed, and the minimum and maximum daysallowed between password changes; and the last field is the number of daysbefore your password will need to be changed. Note that this is simply anexample-Km your system, you may not be allowed to read all of these fields.

    An Incorrect Login

    If you make a mistake in typing either your login or your password, the UNIXSystem will respond this way:

    login: sreedharPassword:Login Incorrectlogin:

    You will receive the "Password:" prompt even if you type an incorrect ornonexistent login name. This prevents someone from guessing login namesand learning which one is valid by discovering one that yields the"Password:" prompt. Because any login results in "Password:" an intrudercannot guess login names in this way.

    If you repeatedly type your login or password incorrectly (three to five times,depending on how your system administrator has set the default), the UNIXSystem will disconnect your terminal if it is connected via modem or LAN. Onsome systems, the system administrator will be notified of erroneous loginattempts as a security measure. If you do not successfully log in within sometime interval (usually a minute), you will be disconnected.

    If you have problems logging in, you might also check to make sure that yourCAPS LOCK key has not been set. If it has been set, you will inadvertently enteran incorrect logname or password, because in UNIX uppercase andlowercase letters are treated differently. (Note that unlike in some otherenvironments, your account will not get locked if you enter your passwordincorrectly some number of times, you will just get disconnected.)