Linux for Developers Developer’S Library
Total Page:16
File Type:pdf, Size:1020Kb
Meydan VIP Bridge, Dubai, United Arab Emirates Dubai’s 80-meter Meydan VIP Bridge carries the royal family and guests to the grandstand at the annual Dubai World Cup horse race. Opened in time for the March 27, 2010 World Cup, the bridge’s architecture aims to evoke the flowing mane of a racehorse. At night, the bridge is lit with shimmering royal blue LED lights embedded in its steel cladding and smoothly following its undulations. While few people have the opportunity to cross this private bridge, it has been seen by millions worldwide, as a key location in the movie Mission Impossible: Ghost Protocol. Linux for Developers Developer’s Library Visit informit.com/devlibrary for a complete list of available publications. The Developer’s Library series from Addison-Wesley provides practicing programmers with unique, high-quality references and tutorials on the latest programming languages and technologies they use in their daily work. All books in the Developer’s Library are written by expert technology practioners who are exceptionally skilled at organizing and presenting information in a way that is useful for other programmers. Developer’s Library titles cover a wide range of topics, from open source programming languages and technologies, mobile application development, and web development to Java programming and more. Linux for Developers Jumpstart Your Linux Programming Skills William “Bo” Rothwell Boston • Columbus • Indianapolis • New York • San Francisco • Amsterdam Cape Town Dubai • London • Madrid • Milan • Munich • Paris • Montreal Toronto • Delhi • Mexico City Sao Paulo • Sidney • Hong Kong • Seoul Singapore • Taipei • Tokyo Many of the designations used by manufacturers and sellers to distinguish their products Editor-in-Chief are claimed as trademarks. Where those designations appear in this book, and the pub- Mark Taub lisher was aware of a trademark claim, the designations have been printed with initial capital Executive Editor letters or in all capitals. Debra Williams The author and publisher have taken care in the preparation of this book, but make no Cauley expressed or implied warranty of any kind and assume no responsibility for errors or Development Editor omissions. No liability is assumed for incidental or consequential damages in connection Chris Zahn with or arising out of the use of the information or programs contained herein. Managing Editor For information about buying this title in bulk quantities, or for special sales opportunities Sandra Schroeder (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our Senior Project Editor corporate sales department at [email protected] or (800) 382-3419. Lori Lyons For government sales inquiries, please contact [email protected]. Project Manager For questions about sales outside the U.S., please contact [email protected]. Dhayanidhi Copy Editor Visit us on the Web: informit.com/aw Paula Lowell Library of Congress Control Number: 2017932512 Indexer Copyright © 2017 Pearson Education, Inc. Cheryl Lenser All rights reserved. Printed in the United States of America. This publication is protected by Proofreader copyright, and permission must be obtained from the publisher prior to any prohibited SathishKumar reproduction, storage in a retrieval system, or transmission in any form or by any means, Technical electronic, mechanical, photocopying, recording, or likewise. For information regarding Reviewers permissions, request forms and the appropriate contacts within the Pearson Education Matthew Helmke Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/. Keith Wright ISBN-13: 978-0-13-465728-8 Editorial Assistant Kim Boedigheimer ISBN-10: 0-13-465728-4 Cover Designer 1 17 Chuti Prasertsith Compositor codeMantra ❖ “A best friend is the only one that walks into your life when the world has walked out.” — Shannon L. Alder Thank you, Sarah, my love and my wife, for walking into my life. “Strong people don't put others down... They lift them up.” — Michael P. Watson Thank you, Mom and Dad, for being supportive. “Fall seven times, stand up eight.” — Japanese Proverb Thank you, Julia, for being understanding. ❖ Contents at a Glance Preface xii I: Open Source Software 1 1 Introduction to Open Source Software 3 II: Linux Essentials 11 2 Introduction to Linux 13 3 The Filesystem 29 4 Essential Commands 47 5 Text Editors 67 6 System Administration 83 III: Linux Programming Languages 95 7 Overview of Linux Programming Languages 97 8 BASH Shell Scripting 111 9 Perl Scripting 119 10 Python Scripting 131 11 C, C++, and Java 139 IV: Using Git 147 12 Git Essentials 149 13 Manage Files with Git 161 14 Manage Differences in Files 173 15 Advanced Git Features 183 Index 189 Contents Preface xii I: Open Source Software 1 1 Introduction to Open Source Software 3 Defining Source Code 3 Closed Source 4 Open Source 5 “Free” Software 5 Choosing Open Source Licensing 7 Options 8 Key Terms 8 Examples 8 Useful Links 9 Summary 10 II: Linux Essentials 11 2 Introduction to Linux 13 Accessing a Linux System 13 Choosing the Right Distribution 14 Logging In 15 Using the GUI 18 Basic Command-Line Execution 21 Command-Line Structure 22 Getting Help 22 Summary 28 3 The Filesystem 29 Understanding the Filesystem 29 Learning the Most Used Directories 30 Naming Considerations 30 Navigating the Filesystem 31 Managing the Filesystem 33 Managing Directories 36 Managing Files 37 Summary 45 viii Contents 4 Essential Commands 47 Command-Line Tools 47 Viewing Files 48 Finding Files 52 Comparing Files 54 Shell Features 55 Permissions 57 Developer Tools 59 File Compression Commands 59 The grep Command 62 Summary 66 5 Text Editors 67 The vi Editor 67 Why Learn vi? 68 What Is vim? 68 Essential vi Commands 69 Additional Editors 79 Emacs 79 gedit and kwrite 81 nano and joe 81 lime and bluefish 82 Summary 82 6 System Administration 83 Essential Tasks 83 Gaining Access to the Root Account 84 Displaying Disk Usage 86 Managing Software 88 Listing and Finding Software 89 Installing Software 90 User Accounts 91 Adding User Accounts 91 Modifying User Accounts 92 Deleting User Accounts 93 Understanding Groups 93 Managing Groups 93 Summary 94 Contents ix III: Linux Programming Languages 95 7 Overview of Linux Programming Languages 97 Scripting Languages 97 BASH Shell Scripting 98 Perl Scripting 99 Python Scripting 100 Additional Scripting Languages 101 Compiled Languages 107 C Programming Basics 107 C++ Programming Basics 108 Java Programming Basics 108 IDEs 108 Summary 109 8 BASH Shell Scripting 111 Basics of BASH Scripting 111 Conditional Expressions 113 Integer Comparisons 115 File Test Comparisons 115 Flow Control Statements 115 The while loop 116 The for Loop 116 The case Statement 117 User Interaction 117 Additional Information 118 Summary 118 9 Perl Scripting 119 Basics of Perl Scripting 119 Executing Perl Code 120 Additional Perl Documentation 121 Variables and Values 122 Flow Control 124 Conditions 126 Additional Features 128 Summary 130 x Contents 10 Python Scripting 131 Basics of Python Scripting 131 Executing Python Code 132 Additional Documentation 133 Variables and Values 133 Flow Control 136 Conditions 137 Additional Features 137 Summary 138 11 C, C++, and Java 139 Understanding System Libraries 139 Managing Shared Library Files 140 Viewing Shared Library Files 142 Building Packages 142 Building RPM Packages 142 Building Debian Packages 145 Exploring Java Installation and Basics 145 Summary 146 IV: Using Git 147 12 Git Essentials 149 Version Control Concepts 149 The First Generation 149 The Second Generation 149 The Third Generation 151 Installing Git 154 Git Concepts and Features 155 Git Stages 155 Choosing Your Git Repository Host 156 Configuring Git 157 Summary 160 13 Manage Files with Git 161 Basic Configuration 161 git status 162 Handling a Multiple Location Situation 165 Contents xi Telling Git to Ignore a File 166 Removing Files 168 Handling Branches 169 Pushing Branches 171 Summary 171 14 Manage Differences in Files 173 Executing Diffs 173 Dealing with White Space 175 Comparing Branches 176 Merging Files 178 Summary 182 15 Advanced Git Features 183 Managing Repositories 183 Getting Content from the Remote Server 184 Connecting via SSH 186 Patching 187 Summary 188 Index 189 Preface When I envisioned this book, I thought of it as the beginning of a journey. Your exact starting point on this journey may differ from that of others, but the purpose of this book is to provide you with what you need to know to start developing software on a Linux Operating System. Some readers will already have software developing experience on Windows-based platforms. For those folks, this book should serve as a guide to how software development differs on Linux from the platform you are used to developing on. Perhaps you already work in Linux, but want to start writing code. Again, this book will provide you with an excellent starting point for that journey. The book is organized into four parts. ■ Part I, “Open Source Software,” contains a single chapter—Chapter 1, “Introduction to Open Source Software.” You learn about open source software, including its advantages over closed source software, as well as some essentials regarding software licenses. ■ In Part II, “Linux Essentials,” you are introduced to the Linux operating system. The goal of this part is to provide you with the key knowledge that software developers need to know while working in Linux. This includes both end user and administration topics. The chapters in this part include the following: ■ Chapter 2, “Introduction to Linux.” In this chapter you learn the basics of Linux, including how to access a Linux system, how to use a Linux-based GUI, and basic command-line execution. ■ Chapter 3, “The Filesystem.” This chapter focuses on how the files are organized in Linux. You are introduced to filesystem concepts, learn how to navigate the filesystem, and learn how to manage the filesystem.