Linux Shell Scripting Tutorial V2.0
Total Page:16
File Type:pdf, Size:1020Kb
Linux Shell Scripting Tutorial v2.0 Written by Vivek Gite <[email protected]> and Edited By Various Contributors PDF generated using the open source mwlib toolkit. See http://code.pediapress.com/ for more information. PDF generated at: Mon, 31 May 2010 07:27:26 CET Contents Articles Linux Shell Scripting Tutorial - A Beginner's handbook:About 1 Chapter 1: Quick Introduction to Linux 4 What Is Linux 4 Who created Linux 5 Where can I download Linux 6 How do I Install Linux 6 Linux usage in everyday life 7 What is Linux Kernel 7 What is Linux Shell 8 Unix philosophy 11 But how do you use the shell 12 What is a Shell Script or shell scripting 13 Why shell scripting 14 Chapter 1 Challenges 16 Chapter 2: Getting Started With Shell Programming 17 The bash shell 17 Shell commands 19 The role of shells in the Linux environment 21 Other standard shells 23 Hello, World! Tutorial 25 Shebang 27 Shell Comments 29 Setting up permissions on a script 30 Execute a script 31 Debug a script 32 Chapter 2 Challenges 33 Chapter 3:The Shell Variables and Environment 34 Variables in shell 34 Assign values to shell variables 38 Default shell variables value 40 Rules for Naming variable name 41 Display the value of shell variables 42 Quoting 46 The export statement 49 Unset shell and environment variables 50 Getting User Input Via Keyboard 50 Perform arithmetic operations 54 Create an integer variable 56 Create the constants variable 57 Bash variable existence check 58 Customize the bash shell environments 59 Recalling command history 63 Path name expansion 65 Create and use aliases 67 The tilde expansion 69 Startup scripts 70 Using aliases 72 Changing bash prompt 73 Setting shell options 77 Setting system wide shell options 82 Chapter 3 Challenges 83 Chapter 4: Conditionals Execution (Decision Making) 84 Bash structured language constructs 84 Test command 86 If structures to execute code based on a condition 87 If. .else. .fi 89 Nested ifs 92 Multilevel if- then- else 93 The exit status of a command 94 Conditional execution 97 Logical AND && 97 Logical OR || 98 Logical Not ! 99 Conditional expression using [ 101 Conditional expression using <nowiki>[[</ nowiki> 102 Numeric comparison 102 String comparison 104 File attributes comparisons 105 Shell command line parameters 110 How to use positional parameters 112 Parameters Set by the Shell 114 Create usage messages 115 Exit command 117 The case statement 119 Dealing with case sensitive pattern 123 Chapter 4 Challenges 126 Chapter 5: Bash Loops 127 The for loop statement 127 Nested for loop statement 133 The while loop statement 135 Use of : to set infinite while loop 139 The until loop statement 141 The select loop statement 143 Exit the select loop statement 146 Using the break statement 148 Using the continue statement 150 Command substitution 153 Chapter 5 Challenges 155 Chapter 6: Shell Redirection 157 Input and Output 157 Standard input 158 Standard output 159 Standard error 160 Empty file creation 161 / dev/ null discards unwanted output 162 Here documents 164 Here strings 166 Redirection of standard error 167 Redirection of standard output 169 Appending redirected output 170 Redirection of both standard error and output 170 Writing output to files 171 Assigns the file descriptor (fd) to file for output 173 Assigns the file descriptor (fd) to file for input 174 Closes the file descriptor (fd) 175 Opening the file descriptors for reading and writing 175 Reads from the file descriptor (fd) 176 Executes commands and send output to the file descriptor (fd) 179 Chapter 6 Challenges 185 Chapter 7: Pipes and Filters 186 Linking Commands 186 Multiple commands 187 Putting jobs in background 188 Pipes 190 How to use pipes to connect programs 191 Input redirection in pipes 193 Output redirection in pipes 194 Why use pipes 194 Filters 195 Chapter 7 Challenges 197 Chapter 8: Traps 198 Signals 198 What is a Process? 199 How to view Processes 201 Sending signal to Processes 204 Terminating Processes 206 Shell signal values 209 The trap statement 210 How to clear trap 212 Include trap statements in a script 214 Use the trap statement to catch signals and handle errors 216 What is a Subshell? 220 Compound command 222 Exec command 223 Chapter 8 Challenges 224 Chapter 9: Functions 225 Writing your first shell function 225 Displaying functions 226 Removing functions 228 Defining functions 228 Writing functions 231 Calling functions 232 Pass arguments into a function 237 Local variable 240 Returning from a function 243 Shell functions library 245 Source command 248 Recursive function 249 Putting functions in background 251 Chapter 9 Challenges 253 Chapter 10: Interactive Scripts 254 Menu driven scripts 254 Getting information about your system 256 Bash display dialog boxes 260 Dialog customization with configuration file 263 A yes/ no dialog box 267 An input dialog box 268 A password box 270 A menu box 273 A progress bar (gauge box) 276 The form dialog for input 279 Console management 281 Get the name of the current terminal 282 Fixing the display with reset 283 Get screen width and hight with tput 284 Moving the cursor with tput 284 Display centered text in the screen in reverse video 285 Set the keyboard leds 286 Turn on or off NumLock leds 286 Turn on or off CapsLock leds 287 Turn on or off ScrollLock leds 287 /etc 288 Shell scripting help 288 Recommended Books 288 References Article Sources and Contributors 289 Image Sources, Licenses and Contributors 293 Article Licenses License 294 Linux Shell Scripting Tutorial - A Beginner's handbook:About 1 Linux Shell Scripting Tutorial - A Beginner's handbook:About This document is Copyright (C) 1999-2009 by Vivek Gite [1] and its contributors. Some rights reserved. Audience This book is for students and Linux System Administrators. It provides the skills to read, write, and debug Linux shell scripts using bash shell. The book begins by describing Linux and simple scripts to automate frequently executed commands and continues by describing conditional logic, user interaction, loops, menus, traps, and functions. Finally, book covers various sys admin related scripts such as making a backup, using cron jobs, writing interactive tools, web based tools, remote login, ftp and database backup related scripts. This book is intended for Linux system administrators or students who have mastered the basics of a Linux Operating System. You should be able to: • Login to local or remote Linux system. • Use basic Linux commands, such as cp, mv, rm, man,less, chmod and others. • Create and edit text files in vi or any other text editor. • GUI is not required except for interactive GTK+ based GUI scripts. Licensing Information This book is available under Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported [2]. • You are free: • to Share — to copy, distribute and transmit the work • to Remix — to adapt the work • Under the following conditions: • Attribution — If you republish this content, we require that you: 1. Indicate that the content is from "Linux Shell Scripting Tutorial - A Beginner's handbook" (http:/ / bash. cyberciti. biz/ guide/ Main_Page), and nixCraft (http:/ / nixcraft. com/ ). 2. Hyperlink to the original article on the source site (e.g., http:/ / bash. cyberciti. biz/ guide/ What_Is_Linux) 3. Show the author name (e.g., Vivek Gite) for all pages. 4. Hyperlink each contributors name back to their profile page on the source wiki (e.g., http:/ / bash. cyberciti. biz/ guide/ User:USERNAME) • Noncommercial — You may not use this work for commercial purposes including the Internet ad supported websites or any sort of print media. • Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one. With the understanding that: • Waiver — Any of the above conditions can be waived if you get permission from the copyright holder (i.e. the Author: Vivek Gite). • Other Rights — In no way are any of the following rights affected by the license: • Your fair dealing or fair use rights; • The author's moral rights; • Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights. Linux Shell Scripting Tutorial - A Beginner's handbook:About 2 • Notice — For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page (http:/ / bash. cyberciti. biz/ guide/ Linux_Shell_Scripting_Tutorial_-_A_Beginner%27s_handbook:About). History • Ver.0.8 - 1998 - First draft with only 8 pages. • Ver.0.9 - 2000 - Second draft with a few more pages. • Ver.1.0 - 2002 - Third draft published on freeos.com. • Ver.1.5r3 - 2004 - Updated version published on freeos.com. • Ver.2.0.beta - Aug-2009 - Wiki started. • This document is now in a wiki format. • All user contributed content licensed under Attribution-Noncommercial-Share Alike 3.0 Unported. Disclaimer • This web site / wiki ("Site") provides educational learning information on Linux scripting for sys admin work, but you are 100% responsible for what you do or don't do with it. • The information compiled by Vivek Gite (and its contributors) as well as the links to complimentary material are provided "as is" with no warranty, express or implied, for their accuracy or reliability. • You use these sites at your own risk, and acknowledge that, while every effort has been made to correct errors before they appear, this site may include certain inaccuracies, errors, or omissions. nixCraft makes no representations as to the suitability, reliability, availability, timeliness, and accuracy of the information on this site for any purpose.