Unix - Tutorialspoint

Transcription

UnixAbout the TutorialUnix is a computer Operating System which is capable of handling activities from multipleusers at the same time. The development of Unix started around 1969 at AT&T Bell Labsby Ken Thompson and Dennis Ritchie. This tutorial gives a very good understanding onUnix.AudienceThis tutorial has been prepared for the beginners to help them understand the basics toadvanced concepts covering Unix commands, Unix shell scripting and various utilities.PrerequisitesWe assume you have adequate exposure to Operating Systems and their functionalities.A basic understanding on various computer concepts will also help you in understandingthe various exercises given in this tutorial.Execute Unix Shell ProgramsIf you are willing to learn the Unix/Linux basic commands and Shell script but you do nothave a setup for the same, then do not worry — The CodingGround is available on a highend dedicated server giving you real programming experience with the comfort of singleclick execution. Yes! It is absolutely free and online.Copyright & Disclaimer Copyright 2016 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at contact@tutorialspoint.comi

UnixTable of ContentsAbout the Tutorial . iAudience . iPrerequisites . iExecute Unix Shell Programs . iCopyright & Disclaimer . iTable of Contents . iiUNIX FOR BEGINNERS . 1Unix — Getting Started . 2What is Unix? . 2Unix Architecture. 2Unix — File Management . 8Listing Files . 8Metacharacters . 10Hidden Files . 10Creating Files . 11Editing Files . 11Display Content of a File . 12Counting Words in a File . 12Copying Files . 13Renaming Files. 13Deleting Files . 13Standard Unix Streams . 14Unix — Directory Management . 15Home Directory . 15Absolute/Relative Pathnames . 15Listing Directories . 16Creating Directories . 16Creating Parent Directories . 17Removing Directories . 18Changing Directories . 18Renaming Directories . 18The directories . (dot) and . (dot dot) . 19Unix — File Permission / Access Modes . 20The Permission Indicators . 20File Access Modes. 20Directory Access Modes . 21Changing Permissions . 21Using chmod with Absolute Permissions . 22Changing Owners and Groups . 23Changing Ownership . 24Changing Group Ownership . 24SUID and SGID File Permission . 24ii

UnixUnix — Environment . 26The .profile File . 27Setting the Terminal Type . 27Setting the PATH . 27PS1 and PS2 Variables . 28Environment Variables . 30Unix — Basic Utilities . 32Printing Files . 32Sending Email . 35Unix — Pipes and Filters . 37The grep Command . 37The Sort Command . 38The pg and more Commands . 39Unix — Processes Management . 41Starting a Process . 41Background Processes . 42Listing Running Processes . 42Stopping Processes . 44Parent and Child Processes . 44Zombie and Orphan Processes . 44Daemon Processes . 45The top Command . 45Job ID Versus Process ID . 45Unix — Network Communication Utilities . 46The ping Utility . 46The ftp Utility . 47The telnet Utility . 49The finger Utility . 50Unix — The vi Editor . 52Starting the vi Editor . 52Operation Modes . 53Getting Out of vi . 53Moving within a File . 54Control Commands . 56Editing Files . 57Deleting Characters . 57Change Commands . 58Copy and Paste Commands . 58Advanced Commands . 59Word and Character Searching. 60Set Commands . 61Running Commands . 62Replacing Text . 62IMPORTANT . 62iii

UnixUNIX SHELL PROGRAMMING . 63Unix — What is Shell? . 64Shell Prompt . 64Shell Types . 64Shell Scripts . 65Example Script . 65Shell Comments . 66Extended Shell Scripts . 66Unix — Using Shell Variables . 68Variable Names . 68Defining Variables. 68Accessing Values . 69Read-only Variables . 69Unsetting Variables . 70Variable Types . 70Unix — Special Variables . 71Command-Line Arguments . 72Special Parameters * and @ . 72Exit Status . 73Unix — Using Shell Arrays . 74Defining Array Values . 74Accessing Array Values . 75Unix — Shell Basic Operators . 77Arithmetic Operators. 77Unix - Shell Arithmetic Operators Example . 78Relational Operators . 80Unix - Shell Relational Operators Example . 80Boolean Operators . 82Unix - Shell Boolean Operators Example . 82String Operators . 84Unix - Shell String Operators Example . 84File Test Operators . 86Unix - Shell File Test Operators Example . 87C Shell Operators . 89Unix - C Shell Operators. 89Korn Shell Operators . 92Unix - Korn Shell Operators . 92Unix — Shell Decision Making . 94The if.else statements . 94Unix Shell - The if.fi statement . 94Unix Shell - The if.else.fi statement . 95Unix Shell - The if.elif.fi statement . 96The case.esac Statement . 97Unix Shell - The case.esac Statement . 98iv

UnixUnix — Shell Loop Types . 101Unix Shell - The while Loop . 101Unix Shell - The for Loop. 102Unix Shell - The until Loop . 103Unix Shell - The select Loop . 104Nesting Loops . 107Nesting while Loops. 107Unix — Shell Loop Control . 109The infinite Loop . 109The break statement . 109The continue statement . 111Unix — Shell Substitution . 113What is Substitution? . 113Command Substitution . 114Variable Substitution . 115Unix — Shell Quoting Mechanisms . 117The Metacharacters . 117The Single Quotes . 118The Double Quotes . 119The Backquotes . 120Unix — Shell Input/Output Redirections . 121Output Redirection . 121Input Redirection . 122Here Document . 122Discard the output . 124Redirection Commands . 125Unix — Shell Functions . 126Creating Functions . 126Pass Parameters to a Function . 127Returning Values from Functions . 127Nested Functions . 128Function Call from Prompt . 129Unix — Shell Man Page Help . 130Man Page Sections . 130Useful Shell Commands . 131Unix - Useful Commands . 131Files and Directories . 131Manipulating data . 132Compressed Files . 134Getting Information . 135Network Communication . 135Messages between Users . 136Programming Utilities . 136Misc Commands . 138v

UnixADVANCED UNIX . 141Unix — Regular Expressions with SED . 142Invoking sed . 142The sed General Syntax . 142Deleting All Lines with sed . 143The sed Addresses . 143The sed Address Ranges . 144The Substitution Command .

BSD are a few examples. Linux is also a flavor of Unix which is freely available. Several people can use a Unix computer at the same time; hence Unix is called a multiuser system. A user can also