Bash Beginners Guide
Bash Beginners Guide
Machtelt Garrels
CoreSequence.com
Table of Contents
Introduction.........................................................................................................................................................1 1. Why this guide?...................................................................................................................................1 2. Who should read this book? .................................................................................................................1 3. New versions of this guide ...................................................................................................................1 4. Revision History..................................................................................................................................1 5. Contributions.......................................................................................................................................2 6. Feedback..............................................................................................................................................2 7. Copyright information.........................................................................................................................2 8. What do you need? ...............................................................................................................................2 9. Conventions used in this document.....................................................................................................2 10. Organization of this document...........................................................................................................3 Chapter 1. Bash and Bash scripts ......................................................................................................................5 1.1. Common shell programs...................................................................................................................5 1.1.1. General shell functions............................................................................................................5 1.1.2. Shell types...............................................................................................................................5 1.2. Advantages of the Bourne Again SHell............................................................................................6 1.2.1. Bash is the GNU shell.............................................................................................................6 1.2.2. Features only found in bash .....................................................................................................6 1.3. Executing commands......................................................................................................................11 1.3.1. General..................................................................................................................................11 1.3.2. Shell builtin commands .......................................................................................................11 1.3.3. Executing programs from a script.........................................................................................12 1.4. Building blocks...............................................................................................................................12 1.4.1. Shell building blocks.............................................................................................................12 1.5. Developing good scripts.................................................................................................................14 1.5.1. Properties of good scripts......................................................................................................15 1.5.2. Structure................................................................................................................................15 1.5.3. Terminology..........................................................................................................................15 1.5.4. A word on order and logic .....................................................................................................15 1.5.5. An example Bash script: mysystem.sh..................................................................................16 1.5.6. Example init script................................................................................................................18 . 1.6. Summary.........................................................................................................................................18 1.7. Exercises.........................................................................................................................................19 Chapter 2. Writing and debugging scripts.....................................................................................................20 2.1. Creating and running a script..........................................................................................................20 2.1.1. Writing and naming...............................................................................................................20 2.1.2. script1.sh ................................................................................................................................20 2.1.3. Executing the script...............................................................................................................21 2.2. Script basics....................................................................................................................................23 2.2.1. Which shell will run the script? .............................................................................................23 2.2.2. Adding comments ..................................................................................................................23 2.3. Debugging Bash scripts..................................................................................................................24 2.3.1. Debugging on the entire script..............................................................................................24 2.3.2. Debugging on part(s) of the script ........................................................................................25 . 2.4. Summary.........................................................................................................................................27 2.5. Exercises.........................................................................................................................................27 i
Table of Contents
Chapter 3. The Bash environment..................................................................................................................28 3.1. Shell initialization files...................................................................................................................28 3.1.1. Systemwide configuration files...........................................................................................28 3.1.2. Individual user configuration files........................................................................................30 3.1.3. Changing shell configuration files........................................................................................32 3.2. Variables.........................................................................................................................................33 3.2.1. Types of variables.................................................................................................................33 3.2.2. Creating variables..................................................................................................................36 3.2.3. Exporting variables ................................................................................................................37 3.2.4. Reserved variables .................................................................................................................38 3.2.5. Special parameters .................................................................................................................40 3.3. Quoting characters..........................................................................................................................42 3.3.1. Why?.....................................................................................................................................42 3.3.2. Escape characters..................................................................................................................42 3.3.3. Single quotes.........................................................................................................................43 3.3.4. Double quotes........................................................................................................................43 3.3.5. ANSIC quoting...................................................................................................................44 3.3.6. Locales ...................................................................................................................................44 3.4. Shell expansion...............................................................................................................................44 3.4.1. General..................................................................................................................................44 3.4.2. Brace expansion....................................................................................................................44 3.4.3. Tilde expansion.....................................................................................................................44 3.4.4. Shell parameter and variable expansion................................................................................45 3.4.5. Command substitution ...........................................................................................................46 3.4.6. Arithmetic expansion............................................................................................................47 3.4.7. Process substitution...............................................................................................................48 3.4.8. Word splitting........................................................................................................................48 3.4.9. File name expansion..............................................................................................................49 3.5. Aliases.............................................................................................................................................49 3.5.1. What are aliases?...................................................................................................................49 3.5.2. Creating and removing aliases..............................................................................................50 3.6. More Bash options..........................................................................................................................51 3.6.1. Displaying options .................................................................................................................51 3.6.2. Changing options ...................................................................................................................52 3.7. Summary.........................................................................................................................................53 3.8. Exercises.........................................................................................................................................53 Chapter 4. Regular expressions.......................................................................................................................55 4.1. Regular expressions........................................................................................................................55 4.1.1. What are regular expressions? ...............................................................................................55 4.1.2. Regular expression metacharacters.......................................................................................55 4.1.3. Basic versus extended regular expressions...........................................................................56 4.2. Examples using grep.......................................................................................................................56 4.2.1. What is grep?.........................................................................................................................56 4.2.2. Grep and regular expressions................................................................................................57 4.3. Pattern matching using Bash features.............................................................................................59 4.3.1. Character ranges....................................................................................................................59 4.3.2. Character classes...................................................................................................................60 ii
Table of Contents
Chapter 4. Regular expressions 4.4. Summary.........................................................................................................................................60 4.5. Exercises.........................................................................................................................................60 Chapter 5. The GNU sed stream editor..........................................................................................................62 5.1. Introduction.....................................................................................................................................62 5.1.1. What is sed? ...........................................................................................................................62 5.1.2. sed commands.......................................................................................................................62 5.2. Interactive editing...........................................................................................................................63 5.2.1. Printing lines containing a pattern.........................................................................................63 5.2.2. Deleting lines of input containing a pattern..........................................................................64 5.2.3. Ranges of lines......................................................................................................................64 5.2.4. Find and replace with sed......................................................................................................65 5.3. Noninteractive editing..................................................................................................................66 5.3.1. Reading sed commands from a file.......................................................................................66 5.3.2. Writing output files...............................................................................................................66 5.4. Summary.........................................................................................................................................68 5.5. Exercises.........................................................................................................................................68 Chapter 6. The GNU awk programming language ........................................................................................69 6.1. Getting started with gawk...............................................................................................................69 6.1.1. What is gawk?.......................................................................................................................69 6.1.2. Gawk commands...................................................................................................................69 6.2. The print program...........................................................................................................................70 6.2.1. Printing selected fields..........................................................................................................70 6.2.2. Formatting fields...................................................................................................................71 6.2.3. The print command and regular expressions .........................................................................72 6.2.4. Special patterns ......................................................................................................................72 6.2.5. Gawk scripts..........................................................................................................................73 6.3. Gawk variables ................................................................................................................................73 6.3.1. The input field separator.......................................................................................................73 . 6.3.2. The output separators............................................................................................................74 6.3.3. The number of records..........................................................................................................75 6.3.4. User defined variables...........................................................................................................76 6.3.5. More examples......................................................................................................................76 6.3.6. The printf program................................................................................................................76 6.4. Summary.........................................................................................................................................77 6.5. Exercises.........................................................................................................................................77 Chapter 7. Conditional statements..................................................................................................................79 7.1. Introduction to if.............................................................................................................................79 7.1.1. General..................................................................................................................................79 7.1.2. Simple applications of if.......................................................................................................82 . 7.2. More advanced if usage..................................................................................................................84 7.2.1. if/then/else constructs............................................................................................................84 7.2.2. if/then/elif/else constructs ......................................................................................................87 7.2.3. Nested if statements ...............................................................................................................88 7.2.4. Boolean operations................................................................................................................88 iii
Table of Contents
Chapter 7. Conditional statements 7.2.5. Using case statements ............................................................................................................89 7.2.6. Using the exit statement and if..............................................................................................90 7.3. Summary.........................................................................................................................................91 7.4. Exercises.........................................................................................................................................92 Chapter 8. Writing interactive scripts............................................................................................................93 8.1. Displaying user messages...............................................................................................................93 8.1.1. Interactive or not? ..................................................................................................................93 8.1.2. Using the echo builtin command........................................................................................93 8.2. Catching user input.........................................................................................................................96 8.2.1. Using the read builtin command.........................................................................................96 8.2.2. Prompting for user input.......................................................................................................97 . 8.2.3. Redirection and file descriptors .............................................................................................98 8.2.4. File input and output...........................................................................................................100 . 8.3. Summary.......................................................................................................................................104 8.4. Exercises.......................................................................................................................................104 Chapter 9. Repetitive tasks............................................................................................................................106 9.1. The for loop ...................................................................................................................................106 9.1.1. How does it work? ...............................................................................................................106 9.1.2. Examples.............................................................................................................................106 9.2. The while loop..............................................................................................................................107 9.2.1. What is it? ............................................................................................................................107 9.2.2. Examples.............................................................................................................................107 9.3. The until loop................................................................................................................................110 9.3.1. What is it? ............................................................................................................................110 9.3.2. Example...............................................................................................................................110 9.4. I/0 redirection and loops...............................................................................................................111 9.4.1. Input redirection..................................................................................................................111 9.4.2. Output redirection ................................................................................................................112 9.5. Break and continue.......................................................................................................................112 9.5.1. The break builtin...............................................................................................................112 9.5.2. The continue builtin..........................................................................................................114 9.5.3. Examples.............................................................................................................................114 9.6. Making menus with the select builtin.........................................................................................115 9.6.1. General................................................................................................................................115 9.6.2. Submenus............................................................................................................................116 9.7. The shift builtin..........................................................................................................................116 9.7.1. What does it do?..................................................................................................................117 9.7.2. Examples.............................................................................................................................117 9.8. Summary.......................................................................................................................................117 9.9. Exercises.......................................................................................................................................118 Chapter 10. More on variables......................................................................................................................119 10.1. Types of variables.......................................................................................................................119 10.1.1. General assignment of values............................................................................................119 10.1.2. Using the declare builtin.................................................................................................119 iv
Table of Contents
Chapter 10. More on variables 10.1.3. Constants...........................................................................................................................120 10.2. Array variables............................................................................................................................121 10.2.1. Creating arrays..................................................................................................................121 10.2.2. Dereferencing the variables in an array .............................................................................122 10.2.3. Deleting array variables .....................................................................................................122 10.2.4. Examples of arrays............................................................................................................122 10.3. Operations on variables ...............................................................................................................125 10.3.1. Arithmetic on variables.....................................................................................................125 10.3.2. Length of a variable ...........................................................................................................125 10.3.3. Transformations of variables.............................................................................................125 10.4. Summary.....................................................................................................................................128 10.5. Exercises.....................................................................................................................................128 Chapter 11. Functions....................................................................................................................................129 11.1. Introduction.................................................................................................................................129 11.1.1. What are functions?...........................................................................................................129 11.1.2. Function syntax.................................................................................................................129 11.1.3. Positional parameters in functions....................................................................................130 11.1.4. Displaying functions ..........................................................................................................130 11.1.5. Examples of functions in scripts.......................................................................................130 11.2. Summary.....................................................................................................................................133 11.3. Exercises.....................................................................................................................................133 Chapter 12. Catching signals.........................................................................................................................134 12.1. Signals.........................................................................................................................................134 12.1.1. Introduction.......................................................................................................................134 12.1.2. Usage of signals with kill..................................................................................................135 12.2. Traps...........................................................................................................................................136 12.2.1. General..............................................................................................................................136 12.2.2. How Bash interprets traps.................................................................................................136 12.2.3. More examples..................................................................................................................137 12.3. Summary.....................................................................................................................................137 12.4. Exercises.....................................................................................................................................137 Appendix A. Shell Features ............................................................................................................................138 A.1. Common features.........................................................................................................................138 A.2. Differing features.........................................................................................................................139 Appendix B. GNU Free Documentation License.........................................................................................142 B.1. Preamble.......................................................................................................................................142 B.2. Applicability and definitions........................................................................................................142 B.3. Verbatim copying.........................................................................................................................143 B.4. Copying in quantity......................................................................................................................143 B.5. Modifications...............................................................................................................................144 B.6. Combining documents.................................................................................................................145 B.7. Collections of documents.............................................................................................................145 B.8. Aggregation with independent works..........................................................................................146 v
Table of Contents
Appendix B. GNU Free Documentation License B.9. Translation ....................................................................................................................................146 B.10. Termination................................................................................................................................146 B.11. Future revisions of this license...................................................................................................146 B.12. How to use this License for your documents.............................................................................147 Glossary...........................................................................................................................................................148 A ...........................................................................................................................................................148 B...........................................................................................................................................................148 C...........................................................................................................................................................148 D ...........................................................................................................................................................149 E...........................................................................................................................................................150 F...........................................................................................................................................................150 G ...........................................................................................................................................................150 H ...........................................................................................................................................................151 I............................................................................................................................................................151 J............................................................................................................................................................151 K ...........................................................................................................................................................152 L...........................................................................................................................................................152 M..........................................................................................................................................................153 N ...........................................................................................................................................................153 P...........................................................................................................................................................154 Q ...........................................................................................................................................................154 R...........................................................................................................................................................154 S...........................................................................................................................................................155 T...........................................................................................................................................................155 U ...........................................................................................................................................................156 V ...........................................................................................................................................................156 W..........................................................................................................................................................157 X ...........................................................................................................................................................157 Z...........................................................................................................................................................158
vi
Introduction
1. Why this guide?
The primary reason for writing this document is that a lot of readers feel the existing HOWTO to be too short and incomplete, while the Bash Scripting guide is too much of a reference work. There is nothing in between these two extremes. I also wrote this guide on the general principal that not enough free basic courses are available, though they should be. This is a practical guide which tries to give reallife instead of theoretical examples. I partly wrote it because I don't get excited with stripped down and oversimplified examples written by people who know what they are talking about, showing some really cool Bash feature so much out of its context that you cannot ever use it in practical circumstances. You can read that sort of stuff after finishing this book, which contains exercises and examples that will help you survive in the real world.
4. Revision History
Revision History Revision 1.0 20040426 Revised by: TM Initial release for LDP; more exercises, more markup, less errors and abuse; added glossary. Revision 1.0beta 20030420 Revised by: MG Prerelease
Introduction
5. Contributions
Thanks to all the friends who helped (or tried to) and to my husband. Thanks to all the people who submitted bug reports among many, many others Hans Bol and Mike Sim. Special thanks to Tabatha Marshall, who volunteered to do a complete review and spell and grammar check.
6. Feedback
Missing information, missing links, missing characters? Mail it to <[email protected]> the maintainer of this document.
7. Copyright information
Copyright 2003 Machtelt Garrels. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being "New versions of this document", "Contributions", "Feedback" and "Copyright information", with no FrontCover Texts and no BackCover Texts. A copy of the license is included in Appendix B entitled "GNU Free Documentation License". The author and publisher have made every effort in the preparation of this book to ensure the accuracy of the information. However, the information contained in this book is offered without warranty, either express or implied. Neither the author nor the publisher nor any dealer or distributor will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. The logos, trademarks and symbols used in this book are the properties of their respective owners.
Introduction
command VARIABLE option argument command options arguments filename Key Button Menu>Choice Terminology
\
Meaning Quotes from people, quoted computer output. Literal computer input and output captured from the terminal, usually rendered with a light grey background. Name of a command that can be entered on the command line. Name of a variable or pointer to content of a variable, as in $VARNAME. Option to a command, as in "the a option to the ls command". Argument to a command, as in "read man ls". Command synopsis or general usage, on a separated line. Name of a file or directory, for example "Change to the /usr/bin directory." Keys to hit on the keyboard, such as "type Q to quit". Graphical button to click, like the OK button. Choice to select from a graphical menu, for instance: "Select Help>About Mozilla in your browser." Important term or concept: "The Linux kernel is the heart of the system." The backslash in a terminal view or command synopsis indicates an unfinished line. In other words, if you see a long command that is cut into multiple lines, \ means "Don't press Enter yet!" link to related subject within this guide. Clickable link to an external web resource.
Introduction
Bash Guide for Beginners Chapter 10: Advanced variables: specifying variable types, introduction to arrays of variables, operations on variables. Chapter 11: Functions: an introduction. Chapter 12: Catching signals: introduction to process signalling, trapping usersent signals.
Introduction
Your default shell is set in the /etc/passwd file, like this line for user mia:
mia:L2NOfqdlPrHwE:504:504:Mia Maya:/home/mia:/bin/bash
To switch from one shell to another, just enter the name of the new shell in the active terminal. The system finds the directory where the name occurs using the PATH settings, and since a shell is an executable file (program), the current shell activates it and it gets executed. A new prompt is usually shown, because each shell has its typical appearance:
mia:~> tcsh [mia@post21 ~]$
Interactive means you can enter commands. The shell is not running because a script has been activated. A login shell means that you got the shell after authenticating to the system, usually by giving your user name and password. Chapter 1. Bash and Bash scripts 6
Bash Guide for Beginners Files read: /etc/profile ~/.bash_profile, ~/.bash_login or ~/.profile: first existing readable file is read ~/.bash_logout upon logout. Error messages are printed if configuration files exist but are not readable. If a file does not exist, bash searches for the next.
1.2.2.2.2. Invoked as an interactive nonlogin shell
A nonlogin shell means that you did not have to authenticate to the system. For instance, when you open a terminal using an icon, or a menu item, that is a nonlogin shell. Files read: ~/.bashrc This file is usually referred to in ~/.bash_profile: if [ f ~/.bashrc ]; then . ~/.bashrc; fi See Chapter 7 for more information on the if construct.
1.2.2.2.3. Invoked noninteractively
All scripts use noninteractive shells. They are programmed to do certain tasks and cannot be instructed to do other jobs than those for which they are programmed. Files read: defined by BASH_ENV PATH is not used to search for this file, so if you want to use it, best refer to it by giving the full path and file name.
1.2.2.2.4. Invoked with the sh command
Bash tries to behave as the historical Bourne sh program while conforming to the POSIX standard as well. Files read: /etc/profile ~/.profile When invoked interactively, the ENV variable can point to extra startup information.
This option is enabled either using the set builtin: set o posix or by calling the bash program with the posix option. Bash will then try to behave as compliant as possible to the POSIX standard for shells. Setting the POSIXLY_CORRECT variable does the same. Files read: defined by ENV variable.
1.2.2.2.6. Invoked remotely
Files read when invoked by rshd: ~/.bashrc Avoid use of rtools Be aware of the dangers when using tools such as rlogin, telnet, rsh and rcp. They are intrinsically insecure because confidential data is sent over the network unencrypted. If you need tools for remote execution, file transfer and so on, use an implementation of Secure SHell, generally known as SSH, freely available from http://www.openssh.org. Different client programs are available for nonUNIX systems as well, see your local software mirror.
1.2.2.2.7. Invoked when UID is not equal to EUID
An interactive shell generally reads from, and writes to, a user's terminal: input and output are connected to a terminal. Bash interactive behavior is started when the bash command is called upon without nonoption arguments, except when the option is a string to read from or when the shell is invoked to read from standard input, which allows for positional parameters to be set (see Chapter 3 ).
1.2.2.3.2. Is this shell interactive?
Test by looking at the content of the special parameter , it contains an 'i' when the shell is interactive:
eddy:~> echo $ himBH
Differences in interactive mode: Bash reads startup files. Job control enabled by default. Prompts are set, PS2 is enabled for multiline commands, it is usually set to ">". This is also the prompt you get when the shell thinks you entered an unfinished command, for instance when you forget quotes, command structures that cannot be left out, etc. Commands are by default read from the command line using readline. Bash interprets the shell option ignoreeof instead of exiting immediately upon receiving EOF (End Of File). Command history and history expansion are enabled by default. History is saved in the file pointed to by HISTFILE when the shell exits. By default, HISTFILE points to ~/.bash_history. Alias expansion is enabled. In the absence of traps, the SIGTERM signal is ignored. In the absence of traps, SIGINT is caught and handled. Thus, typing Ctrl+C, for example, will not quit your interactive shell. Sending SIGHUP signals to all jobs on exit is configured with the huponexit option. Commands are executed upon read. Bash checks for mail periodically. Bash can be configured to exit when it encounters unreferenced variables. In interactive mode this behavior is disabled. When shell builtin commands encounter redirection errors, this will not cause the shell to exit. Special builtins returning errors when used in POSIX mode don't cause the shell to exit. The builtin commands are listed in Section 1.3. Failure of exec will not exit the shell. Parser syntax errors don't cause the shell to exit. Simple spell check for the arguments to the cd builtin is enabled by default. Automatic exit after the length of time specified in the TMOUT variable has passed, is enabled. More information: Section 3.2 Section 3.6 See Chapter 12 for more about signals. Section 3.4 discusses the various expansions performed upon entering a command. 1.2.2.4. Conditionals Conditional expressions are used by the [[ compound command and by the test and [ builtin commands. Expressions may be unary or binary. Unary expressions are often used to examine the status of a file. You only need one object, for instance a file, to do the operation on. There are string operators and numeric comparison operators as well; these are binary operators, requiring two objects to do the operation on. If the FILE argument to one of the primaries is in the form /dev/fd/N, then file descriptor N is checked. If the FILE argument to one of the primaries is one of /dev/stdin, /dev/stdout or /dev/stderr, then file descriptor 0, 1 or 2 respectively is checked. Conditionals are discussed in detail in Chapter 7. Chapter 1. Bash and Bash scripts 9
Bash Guide for Beginners More information about the file descriptors in Section 8.2.3. 1.2.2.5. Shell arithmetic The shell allows arithmetic expressions to be evaluated, as one of the shell expansions or by the let builtin. Evaluation is done in fixedwidth integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their precedence and associativity are the same as in the C language, see Chapter 3. 1.2.2.6. Aliases Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias commands. Bash always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. The commands following the alias definition on that line are not affected by the new alias. Aliases are expanded when a function definition is read, not when the function is executed, because a function definition is itself a compound command. As a consequence, aliases defined in a function are not available until after that function is executed. We will discuss aliases in detail in Section 3.5. 1.2.2.7. Arrays Bash provides onedimensional array variables. Any variable may be used as an array; the declare builtin will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Arrays are zerobased. See Chapter 10. 1.2.2.8. Directory stack The directory stack is a list of recentlyvisited directories. The pushd builtin adds directories to the stack as it changes the current directory, and the popd builtin removes specified directories from the stack and changes the current directory to the directory removed. Content can be displayed issuing the dirs command or by checking the content of the DIRSTACK variable. More information about the workings of this mechanism can be found in the Bash info pages. 1.2.2.9. The prompt Bash makes playing with the prompt even more fun. See the section Controlling the Prompt in the Bash info pages.
10
Bash Guide for Beginners 1.2.2.10. The restricted shell When invoked as rbash or with the restricted or r option, the following happens: The cd builtin is disabled. Setting or unsetting SHELL, PATH, ENV or BASH_ENV is not possible. Command names can no longer contain slashes. Filenames containing a slash are not allowed with the . (source) builtin command. The hash builtin does not accept slashes with the p option. Import of functions at startup is disabled. SHELLOPTS is ignored at startup. Output redirection using >, >|, ><, >&, &> and >> is disabled. The exec builtin is disabled. The f and d options are disabled for the enable builtin. A default PATH cannot be specified with the command builtin. Turning off restricted mode is not possible. When a command that is found to be a shell script is executed, rbash turns off any restrictions in the shell spawned to execute the script. More information: Section 3.2 Section 3.6 Info Bash>Basic Shell Features>Redirections Section 8.2.3: advanced redirection
Bash Guide for Beginners Builtin commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities. Bash supports 3 types of builtin commands: Bourne Shell builtins: :, ., break, cd, continue, eval, exec, exit, export, getopts, hash, pwd, readonly, return, set, shift, test, [, times, trap, umask and unset. Bash builtin commands: alias, bind, builtin, command, declare, echo, enable, help, let, local, logout, printf, read, shopt, type, typeset, ulimit and unalias. Special builtin commands: When Bash is executing in POSIX mode, the special builtins differ from other builtin commands in three respects: 1. Special builtins are found before shell functions during command lookup. 2. If a special builtin returns an error status, a noninteractive shell exits. 3. Assignment statements preceding the command stay in effect in the shell environment after the command completes. The POSIX special builtins are :, ., break, continue, eval, exec, exit, export, readonly, return, set, shift, trap and unset. Most of these builtins will be discussed in the next chapters. For those commands for which this is not the case, we refer to the Info pages.
12
Bash Guide for Beginners The shell reads its input from a file, from a string or from the user's terminal. Input is broken up into words and operators, obeying the quoting rules, see Chapter 3. These tokens are separated by metacharacters. Alias expansion is performed. The shell parses (analyzes and substitutes) the tokens into simple and compound commands. Bash performs various shell expansions, breaking the expanded tokens into lists of filenames and commands and arguments. Redirection is performed if necessary, redirection operators and their operands are removed from the argument list. Commands are executed. Optionally the shell waits for the command to complete and collects its exit status. 1.4.1.2. Shell commands A simple shell command such as touch file1 file2 file3 consists of the command itself followed by arguments, separated by spaces. More complex shell commands are composed of simple commands arranged together in a variety of ways: in a pipeline in which the output of one command becomes the input of a second, in a loop or conditional construct, or in some other grouping. A couple of examples: ls | more gunzip file.tar.gz | tar xvf 1.4.1.3. Shell functions Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a "regular" command. When the name of a shell function is used as a simple command name, the list of commands associated with that function name is executed. Shell functions are executed in the current shell context; no new process is created to interpret them. Functions are explained in Chapter 11. 1.4.1.4. Shell parameters A parameter is an entity that stores values. It can be a name, a number or a special value. For the shell's purpose, a variable is a parameter that stores a name. A variable has a value and zero or more attributes. Variables are created with the declare shell builtin command. If no value is given, a variable is assigned the null string. Variables can only be removed with the unset builtin. Assigning variables is discussed in Section 3.2, advanced use of variables in Chapter 10. 1.4.1.5. Shell expansions Shell expansion is performed after each command line has been split into tokens. These are the expansions performed:
13
Bash Guide for Beginners Brace expansion Tilde expansion Parameter and variable expansion Command substitution Arithmetic expansion Word splitting Filename expansion We'll discuss these expansion types in detail in Section 3.4. 1.4.1.6. Redirections Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. Redirection may also be used to open and close files for the current shell execution environment. 1.4.1.7. Executing commands When executing a command, the words that the parser has marked as variable assignments (preceding the command name) and redirections are saved for later reference. Words that are not variable assignments or redirections are expanded; the first remaining word after expansion is taken to be the name of the command and the rest are arguments to that command. Then redirections are performed, then strings assigned to variables are expanded. If no command name results, variables will affect the current shell environment. An important part of the tasks of the shell is to search for commands. Bash does this as follows: Check whether the command contains slashes. If not, first check with the function list to see if it contains a command by the name we are looking for. If command is not a function, check for it in the builtin list. If command is neither a function nor a builtin, look for it analyzing the directories listed in PATH. Bash uses a hash table (data storage area in memory) to remember the full path names of executables so extensive PATH searches can be avoided. If the search is unsuccessful, bash prints an error message and returns an exit status of 127. If the search was successful or if the command contains slashes, the shell executes the command in a separate execution environment. If execution fails because the file is not executable and not a directory, it is assumed to be a shell script. If the command was not begun asynchronously, the shell waits for the command to complete and collects its exit status. 1.4.1.8. Shell scripts When a file containing shell commands is used as the first nonoption argument when invoking Bash (without c or s, this will create a noninteractive shell. This shell first searches for the script file in the current directory, then looks in PATH if the file cannot be found there.
14
1.5.2. Structure
The structure of a shell script is very flexible. Even though in Bash a lot of freedom is granted, you must ensure correct logic, flow control and efficiency so that users executing the script can do so easily and correctly. When starting on a new script, ask yourself the following questions: Will I be needing any information from the user or from the user's environment? How will I store that information? Are there any files that need to be created? Where and with which permissions and ownerships? What commands will I use? When using the script on different systems, do all these systems have these commands in the required versions? Does the user need any notifications? When and why?
1.5.3. Terminology
The table below gives an overview of programming terms that you need to be familiar with:
Table 11. Overview of programming terms Term Command control Conditional branch Logic flow Loop User input What is it? Testing exit status of a command in order to determine whether a portion of the program should be executed. Logical point in the program when a condition determines what happens next. The overall design of the program. Determines logical sequence of tasks so that the result is successful and controlled. Part of the program that is performed zero or more times. Information provided by an external source while the program is running, can be stored and recalled when needed.
15
Bash Guide for Beginners A number of methods can be used; one of the most common is working with lists. Itemizing the list of tasks involved in a program allows you to describe each process. Individual tasks can be referenced by their item number. Using your own spoken language to pin down the tasks to be executed by your program will help you to create an understandable form of your program. Later, you can replace the everyday language statements with shell language words and constructs. The example below shows such a logic flow design. It describes the rotation of log files. This example shows a possible repetitive loop, controlled by the number of base log files you want to rotate: 1. Do you want to rotate logs? a. If yes: i. Enter directory name containing the logs to be rotated. ii. Enter base name of the log file. iii. Enter number of days logs should be kept. iv. Make settings permanent in user's crontab file. b. If no, go to step 3. 2. Do you want to rotate another set of logs? a. If yes: repeat step 1. b. If no: go to step 3. 3. Exit The user should provide information for the program to do something. Input from the user must be obtained and stored. The user should be notified that his crontab will change.
16
A script always starts with the same two characters, "#!". After that, the shell that will execute the commands following the first line is defined. This script starts with clearing the screen on line 2. Line 3 makes it print a message, informing the user about what is going to happen. Line 5 greets the user. Lines 6, 9, 13, 16 and 20 are only there for orderly output display purposes. Line 8 prints the current date and the number of the week. Line 11 is again an informative message, like lines 3, 18 and 22. Line 12 formats the output of the w; line 15 shows operating system and CPU information. Line 19 gives the uptime and load information. Both echo and printf are Bash builtin commands. The first always exits with a 0 status, and simply prints arguments followed by an end of line character on the standard output, while the latter allows for definition of a formatting string and gives a nonzero exit status code upon failure. This is the same script using the printf builtin:
tom:~> cat mysystem.sh #!/bin/bash clear printf "This is information provided by mysystem.sh. printf "Hello, $USER.\n\n" printf "Today's date is `date`, this is week `date +"%V"`.\n\n" printf "These users are currently connected:\n" w | cut d " " f 1 | grep v USER | sort u printf "\n" printf "This is `uname s` running on a `uname m` processor.\n\n" printf "This is the uptime information:\n" uptime printf "\n" printf "That's all folks!\n"
Creating user friendly scripts by means of inserting messages is treated in Chapter 8. Standard location of the Bourne Again shell This implies that the bash program is installed in /bin. If stdout is not available If you execute a script from cron, supply full path names and redirect output and errors. Since the shell runs in noninteractive mode, any errors will cause the script to exit prematurely if you don't think about this. The following chapters will discuss the details of the above scripts.
17
The case statement often used in this kind of script is described in Section 7.2.5.
1.6. Summary
Bash is the GNU shell, compatible with the Bourne shell and incorporating many useful features from other shells. When the shell is started, it reads its configuration files. The most important are: /etc/profile ~/.bash_profile ~/.bashrc Bash behaves different when in interactive mode and also has a POSIX compliant and a restricted mode. Shell commands can be split up in three groups: the shell functions, shell builtins and existing commands in a directory on your system. Bash supports additional builtins not found in the plain Bourne shell. Shell scripts consist of these commands arranged as shell syntax dictates. Scripts are read and executed line per line and should have a logical structure. Chapter 1. Bash and Bash scripts 18
1.7. Exercises
These are some exercises to warm you up for the next chapter: 1. Where is the bash program located on your system? 2. Use the version option to find out which version you are running. 3. Which shell configuration files are read when you login to your system using the graphical user interface and then opening a terminal window? 4. Are the following shells interactive shells? Are they login shells? A shell opened by clicking on the background of your graphical desktop, selecting "Terminal" or such from a menu. A shell that you get after issuing the command ssh localhost. A shell that you get when logging in to the console in text mode. A shell obtained by the command xterm &. A shell opened by the mysystem.sh script. A shell that you get on a remote host, for which you didn't have to give the login and/or password because you use SSH and maybe SSH keys. 5. Can you explain why bash does not exit when you type Ctrl+C on the command line? 6. Display directory stack content. 7. If it is not yet the case, set your prompt so that it displays your location in the file system hierarchy, for instance add this line to ~/.bashrc: export PS1="\u@\h \w> " 8. Display hashed commands for your current shell session. 9. How many processes are currently running on your system? Use ps and wc, the first line of output of ps is not a process! 10. How to display the system hostname? Only the name, nothing more!
19
2.1.2. script1.sh
In this example we use the echo Bash builtin to inform the user about what is going to happen, before the task that will create the output is executed. It is strongly advised to inform users about what a script is doing, in order to prevent them from becoming nervous because the script is not doing anything. We will return to the subject of notifying users in Chapter 8.
20
Write this script for yourself as well. It might be a good idea to create a directory ~/scripts to hold your scripts. Add the directory to the contents of the PATH variable: export PATH="$PATH:~/scripts" If you are just getting started with Bash, use a text editor that uses different colours for different shell constructs. Syntax highlighting is supported by vim, gvim, (x)emacs, kwrite and many other editors; check the documentation of your favorite editor. Different prompts The prompts throughout this course vary depending on the author's mood. This resembles much more real life situations than the standard educational $ prompt. The only convention we stick to, is that the root prompt ends in a hash mark (#).
21
I'm setting two variables now. This is a string: black And this is a number: 9 I'm giving you back your prompt now. willy:~/scripts> echo $COLOUR willy:~/scripts> echo $VALUE willy:~/scripts>
This is the most common way to execute a script. It is preferred to execute the script like this in a subshell. The variables, functions and aliases created in this subshell are only known to the particular bash session of that subshell. When that shell exits and the parent regains control, everything is cleaned up and all changes to the state of the shell made by the script, are forgotten. If you did not put the scripts directory in your PATH, and . (the current directory) is not in the PATH either, you can activate the script like this: ./script_name.sh A script can also explicitly be executed by a given shell, but generally we only do this if we want to obtain special behavior, such as checking if the script works with another shell or printing traces for debugging: rbash script_name.sh sh script_name.sh bash x script_name.sh The specified shell will start as a subshell of your current shell and execute the script. This is done when you want the script to start up with specific options or under specific conditions which are not specified in the script. If you don't want to start a new shell but execute the script in the current shell, you source it: source script_name.sh Chapter 2. Writing and debugging scripts 22
Bash Guide for Beginners source = . The Bash source builtin is a synonym for the Bourne shell . (dot) command. The script does not need execute permission in this case. Commands are executed in the current shell context, so any changes made to your environment will be visible when the script finishes execution:
willy:~/scripts> source script1.sh output ommitted willy:~/scripts> echo $VALUE 9 willy:~/scripts>
23
echo "I will now fetch you a list of connected users:" echo w # show who is logged on and echo # what they are doing echo "I'm setting two variables now." COLOUR="black" VALUE="9" echo "This is a string: $COLOUR" echo "And this is a number: $VALUE" echo echo "I'm giving you back your prompt now." echo
# # # #
set a local shell variable set a local shell variable display content of variable display content of variable
In a decent script, the first lines are usually comment about what to expect. Then each big chunk of commands will be commented as needed for clarity's sake. Linux init scripts, as an example, in your system's init.d directory, are usually well commented since they have to be readable and editable by everyone running Linux.
4 users, load average: 0.58, 0.62, 0.40 LOGIN@ IDLE JCPU PCPU WHAT Sat 2pm 5:36m 0.24s 0.05s bash Sat 2pm ? 0.00s ?
24
+ echo 'I'\''m setting two variables now.' I'm setting two variables now. + COLOUR=black + VALUE=9 + echo 'This is a string: ' This is a string: + echo 'And this is a number: ' And this is a number: + echo + echo 'I'\''m giving you back your prompt now.' I'm giving you back your prompt now. + echo
4 users, load average: 0.79, 0.39, 0.33 LOGIN@ IDLE JCPU PCPU WHAT Sat 2pm 5:47m 0.24s 0.05s bash Sat 2pm ? 0.00s ? Sat 2pm 54:02 36.88s 36.88s BitchX willyke Sat 2pm 54:02 0.13s 0.06s /usr/bin/screen
I'm setting two variables now. This is a string: And this is a number: I'm giving you back your prompt now. willy: ~/scripts>
You can switch debugging mode on and off as many times as you want within the same script. The table below gives an overview of other useful Bash options: Chapter 2. Writing and debugging scripts 25
Bash Guide for Beginners Table 21. Overview of set debugging options Short notation set f set v set x Long notation set o noglob set o verbose set o xtrace Result Disable file name generation using metacharacters (globbing). Prints shell input lines as they are read. Print command traces before executing command.
The dash is used to activate a shell option and a plus to deactivate it. Don't let this confuse you! In the example below, we demonstrate these options on the command line:
willy:~/scripts> set v willy:~/scripts> ls ls commentedscripts.sh willy:~/scripts> set +v set +v willy:~/scripts> ls * commentedscripts.sh willy:~/scripts> set f willy:~/scripts> ls * ls: *: No such file or directory willy:~/scripts> touch * willy:~/scripts> ls * commentedscripts.sh willy:~/scripts> rm * willy:~/scripts> ls commentedscripts.sh
script1.sh
script1.sh
script1.sh
script1.sh
Alternatively, these modes can be specified in the script itself, by adding the desired options to the first line shell declaration. Options can be combined, as is usually the case with UNIX commands: #!/bin/bash xv Once you found the buggy part of your script, you can add echo statements before each command of which you are unsure, so that you will see exactly where and why things don't work. In the example commentedscript1.sh script, it could be done like this, still assuming that the displaying of users gives us problems:
echo "debug message: now attempting to start w command"; w
In more advanced scripts, the echo can be inserted to display the content of variables at different stages in the script, so that flaws can be detected:
echo "Variable VARNAME is now set to $VARNAME."
26
2.4. Summary
A shell script is a reusable series of commands put in an executable text file. Any text editor can be used to write scripts. Scripts start with #! followed by the path to the shell executing the commands from the script. Debugging a script can be done using shell options.
2.5. Exercises
This exercise will help you to create your first script. 1. Write a script using your favorite editor. The script should display the path to your homedirectory and the terminal type that you are using. Additionally it shows all the services started up in runlevel 3 on your system. (hint: use HOME, TERM and ls /etc/rc3.d/S*) 2. Add comments in your script. 3. Add information for the users of your script. 4. Change permissions on your script so that you can run it. 5. Run the script in normal mode and in debug mode. It should run without errors. 6. Make errors in your script: see what happens if you misspell commands, if you leave out the first line or put something unintelligible there, or if you misspell shell variable names or write them in lower case characters after they have been declared in capitals. Check what the debug comments say about this.
27
28
This configuration file sets some basic shell environment variables as well as some variables required by users running Java and/or Java applications in their web browser. See Section 3.2. See Chapter 7 for more on the conditional if used in this file; Chapter 9 discusses loops such as the for construct. The Bash source contains sample profile files for general or individual use. These and the one in the example above need changes in order for them to work in your environment! 3.1.1.2. /etc/bashrc On systems offering multiple types of shells, it might be better to put Bashspecific configurations in this file, since /etc/profile is also read by other shells, such as the Bourne shell. Errors generated by shells that don't understand the Bash syntax are prevented by splitting the configuration files for the different types of shells. In such cases, the user's ~/.bashrc might point to /etc/bashrc in order to include it in the shell initialization process upon login. You might also find that /etc/profile on your system only holds shell environment and program startup settings, while /etc/bashrc contains systemwide definitions for shell functions and aliases. The /etc/bashrc file might be referred to in /etc/profile or in individual user shell initialization files. The source contains sample bashrc files, or you might find a copy in /usr/share/doc/bash2.05b/startupfiles. This is part of the bashrc that comes with the Bash documentation:
alias alias alias alias ll='ls l' dir='ls ba' c='clear' ls='ls color'
29
Apart from general aliases, it contains useful aliases which make commands work even if you misspell them. We will discuss aliases in Section 3.5.2. This file contains a function, pskill; functions will be studied in detail in Chapter 11.
This user configures the backspace character for login on different operating systems. Apart from that, the user's .bashrc and .bash_login are read.
30
Bash Guide for Beginners 3.1.2.2. ~/.bash_login This file contains specific settings that are normally only executed when you log in to the system. In the example, we use it to configure the umask value and to show a list of connected users upon login. This user also gets the calendar for the current month:
####################################################################### # # # Bash_login file # # # # commands to perform from the bash shell at login time # # (sourced from .bash_profile) # # # ####################################################################### # file protection umask 002 # all to me, read to group and others # miscellaneous w cal `date +"%m"` `date +"%Y"`
In the absence of ~/.bash_profile, this file will be read. 3.1.2.3. ~/.profile In the absence of ~/.bash_profile and ~/.bash_login, ~/.profile is read. It can hold the same configurations, which are then also accessible by other shells. Mind that other shells might not understand the Bash syntax. 3.1.2.4. ~/.bashrc Today, it is more common to use a nonlogin shell, for instance when logged in graphically using X terminal windows. Upon opening such a window, the user does not have to provide a user name or password; no authentication is done. Bash searches for ~/.bashrc when this happens, so it is referred to in the files read upon login as well, which means you don't have to enter the same settings in multiple files. In this user's .bashrc a couple of aliases are defined and variables for specific programs are set after the systemwide /etc/bashrc is read:
franky ~> cat .bashrc # /home/franky/.bashrc # Source global definitions if [ f /etc/bashrc ]; then . /etc/bashrc fi # shell options set o noclobber # my shell variables export PS1="\[\033[1;44m\]\u \w\[\033[0m\] " export PATH="$PATH:~/bin:~/scripts"
31
More examples can be found in the Bash package. Remember that sample files might need changes in order to work in your environment. Aliases are discussed in Section 3.5. 3.1.2.5. ~/.bash_logout This file contains specific instructions for the logout procedure. In the example, the terminal window is cleared upon logout. This is useful for remote connections, which will leave a clean window after closing them.
franky ~> cat .bash_logout ####################################################################### # # # Bash_logout file # # # # commands to perform from the bash shell at logout time # # # ####################################################################### clear franky ~>
32
Most shell scripts execute in a private environment: variables are not inherited by child processes unless they are exported by the parent shell. Sourcing a file containing shell commands is a way of applying changes to your own environment and setting variables in the current shell. This example also demonstrates the use of different prompt settings by different users. In this case, red means danger. When you have a green prompt, don't worry too much. Note that source resourcefile is the same as . resourcefile. Should you get lost in all these configuration files, and find yourself confronted with settings of which the origin is not clear, use echo statements, just like for debugging scripts; see Section 2.3.2. You might add lines like this:
echo "Now executing .bash_profile.."
or like this:
echo "Now setting PS1 in .bashrc:" export PS1="[some value]" echo "PS1 is now set to $PS1"
3.2. Variables
3.2.1. Types of variables
As seen in the examples above, shell variables are in uppercase characters by convention. Bash keeps a list of two types of variables: 3.2.1.1. Global variables Global variables or environment variables are available in all shells. The env or printenv commands can be used to display environment variables. These programs come with the shutils package.
33
franky ~> printenv CC=gcc CDPATH=.:~:/usr/local:/usr:/ CFLAGS=O2 fomitframepointer COLORTERM=gnometerminal CXXFLAGS=O2 fomitframepointer DISPLAY=:0 DOMAIN=hq.soti.org e= TOR=vi FCEDIT=vi FIGNORE=.o:~ G_BROKEN_FILENAMES=1 GDK_USE_XFT=1 GDMSESSION=Default GNOME_DESKTOP_SESSION_ID=Default GTK_RC_FILES=/etc/gtk/gtkrc:/nethome/franky/.gtkrc1.2gnome2 GWMCOLOR=darkgreen GWMTERM=xterm HISTFILESIZE=5000 history_control=ignoredups HISTSIZE=2000 HOME=/nethome/franky HOSTNAME=octarine.hq.soti.org INPUTRC=/etc/inputrc IRCNAME=franky JAVA_HOME=/usr/java/j2sdk1.4.0 LANG=en_US LDFLAGS=s LD_LIBRARY_PATH=/usr/lib/mozilla:/usr/lib/mozilla/plugins LESSCHARSET=latin1 LESS=edfMQ LESSOPEN=|/usr/bin/lesspipe.sh %s LEX=flex LOCAL_MACHINE=octarine LOGNAME=franky LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41: MACHINES=octarine MAILCHECK=60 MAIL=/var/mail/franky MANPATH=/usr/man:/usr/share/man/:/usr/local/man:/usr/X11R6/man MEAN_MACHINES=octarine MOZ_DIST_BIN=/usr/lib/mozilla MOZILLA_FIVE_HOME=/usr/lib/mozilla MOZ_PROGRAM=/usr/lib/mozilla/mozillabin MTOOLS_FAT_COMPATIBILITY=1 MYMALLOC=0 NNTPPORT=119 NNTPSERVER=news NPX_PLUGIN_PATH=/plugin/ns4plugin/:/usr/lib/netscape/plugins OLDPWD=/nethome/franky OS=Linux PAGER=less PATH=/nethome/franky/bin.Linux:/nethome/franky/bin:/usr/local/bin:/usr/local/sbin:/usr/X11R6/bin: PS1=\[\033[1;44m\]franky is in \w\[\033[0m\] PS2=More input> PWD=/nethome/franky SESSION_MANAGER=local/octarine.hq.soti.org:/tmp/.ICEunix/22106 SHELL=/bin/bash SHELL_LOGIN=login
34
SHLVL=2 SSH_AGENT_PID=22161 SSH_ASKPASS=/usr/libexec/openssh/gnomesshaskpass SSH_AUTH_SOCK=/tmp/sshXXmhQ4fC/agent.22106 START_WM=twm TERM=xterm TYPE=type USERNAME=franky USER=franky _=/usr/bin/printenv VISUAL=vi WINDOWID=20971661 XAPPLRESDIR=/nethome/franky/appdefaults XAUTHORITY=/nethome/franky/.Xauthority XENVIRONMENT=/nethome/franky/.Xdefaults XFILESEARCHPATH=/usr/X11R6/lib/X11/%L/%T/%N%C%S:/usr/X11R6/lib/X11/%l/%T/%N%C%S:/usr/X11R6/lib/X1 XKEYSYMDB=/usr/X11R6/lib/X11/XKeysymDB XMODIFIERS=@im=none XTERMID= XWINHOME=/usr/X11R6 X=X11R6 YACC=bison y
3.2.1.2. Local variables Local variables are only available in the current shell. Using the set builtin command without any options will display a list of all variables (including environment variables) and functions. The output will be sorted according to the current locale and displayed in a reusable format. Below is a diff file made by comparing printenv and set output, after leaving out the functions which are also displayed by the set command:
franky ~> diff set.sorted printenv.sorted | grep "<" | awk '{ print $2 }' BASE=/nethome/franky/.Shell/hq.soti.org/octarine.aliases BASH=/bin/bash BASH_VERSINFO=([0]="2" BASH_VERSION='2.05b.0(1)release' COLUMNS=80 DIRSTACK=() DO_FORTUNE= EUID=504 GROUPS=() HERE=/home/franky HISTFILE=/nethome/franky/.bash_history HOSTTYPE=i686 IFS=$' LINES=24 MACHTYPE=i686pclinuxgnu OPTERR=1 OPTIND=1 OSTYPE=linuxgnu PIPESTATUS=([0]="0") PPID=10099 PS4='+ PWD_REAL='pwd SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactivecomments:monitor THERE=/home/franky UID=504
35
Bash Guide for Beginners Awk the GNU Awk programming language is explained in Chapter 6. 3.2.1.3. Variables by content Apart from dividing variables in local and global variables, we can also divide them in categories according to the sort of content the variable contains. In this respect, variables come in 4 types: String variables Integer variables Constant variables Array variables We'll discuss these types in Chapter 10. For now, we will work with integer and string values for our variables.
To set a variable in the shell, use VARNAME="value" Putting spaces around the equal sign will cause errors. It is a good habit to quote content strings when assigning values to variables: this will reduce the chance that you make errors. Some examples using upper and lower cases, numbers and spaces:
franky ~> MYVAR1="2" franky ~7gt; echo $MYVAR1 2 franky ~> first_name="Franky" franky ~> echo $first_name Franky franky ~> full_name="Franky M. Singh" franky ~> echo $full_name Franky M. Singh franky ~> MYVAR2="2" bash: MYVAR2=2: command not found franky ~> MYVAR1 ="2"
36
franky ~> exit franky ~> export full_name franky ~> bash franky ~> echo $full_name Franky M. Singh franky ~> export full_name="Charles the Great" franky ~> echo $full_name Charles the Great franky ~> exit franky ~> echo $full_name Franky M. Singh franky ~>
When first trying to read the value of full_name in a subshell, it is not there (echo shows a null string). The subshell quits, and full_name is exported in the parent a variable can be exported after it has been assigned a value. Then a new subshell is started, in which the variable exported from the parent is visible. The variable is changed to hold another name, but the value for this variable in the parent stays the same.
37
Table 31. Reserved Bourne shell variables Variable name CDPATH HOME IFS MAIL MAILPATH OPTARG OPTIND PATH PS1 PS2 Definition A colonseparated list of directories used as a search path for the cd builtin command. The current user's home directory; the default for the cd builtin. The value of this variable is also used by tilde expansion. A list of characters that separate fields; used when the shell splits words as part of expansion. If this parameter is set to a file name and the MAILPATH variable is not set, Bash informs the user of the arrival of mail in the specified file. A colonseparated list of file names which the shell periodically checks for new mail. The value of the last option argument processed by the getopts builtin. The index of the last option argument processed by the getopts builtin. A colonseparated list of directories in which the shell looks for commands. The primary prompt string. The default value is "'\s\v\$ '". The secondary prompt string. The default value is "'> '".
3.2.4.2. Bash reserved variables These variables are set or used by Bash, but other shells do not normally treat them specially.
Table 32. Reserved Bash variables Variable name auto_resume BASH BASH_ENV BASH_VERSION BASH_VERSINFO COLUMNS COMP_CWORD Definition This variable controls how the shell interacts with the user and job control. The full pathname used to execute the current instance of Bash. If this variable is set when Bash is invoked to execute a shell script, its value is expanded and used as the name of a startup file to read before executing the script. The version number of the current instance of Bash. A readonly array variable whose members hold version information for this instance of Bash. Used by the select builtin to determine the terminal width when printing selection lists. Automatically set upon receipt of a SIGWINCH signal. An index into ${COMP_WORDS} of the word containing the current cursor position. 38
Bash Guide for Beginners COMP_LINE COMP_POINT COMP_WORDS COMPREPLY DIRSTACK EUID FCEDIT FIGNORE FUNCNAME GLOBIGNORE GROUPS histchars HISTCMD HISTCONTROL HISTFILE HISTFILESIZE HISTIGNORE HISTSIZE HOSTFILE HOSTNAME HOSTTYPE IGNOREEOF INPUTRC LANG LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES LC_NUMERIC The current command line. The index of the current cursor position relative to the beginning of the current command. An array variable consisting of the individual words in the current command line. An array variable from which Bash reads the possible completions generated by a shell function invoked by the programmable completion facility. An array variable containing the current contents of the directory stack. The numeric effective user ID of the current user. The editor used as a default by the e option to the fc builtin command. A colonseparated list of suffixes to ignore when performing file name completion. The name of any currentlyexecuting shell function. A colonseparated list of patterns defining the set of file names to be ignored by file name expansion. An array variable containing the list of groups of which the current user is a member. Up to three characters which control history expansion, quick substitution, and tokenization. The history number, or index in the history list, of the current command. Defines whether a command is added to the history file. The name of the file to which the command history is saved. The default value is ~/.bash_history. The maximum number of lines contained in the history file, defaults to 500. A colonseparated list of patterns used to decide which command lines should be saved in the history list. The maximum number of commands to remember on the history list, default is 500. Contains the name of a file in the same format as /etc/hosts that should be read when the shell needs to complete a hostname. The name of the current host. A string describing the machine Bash is running on. Controls the action of the shell on receipt of an EOF character as the sole input. The name of the Readline initialization file, overriding the default /etc/inputrc. Used to determine the locale category for any category not specifically selected with a variable starting with LC_. This variable overrides the value of LANG and any other LC_ variable specifying a locale category. This variable determines the collation order used when sorting the results of file name expansion, and determines the behavior of range expressions, equivalence classes, and collating sequences within file name expansion and pattern matching. This variable determines the interpretation of characters and the behavior of character classes within file name expansion and pattern matching. This variable determines the locale used to translate doublequoted strings preceded by a "$" sign. This variable determines the locale category used for number formatting. 39
Bash Guide for Beginners LINENO The line number in the script or shell function currently executing. Used by the select builtin to determine the column length for printing selection LINES lists. A string that fully describes the system type on which Bash is executing, in the MACHTYPE standard GNU CPUCOMPANYSYSTEM format. How often (in seconds) that the shell should check for mail in the files specified in MAILCHECK the MAILPATH or MAIL variables. OLDPWD The previous working directory as set by the cd builtin. OPTERR If set to the value 1, Bash displays error messages generated by the getopts builtin. OSTYPE A string describing the operating system Bash is running on. An array variable containing a list of exit status values from the processes in the PIPESTATUS most recently executed foreground pipeline (which may contain only a single command). If this variable is in the environment when bash starts, the shell enters POSIX POSIXLY_CORRECT mode. PPID The process ID of the shell's parent process. If set, the value is interpreted as a command to execute before the printing of each PROMPT_COMMAND primary prompt (PS1). The value of this variable is used as the prompt for the select command. Defaults to PS3 "'#? '" The value is the prompt printed before the command line is echoed when the x PS4 option is set; defaults to "'+ '". PWD The current working directory as set by the cd builtin command. Each time this parameter is referenced, a random integer between 0 and 32767 is RANDOM generated. Assigning a value to this variable seeds the random number generator. REPLY The default variable for the read builtin. SECONDS This variable expands to the number of seconds since the shell was started. SHELLOPTS A colonseparated list of enabled shell options. SHLVL Incremented by one each time a new instance of Bash is started. The value of this parameter is used as a format string specifying how the timing TIMEFORMAT information for pipelines prefixed with the time reserved word should be displayed. If set to a value greater than zero, TMOUT is treated as the default timeout for the read builtin. In an interative shell, the value is interpreted as the number of TMOUT seconds to wait for input after issuing the primary prompt when the shell is interactive. Bash terminates after that number of seconds if input does not arrive. UID The numeric, real user ID of the current user. Check the Bash man, info or doc pages for extended information. Some variables are readonly, some are set automatically and some lose their meaning when set to a different value than the default.
40
Bash Guide for Beginners Table 33. Special bash variables Character $* $@ $# $? $ $$ $! $0 Definition Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. Expands to the number of positional parameters in decimal. Expands to the exit status of the most recently executed foreground pipeline. A hyphen expands to the current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the i). Expands to the process ID of the shell. Expands to the process ID of the most recently executed background (asynchronous) command. Expands to the name of the shell or shell script. The underscore variable is set at shell startup and contains the absolute file name of the shell or script being executed as passed in the argument list. Subsequently, it expands to the last argument to the previous command, after expansion. It is also set to the full pathname of each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.
$_
The positional parameters are the words following the name of a shell script. They are put into the variables $1, $2, $3 and so on. As long as needed, variables are added to an internal array. $# holds the total number of parameters, as is demonstrated with this simple script:
#!/bin/bash # positional.sh # This script reads 3 positional parameters and prints them out. POSPAR1="$1" POSPAR2="$2" POSPAR3="$3" echo echo echo echo echo "$1 is the first positional parameter, \$1." "$2 is the second positional parameter, \$2." "$3 is the third positional parameter, \$3." "The total number of positional parameters is $#."
41
More on evaluating these parameters is in Chapter 7 and Section 9.7. Some examples on the other special parameters:
franky ~> grep dictionary /usr/share/dict/words dictionary franky ~> echo $_ /usr/share/dict/words franky ~> echo $$ 10662 franky ~> mozilla & [1] 11064 franky ~> echo $! 11064 franky ~> echo $0 bash franky ~> ls doesnotexist ls: doesnotexist: No such file or directory franky ~> echo $? 0 franky ~> echo $? 1 franky ~>
User franky starts entering the idcommand, which results in the assignment of the _ variable. The process ID of his shell is 10662. After putting a job in the background, the ! holds the process ID of the backgrounded job. The shell running is bash. When a mistake is made, ? holds an exit code different from 0 (zero).
In this example, the variable date is created and set to hold a value. The first echo displays the value of the variable, but for the second, the dollar sign is escaped.
43
3.3.6. Locales
A doublequoted string preceded by a dollar sign will cause the string to be translated according to the current locale. If the current locale is "C" or "POSIX", the dollar sign is ignored. If the string is translated and replaced, the replacement is doublequoted.
Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces. To avoid conflicts with parameter expansion, the string "${" is not considered eligible for brace expansion. A correctlyformed brace expansion must contain unquoted opening and closing braces, and at least one unquoted comma. Any incorrectly formed brace expansion is left unchanged.
Bash Guide for Beginners name. If this login name is the null string, the tilde is replaced with the value of the HOME shell variable. If HOME is unset, the home directory of the user executing the shell is substituted instead. Otherwise, the tildeprefix is replaced with the home directory associated with the specified login name. If the tildeprefix is "~+", the value of the shell variable PWD replaces the tildeprefix. If the tildeprefix is "~", the value of the shell variable OLDPWD, if it is set, is substituted. If the characters following the tilde in the tildeprefix consist of a number N, optionally prefixed by a "+" or a "", the tildeprefix is replaced with the corresponding element from the directory stack, as it would be displayed by the dirs builtin invoked with the characters following tilde in the tildeprefix as an argument. If the tildeprefix, without the tilde, consists of a number without a leading "+" or "", "+" is assumed. If the login name is invalid, or the tilde expansion fails, the word is left unchanged. Each variable assignment is checked for unquoted tildeprefixes immediately following a ":" or "=". In these cases, tilde expansion is also performed. Consequently, one may use file names with tildes in assignments to PATH, MAILPATH, and CDPATH, and the shell assigns the expanded value. Example:
franky ~> export PATH="$PATH:~/testdir"
~/testdir will be expanded to $HOME/testdir, so if $HOME is /var/home/franky, the directory /var/home/franky/testdir will be added to the content of the PATH variable.
45
Note that this is not the same as echo $N*. The following construct allows for creation of the named variable if it does not yet exist: ${VAR:=value} Example:
franky ~> echo $FRANKY franky ~> echo ${FRANKY:=Franky} Franky
Special parameters, among others the positional parameters, may not be assigned this way, however. We will further discuss the use of the curly braces for treatment of variables in Chapter 10. More information can also be found in the Bash info pages.
When the oldstyle backquoted form of substitution is used, backslash retains its literal meaning except when followed by "$", "`", or "\". The first backticks not preceded by a backslash terminates the command substitution. When using the "$(COMMAND)" form, all characters between the parentheses make up the command; none are treated specially. Command substitutions may be nested. To nest when using the backquoted form, escape the inner backticks with backslashes. If the substitution appears within double quotes, word splitting and file name expansion are not performed on the results. Chapter 3. The Bash environment 46
Table 34. Arithmetic operators Operator VAR++ and VAR ++VAR and VAR and + ! and ~ ** *, / and % + and << and >> <=, >=, < and > == and !== & ^ | && || expr ? expr : expr =, *=, /=, %=, +=, =, <<=, >>=, &=, ^= and |= , Meaning variable postincrement and postdecrement variable preincrement and predecrement unary minus and plus logical and bitwise negation exponentiation multiplication, division, remainder addition, subtraction left and right bitwise shifts comparison operators equality and inequality bitwise AND bitwise exclusive OR bitwise OR logical AND logical OR conditional evaluation assignments separator between expressions
Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. The value of a variable is evaluated as an arithmetic expression when it is referenced. A shell variable need not have its integer attribute turned on to be used in an expression. Constants with a leading 0 (zero) are interpreted as octal numbers. A leading "0x" or "0X" denotes hexadecimal. Otherwise, numbers take the form "[BASE'#']N", where "BASE" is a decimal number between Chapter 3. The Bash environment 47
Bash Guide for Beginners 2 and 64 representing the arithmetic base, and N is a number in that base. If "BASE'#'" is omitted, then base 10 is used. The digits greater than 9 are represented by the lowercase letters, the uppercase letters, "@", and "_", in that order. If "BASE" is less than or equal to 36, lowercase and uppercase letters may be used interchangably to represent numbers between 10 and 35. Operators are evaluated in order of precedence. Subexpressions in parentheses are evaluated first and may override the precedence rules above. Wherever possible, Bash users should try to use the syntax with angular brackets: $[ EXPRESSION ] However, this will only calculate the result of EXPRESSION, and do no tests:
franky ~> echo $[365*24] 8760
Bash Guide for Beginners the whitespace character is in the value of IFS (an IFS whitespace character). Any character in IFS that is not IFS whitespace, along with any adjacent IF whitespace characters, delimits a field. A sequence of IFS whitespace characters is also treated as a delimiter. If the value of IFS is null, no word splitting occurs. Explicit null arguments ("""" or "''") are retained. Unquoted implicit null arguments, resulting from the expansion of parameters that have no values, are removed. If a parameter with no value is expanded within double quotes, a null argument results and is retained. Expansion and word splitting If no expansion occurs, no splitting is performed.
3.5. Aliases
3.5.1. What are aliases?
An alias allows a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias builtin commands. Issue the alias without options to display a list of aliases known to the current shell.
franky: ~> alias alias ..='cd ..' alias ...='cd ../..' alias ....='cd ../../..' alias PAGER='less r' alias Txterm='export TERM=xterm' alias XARGS='xargs r' alias cdrecord='cdrecord dev 0,0,0 speed=8' alias e='vi'
49
franky ~>
Aliases are useful for specifying the default version of a command that exists in several versions on your system, or to specify default options to a command. Another use for aliases is for correcting incorrect spelling. The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The alias name and the replacement text may contain any valid shell input, including shell metacharacters, with the exception that the alias name may not contain "=". The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias ls to ls F, for instance, and Bash will not try to recursively expand the replacement text. If the last character of the alias value is a space or tab character, then the next command word following the alias is also checked for alias expansion. Aliases are not expanded when the shell is not interactive, unless the expand_aliases option is set using the shopt shell builtin.
Used Avail Use% Mounted on 272M 1018M 22% / 9.4M 105M 9% /boot 8.7G 3.7G 70% /home 5.3G 7.1G 43% /opt
50
Bash always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. The commands following the alias definition on that line are not affected by the new alias. This behavior is also an issue when functions are executed. Aliases are expanded when a function definition is read, not when the function is executed, because a function definition is itself a compound command. As a consequence, aliases defined in a function are not available until after that function is executed. To be safe, always put alias definitions on a separate line, and do not use alias in compound commands. Aliases are not inherited by child processes. Bourne shell (sh) does not recognize aliases. More about functions is in Chapter 11. Functions are faster Aliases are looked up after functions and thus resolving is slower. While aliases are easier to understand, shell functions are preferred over aliases for almost every purpose.
51
See the Bash Info pages, section Shell Builtin Commands>The Set Builtin for a description of each option. A lot of options have onecharacter shorthands: the xtrace option, for instance, is equal to specifying set x.
For changing the current environment temporarily, or for use in a script, we would rather use set. Use (dash) for enabling an option, + for disabling:
willy:~/test> set o noclobber willy:~/test> touch test willy:~/test> date > test bash: test: cannot overwrite existing file willy:~/test> set +o noclobber willy:~/test> date > test
The above example demonstrates the noclobber option, which prevents existing files from being overwritten by redirection operations. The same goes for onecharacter options, for instance u, which will treat unset variables as an error when set, and exits a noninteractive shell upon encountering such errors:
willy:~> echo $VAR
This option is also useful for detecting incorrect content assignment to variables: the same error will also occur, for instance, when assigning a character string to a variable that was declared explicitly as one holding only integer values. One last example follows, demonstrating the noglob option, which prevents special characters from being expanded: Chapter 3. The Bash environment 52
0 Feb 27 13:37 *
3.7. Summary
The Bash environment can be configured globally and on a per user basis. Various configuration files are used to finetune the behavior of the shell. These files contain shell options, settings for variables, function definitions and various other building blocks for creating ourselves a cosy environment. Except for the reserved Bourne shell, Bash and special parameters, variable names can be chosen more or less freely. Because a lot of characters have double or even triple meanings, depending on the environment, Bash uses a system of quoting to take away special meaning from one or multiple characters when special treatment is not wanted. Bash uses various methods of expanding command line entries in order to determine which commands to execute.
3.8. Exercises
For this exercise, you will need to read the useradd man pages, because we are going to use the /etc/skel directory to hold default shell configuration files, which are copied to the home directory of each newly added user. First we will do some general exercises on setting and displaying variables. 1. Create 3 variables, VAR1, VAR2 and VAR3; initialize them to hold the values "thirteen", "13" and "Happy Birthday" respectively. 2. Display the values of all three variables. 3. Are these local or global variables? 4. Remove VAR3. 5. Can you see the two remaining variables in a new terminal window? 6. Edit /etc/profile so that all users are greeted upon login (test this). 7. For the root account, set the prompt to something like "Danger!! root is doing stuff in \w", preferably in a bright color such as red or pink or in reverse video mode. 8. Make sure that newly created users also get a nice personalized prompt which informs them on which system in which directory they are working. Test your changes by adding a new user and logging in as that user. 9. Write a script in which you assign two integer values to two variables. The script should calculate the surface of a rectangle which has these proportions. It should be aired with comments and generate elegant output. Don't forget to chmod your scripts! Chapter 3. The Bash environment 53
54
Table 41. Regular expression operators Operator . ? * + {N} {N,} {N,M} ^ $ \b \B \< \> Effect Matches any single character. The preceding item is optional and will be matched, at most, once. The preceding item will be matched zero or more times. The preceding item will be matched one or more times. The preceding item is matched exactly N times. The preceding item is matched N or more times. The preceding item is matched at least N times, but not more than M times. represents the range if it's not first or last in a list or the ending point of a range in a list. Matches the empty string at the beginning of a line; also represents the characters not in the range of a list. Matches the empty string at the end of a line. Matches the empty string at the edge of a word. Matches the empty string provided it's not at the edge of a word. Match the empty string at the beginning of word. Match the empty string at the end of word.
Two regular expressions may be concatenated; the resulting regular expression matches any string formed by Chapter 4. Regular expressions 55
Bash Guide for Beginners concatenating two substrings that respectively match the concatenated subexpressions. Two regular expressions may be joined by the infix operator "|"; the resulting regular expression matches any string matching either subexpression. Repetition takes precedence over concatenation, which in turn takes precedence over alternation. A whole subexpression may be enclosed in parentheses to override these precedence rules.
56
With the first command, user cathy displays the lines from /etc/passwd containing the string root. Then she displays the line numbers containing this search string. With the third command she checks which users are not using bash, but accounts with the nologin shell are not displayed. Then she counts the number of accounts that have /bin/false as the shell. The last command displays the lines from all the files in her home directory starting with ~/.bash, excluding matches containing history, so as to exclude matches from ~/.bash_history which might contain the same string, in upper or lower cases. Now let's see what else we can do with grep, using regular expressions.
If we want to see which accounts have no shell assigned whatsoever, we search for lines ending in ":":
cathy ~> grep :$ /etc/passwd news:x:9:13:news:/var/spool/news:
To check that PATH is exported in ~/.bashrc, first select "export" lines and then search for lines starting with the string "PATH", so as not to display MANPATH and other possible paths:
cathy ~> grep export ~/.bashrc | grep '\<PATH' export PATH="/bin:/usr/lib/mh:/lib:/usr/bin:/usr/local/bin:/usr/ucb:/usr/dbin:$PATH"
Similarly, \> matches the end of a word. If you want to find a string that is a separate word (enclosed by spaces), it is better use the w, as in this example where we are displaying information for the root partition: Chapter 4. Regular expressions 57
If this option is not used, all the lines from the file system table will be displayed. 4.2.2.2. Character classes A bracket expression is a list of characters enclosed by "[" and "]". It matches any single character in that list; if the first character of the list is the caret, "^", then it matches any character NOT in the list. For example, the regular expression "[0123456789]" matches any single digit. Within a bracket expression, a range expression consists of two characters separated by a hyphen. It matches any single character that sorts between the two characters, inclusive, using the locale's collating sequence and character set. For example, in the default C locale, "[ad]" is equivalent to "[abcd]". Many locales sort characters in dictionary order, and in these locales "[ad]" is typically not equivalent to "[abcd]"; it might be equivalent to "[aBbCcDd]", for example. To obtain the traditional interpretation of bracket expressions, you can use the C locale by setting the LC_ALL environment variable to the value "C". Finally, certain named classes of characters are predefined within bracket expressions. See the grep man or info pages for more information about these predefined expressions.
cathy ~> grep [yf] /etc/group sys:x:3:root,bin,adm tty:x:5: mail:x:12:mail,postfix ftp:x:50: nobody:x:99: floppy:x:19: xfs:x:43: nfsnobody:x:65534: postfix:x:89: cathy ~> ls *[19].xml app1.xml chap1.xml chap2.xml
chap3.xml
chap4.xml
In the example, all the lines containing either a "y" or "f" character are first displayed, followed by an example of using a range with the ls command. 4.2.2.3. Wildcards Use the "." for a single character match. If you want to get a list of all fivecharacter English dictionary words starting with "c" and ending in "h" (handy for solving crosswords):
cathy ~> grep '\<c...h\>' /usr/share/dict/words catch clash cloth coach couch cough crash crush
If you want to display lines containing the literal dot character, use the F to grep. Chapter 4. Regular expressions 58
Bash Guide for Beginners For matching multiple characters, use the asterisk. This example selects all words starting with "c" and ending in "h" from the system's dictionary:
cathy ~> grep '\<c.*h\>' /usr/share/dict/words caliph cash catch cheesecloth cheetah output omitted
If you want to find the literal asterisk character in a file or output, use grep F:
cathy ~> grep * /etc/profile cathy ~> grep F '*' /etc/profile for i in /etc/profile.d/*.sh ; do
But you can also use the square braces to match any enclosed character or range of characters, if pairs of characters are separated by a hyphen. An example:
cathy ~> ls ld [acxz]* drwxrxrx 2 cathy cathy drwxrwxrx 4 cathy cathy drwxrwxrx 2 cathy cathy drwxrxrx 7 cathy cathy drwxrwxrx 3 cathy cathy
Jul 20 2002 appdefaults/ May 25 2002 arabic/ Mar 4 18:30 bin/ Sep 2 2001 crossover/ Mar 22 2002 xml/
This lists all files in cathy's home directory, starting with "a", "b", "c", "x", "y" or "z". If the first character within the braces is "!" or "^", any character not enclosed will be matched. To match the dash (""), include it as the first or last character in the set. The sorting depends on the current locale and of the value of the LC_COLLATE variable, if it is set. Mind that other locales might interpret "[acxz]" as "[aBbCcXxYyZz]" if sorting is done in dictionary order. If you want to be sure to have the traditional interpretation of ranges, force this behavior by setting LC_COLLATE or LC_ALL to "C".
59
4096 Sep 30 2001 Nautilus/ 4096 Jul 11 2002 OpenOffice.org1.0/ 997376 Apr 18 15:39 Schedule.sdc
When the extglob shell option is enabled (using the shopt builtin), several extended pattern matching operators are recognized. Read more in the Bash info pages, section Basic shell features>Shell Expansions>Filename Expansion>Pattern Matching.
4.4. Summary
Regular expressions are powerful tools for selecting particular lines from files or output. In this chapter we got the feel of grep. The grep command can do much more; we only used it as an example for regular expressions. The GNU grep version comes with plenty of documentation, which you are strongly advised to read!
4.5. Exercises
These exercises will help you master regular expressions. 1. Display a list of all the users on your system who log in with the Bash shell as a default. 2. From the /etc/group directory, display all lines starting with the string "daemon". 3. Print all the lines from the same file that don't contain the string. 4. Display localhost information from the /etc/hosts file, display the line number(s) matching the search string and count the number of occurrences of the string. 5. Display a list of /usr/share/doc subdirectories containing information about shells. 6. How many README files do these subdirectories contain? Don't count anything in the form of "README.a_string". 7. Make a list of files in your home directory that were changed less that 10 hours ago, using grep, but leave out directories. 8. Put these commands in a shell script that will generate comprehensible output. 9. Can you find an alternative for wc l, using grep? 10. Using the file system table (/etc/fstab for instance), list local disk devices. 11. Make a script that checks whether a user exists in /etc/passwd. For now, you can specify the user name in the script, you don't have to work with arguments and conditionals at this stage. 12. Display configuration files in /etc that contain numbers in their names. Chapter 4. Regular expressions 60
61
5.1. Introduction
5.1.1. What is sed?
A Stream EDitor is used to perform basic transformations on text read from a file or a pipe. The result is sent to standard output. The sed syntax has no output file specification, but results can be saved to a file using output redirection. The editor does not modify the original input. What distinguishes sed from other editors, such as vi and ed, is its ability to filter text that it gets from a pipeline feed. You do not need to interact with the editor while it is running; that is why sed is sometimes called a batch editor. This feature allows use of editing commands in scripts, greatly easing repetitive editing tasks. When facing replacement of text in a large number of files, sed is a great help.
Table 51. Sed editing commands Command a\ c\ d i\ Result Append text below current line. Change text in the current line with new text. Delete text. Insert text above current line.
62
Bash Guide for Beginners p r s w Print text. Read a file. Search and replace text. Write to a file.
Apart from editing commands, you can give options to sed. An overview is in the table below:
Table 52. Sed options Option e SCRIPT f n V Effect Add the commands in SCRIPT to the set of commands to be run while processing the input. Add the commands contained in the file SCRIPTFILE to the set of commands to be run while processing the input. Silent mode. Print version information and exit.
The sed info pages contain more information; we only list the most frequently used commands and options here.
We want sed to find all the lines containing our search pattern, in this case "erors". We use the p to obtain the result:
sandy ~> sed '/erors/p' example This is the first line of an example text. It is a text with erors. It is a text with erors. Lots of erors. Lots of erors. So much erors, all these erors are making me sick. So much erors, all these erors are making me sick.
63
As you notice, sed prints the entire file, but the lines containing the search string are printed twice. This is not what we want. In order to only print those lines matching our pattern, use the n option:
sandy ~> sed n '/erors/p' example It is a text with erors. Lots of erors. So much erors, all these erors are making me sick. sandy ~>
The d command results in excluding lines from being displayed. Matching lines starting with a given pattern and ending in a second pattern are showed like this:
sandy ~> sed n '/^This.*errors.$/p' example This is a line not containing any errors. sandy ~>
To print the file starting from a certain line until the end of the file, use a command similar to this:
sandy ~> sed '3,$d' example This is the first line of an example text. It is a text with erors. sandy ~>
64
Bash Guide for Beginners This only prints the first two lines of the example file. The following command prints the first line containing the pattern "a text", up to and including the next line containing the pattern "a line":
sandy ~> sed n '/a text/,/This/p' example It is a text with erors. Lots of erors. So much erors, all these erors are making me sick. This is a line not containing any errors. sandy ~>
As you can see, this is not exactly the desired effect: in line 4, only the first occurrence of the search string has been replaced, and there is still an 'eror' left. Use the g command to indicate to sed that it should examine the entire line instead of stopping at the first occurrence of your string:
sandy ~> sed 's/erors/errors/g' example This is the first line of an example text. It is a text with errors. Lots of errors. So much errors, all these errors are making me sick. This is a line not containing any errors. This is the last line. sandy ~>
To insert a string at the beginning of each line of a file, for instance for quoting:
sandy ~> sed 's/^/> /' example > This is the first line of an example text. > It is a text with erors. > Lots of erors. > So much erors, all these erors are making me sick. > This is a line not containing any errors. > This is the last line. sandy ~>
65
Multiple find and replace commands are separated with individual e options:
sandy ~> sed e 's/erors/errors/g' e 's/last/final/g' example This is the first line of an example text. It is a text with errors. Lots of errors. So much errors, all these errors are making me sick. This is a line not containing any errors. This is the final line. sandy ~>
Keep in mind that by default sed prints its results to the standard output, most likely your terminal window. If you want to save the output to a file, redirect it: sed option 'some/expression' file_to_process > sed_output_in_a_file
66
$1 holds the first argument to a given command, in this case the name of the file to convert:
sandy ~> cat test line1 line2 line3
This is not really how it is done; this example just demonstrates sed capabilities. See Section 6.3 for a more decent solution to this problem, using awk BEGIN and END constructs. Easy sed Advanced editors, supporting syntax highlighting, can recognize sed syntax. This can be a great help if you tend to forget backslashes and such.
67
5.4. Summary
The sed stream editor is a powerful command line tool, which can handle streams. This makes it fit for noninteractive use. The sed editor uses vilike commands and accepts regular expressions.
5.5. Exercises
These exercises are meant to further demonstrate what sed can do. 1. Print a list of files in your scripts directory, ending in ".sh". Mind that you might have to unalias ls. Put the result in a temporary file. 2. Make a list of files in /usr/bin that have the letter "a" as the second character. Put the result in a temporary file. 3. Delete the first 3 lines of each temporary file. 4. Print to standard output only the lines containing the pattern "an". 5. Create a file holding sed commands to perform the previous two tasks. Add an extra command to this file that adds a string like "*** This might have something to do with man and man pages ***" in the line preceding every occurence of the string "man". Check the results. 6. A long listing of the root directory, /, is used for input. Create a file holding sed commands that check for symbolic links and plain files. If a file is a symbolic link, precede it with a line like "This is a symlink". If the file is a plain file, add a string on the same line, adding a comment like "< this is a plain file". 7. Create a script that shows lines containing trailing white spaces from a file. This script should use a sed script and show sensible information to the user.
68
In the output of ls l, there are 9 columns. The print statement uses these fields as follows:
kelly@octarine ~/test> ls l | awk '{ print $9 $5 }' 160orig 121script.sed 120temp_file 126test 120twolines 441txt2html.sh kelly@octarine ~/test>
70
Bash Guide for Beginners This command printed the fifth column of a long file listing, which contains the file size, and the last column, the name of the file. This output is not very readable unless you use the official way of referring to columns, which is to separate the ones that you want to print with a comma. In that case, the default output separater character, usually a space, will be put in between each output field.
Note the use of the backslash, which makes long input continue on the next line without the shell interpreting this as a separate command. While your command line input can be of virtually unlimited length, your monitor is not, and printed paper certainly isn't. Using the backslash also allows for copying and pasting of the above lines into a terminal window. The h option to ls is used for supplying humanly readable size formats for bigger files. The output of a long listing displaying the total amount of blocks in the directory is given when a directory is the argument. This line is useless to us, so we add an asterisk. We also add the d option for the same reason, in case asterisk expands to a directory. The backslash in this example marks the continuation of a line. See Section 3.3.2. You can take out any number of columns and even reverse the order. In the example below this is demonstrated for showing the most critical partitions:
kelly@octarine ~> df h | sort rnk 5 | head 3 | \ awk '{ print "Partition " $6 "\t: " $5 " full!" }' Partition /var : 86% full! Partition /usr : 85% full! Partition /home : 70% full! kelly@octarine ~>
Table 61. Formatting characters for gawk Sequence Meaning \a Bell character \n Newline character Chapter 6. The GNU awk programming language 71
Quotes, dollar signs and other metacharacters should be escaped with a backslash.
Slashes need to be escaped, because they have a special meaning to the awk program. Below another example where we search the /etc directory for files ending in ".conf" and starting with either "a" or "x":
kelly is in /etc> ls l | awk '/\<[a|x].*\.conf$/ { print $9 }' amd.conf antivir.conf xcdroast.conf xinetd.conf kelly is in /etc>
This example illustrates the special meaning of the dot in regular expressions: the first one indicates that we want to search for any character after the first search string, the second is escaped because it is part of a string to find (the end of the file name).
72
Bash Guide for Beginners The END statement can be added for inserting text after the entire input is processed:
kelly is in /etc> ls l | \ awk '/\<[a|x].*\.conf$/ { print $9 } END { print \ "Can I do anything else for you, mistress?" }' amd.conf antivir.conf xcdroast.conf xinetd.conf Can I do anything else for you, mistress? kelly is in /etc>
awk first prints a begin message, then formats all the lines that contain an eight or a nine at the beginning of a word, followed by one other number and a percentage sign. An end message is added. Syntax highlighting Awk is a programming language. Its syntax is recognized by most editors that can do syntax highlighting for other languages, such as C, Bash, HTML, etc.
73
Bash Guide for Beginners The value of the field separator variable can be changed in the awk program with the assignment operator =. Often the right time to do this is at the beginning of execution before any input has been processed, so that the very first record is read with the proper separator. To do this, use the special BEGIN pattern. In the example below, we build a command that displays all the users on your system with a description:
kelly is in ~> awk 'BEGIN { FS=":" } { print $1 "\t" $5 }' /etc/passwd output omitted kelly Kelly Smith franky Franky B. eddy Eddy White willy William Black cathy Catherine the Great sandy Sandy Li Wong kelly is in ~>
Choose input field separators carefully to prevent problems. An example to illustrate this: say you get input in the form of lines that look like this: "Sandy L. Wong, 64 Zoo St., Antwerp, 2000X" You write a command line or a script, which prints out the name of the person in that record: awk 'BEGIN { FS="," } { print $1, $2, $3 }' inputfile But a person might have a PhD, and it might be written like this: "Sandy L. Wong, PhD, 64 Zoo St., Antwerp, 2000X" Your awk will give the wrong output for this line. If needed, use an extra awk or sed to uniform data input formats. The default input field separator is one or more whitespaces or tabs.
74
kelly@octarine ~/test> awk '{ print $1 $2}' test record1data1 record2data2 kelly@octarine ~/test> awk '{ print $1, $2}' test record1 data1 record2 data2 kelly@octarine ~/test>
If you don't put in the commas, print will treat the items to output as one argument, thus omitting the use of the default output separator, OFS. Any character string may be used as the output field separator by setting this builtin variable. 6.3.2.2. The output record separator The output from an entire print statement is called an output record. Each print command results in one output record, and then outputs a string called the output record separator, ORS. The default value for this variable is "\n", a newline character. Thus, each print statement generates a separate line. To change the way output fields and records are separated, assign new values to OFS and ORS:
kelly@octarine ~/test> awk 'BEGIN { OFS=";" ; ORS="\n>\n" } \ { print $1,$2}' test record1;data1 > record2;data2 > kelly@octarine ~/test>
If the value of ORS does not contain a newline, the program's output is run together on a single line.
75
kelly@octarine ~> cat total.awk { total=total + $5 } { print "Send bill for " $5 " dollar to " $4 } END { print "\nTotal revenue: " total } kelly@octarine ~> awk f total.awk test Send bill for 2500 dollar to BigComp Send bill for 2000 dollar to EduComp Send bill for 10000 dollar to SmartComp Send bill for 5000 dollar to EduComp Total revenue: 19500 kelly@octarine ~>
kelly@octarine ~/html> cat makehtmlfromtext.awk BEGIN { print "<html>\n<head><title>Awkgenerated HTML</title></head>\n<body bgcolor=\"#ffffff\"> { print $0 } END { print "</pre>\n</body>\n</html<" }
And the command to execute is also much more straightforward when using awk instead of sed:
kelly@octarine ~/html> awk f makehtmlfromtext.awk testfile > file.html
Bash Guide for Beginners and where to print the other arguments. The syntax is the same as for the Clanguage printf statement; see your C introduction guide. The gawk info pages contain full explanations.
6.4. Summary
The gawk utility interprets a specialpurpose programming language, handling simple datareformatting jobs with just a few lines of code. It is the free version of the general UNIX awk command.
6.5. Exercises
These are some practical examples where awk can be useful. 1. For the first exercise, your input is lines in the following form:
Username:Firstname:Lastname:Telephone number
Make an awk script that will convert such a line to an LDAP record in this format:
dn: uid=Username, dc=example, dc=com cn: Firstname Lastname sn: Lastname telephoneNumber: Telephone number
Create a file containing a couple of test records and check. 2. Create a Bash script using awk and standard UNIX commands that will show the top three users of disk space in the /home file system (if you don't have the directory holding the homes on a separate partition, make the script for the / partition; this is present on every UNIX system). First, execute the commands from the command line. Then put them in a script. The script should create sensible output (sensible as in readable by the boss). If everything proves to work, have the script email its results to you (use for instance mail s Disk space usage <you@your_comp> < result). If the quota daemon is running, use that information; if not, use find. 3. Create XMLstyle output from a Tabseparated list in the following form:
Meaning very long line with a lot of description meaning another long line othermeaning testmeaning more longline
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong lin
77
</entry> </row> <row> <entryothermeaning</entry> <entry> more longline </entry> </row> <row> <entrytestmeaning</entry> <entry> looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line, but i mean re </entry> </row>
Additionally, if you know anything about XML, write a BEGIN and END script to complete the table. Or do it in HTML.
78
7.1. Introduction to if
7.1.1. General
At times you need to specify different courses of action to be taken in a shell script, depending on the success or failure of a command. The if construction allows you to specify such conditions. The most compact syntax of the if command is: if TESTCOMMANDS; then CONSEQUENTCOMMANDS; fi The TESTCOMMAND list is executed, and if its return status is zero, the CONSEQUENTCOMMANDS list is executed. The return status is the exit status of the last command executed, or zero if no condition tested true. The TESTCOMMAND often involves numerical or string comparison tests, but it can also be any command that returns a status of zero when it succeeds and some other status when it fails. Unary expressions are often used to examine the status of a file. If the FILE argument to one of the primaries is of the form /dev/fd/N, then file descriptor "N" is checked. stdin, stdout and stderr and their respective file descriptors may also be used for tests. 7.1.1.1. Expressions used with if The table below contains an overview of the socalled "primaries" that make up the TESTCOMMAND command or list of commands. These primaries are put between square brackets to indicate the test of a conditional expression.
Table 71. Primary expressions Primary [ a FILE ] Meaning True if FILE exists. 79
Bash Guide for Beginners [ b FILE ] [ c FILE ] [ d FILE ] [ e FILE ] [ f FILE ] [ g FILE ] [ h FILE ] [ k FILE ] [ p FILE ] [ r FILE ] [ s FILE ] [ t FD ] [ u FILE ] [ w FILE ] [ x FILE ] [ O FILE ] [ G FILE ] [ L FILE ] [ N FILE ] [ S FILE ] [ FILE1 nt FILE2 ] [ FILE1 ot FILE2 ] [ FILE1 ef FILE2 ] [ o OPTIONNAME ] [ z STRING ] [ n STRING ] or [ STRING ] [ STRING1 == STRING2 ] [ STRING1 != STRING2 ] [ STRING1 < STRING2 ] [ STRING1 > STRING2 ] True if FILE exists and is a blockspecial file. True if FILE exists and is a characterspecial file. True if FILE exists and is a directory. True if FILE exists. True if FILE exists and is a regular file. True if FILE exists and its SGID bit is set. True if FILE exists and is a symbolic link. True if FILE exists and its sticky bit is set. True if FILE exists and is a named pipe (FIFO). True if FILE exists and is readable. True if FILE exists and has a size greater than zero. True if file descriptor FD is open and refers to a terminal. True if FILE exists and its SUID (set user ID) bit is set. True if FILE True if FILE exists and is writable. True if FILE exists and is executable. True if FILE exists and is owned by the effective user ID. True if FILE exists and is owned by the effective group ID. True if FILE exists and is a symbolic link. True if FILE exists and has been modified since it was last read. True if FILE exists and is a socket. True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not. True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not. True if FILE1 and FILE2 refer to the same device and inode numbers. True if shell option "OPTIONNAME" is enabled. True of the length of "STRING" is zero. True of the length of "STRING" is nonzero. True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance. True if the strings are not equal. True if "STRING1" sorts before "STRING2" lexicographically in the current locale. True if "STRING1" sorts after "STRING2" lexicographically in the current locale.
"OP" is one of eq, ne, lt, le, gt or ge. These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater [ ARG1 OP ARG2 ] than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers.
80
Bash Guide for Beginners Expressions may be combined using the following operators, listed in decreasing order of precedence:
Table 72. Combining expressions Operation [ ! EXPR ] [ ( EXPR ) ] Effect True if EXPR is false. Returns the value of EXPR. This may be used to override the normal precedence of operators.
[ EXPR1 a EXPR2 True if both EXPR1 and EXPR2 are true. ] [ EXPR1 o EXPR2 True if either EXPR1 or EXPR2 is true. ] The [ (or test) builtin evaluates conditional expressions using a set of rules based on the number of arguments. More information about this subject can be found in the Bash documentation. Just like the if is closed with fi, the opening angular bracket should be closed after the conditions have been listed. 7.1.1.2. Commands following the then statement The CONSEQUENTCOMMANDS list that follows the then statement can be any valid UNIX command, any executable program, any executable shell script or any shell statement, with the exception of the closing fi. It is important to remember that the then and fi are considered to be separated statements in the shell. Therefore, when issued on the command line, they are separated by a semicolon. In a script, the different parts of the if statement are usually wellseparated. Below, a couple of simple examples. 7.1.1.3. Checking files The first example checks for the existence of a file:
anny ~> cat msgcheck.sh #!/bin/bash echo "This scripts checks the existence of the messages file." echo "Checking..." if [ f /var/log/messages ] then echo "/var/log/messages exists." fi echo echo "...done." anny ~> ./msgcheck.sh This scripts checks the existence of the messages file. Checking... /var/log/messages exists. ...done.
81
Bash Guide for Beginners 7.1.1.4. Checking shell options To add in your Bash configuration files:
# These lines will print a message if the noclobber option is set: if [ o noclobber ] then echo "Your files are protected against accidental overwriting using redirection." fi
The environment The above example will work when entered on the command line:
anny ~> if [ o noclobber ] ; then echo ; echo "your files are protected against overwriting." ; echo ; fi your files are protected against overwriting. anny ~>
However, if you use testing of conditions that depend on the environment, you might get different results when you enter the same command in a script, because the script will open a new shell, in which expected variables and options might not be set automatically.
anny ~>
The following example demonstrates that TESTCOMMANDS might be any UNIX command that returns an exit status, and that if again returns an exit status of zero:
anny ~> if ! grep $USER /etc/passwd More input> then echo "your user account is not managed locally"; fi your user account is not managed locally anny > echo $? 0 anny >
82
Bash Guide for Beginners The same result can be obtained as follows:
anny > grep $USER /etc/passwd anny > if [ $? ne 0 ] ; then echo "not a local account" ; fi not a local account anny >
anny >
This script is executed by cron every Sunday. If the week number is even, it reminds you to put out the garbage cans:
#!/bin/bash # Calculate the week number using the date command: WEEKOFFSET=$[ $(date +"%V") % 2 ] # Test if we have a remainder. # Else, do nothing. If not, this is an even week so send a message.
if [ $WEEKOFFSET eq "0" ]; then echo "Sunday evening, put out the garbage cans." | mail s "Garbage cans out" your@your_domain.
7.1.2.3. String comparisons An example of comparing strings for testing the user ID:
if [ "$(whoami)" != 'root' ]; then echo "You have no permission to run $0 as nonroot user." exit 1; fi
83
See the info pages for Bash for more information on pattern matching with the "(( EXPRESSION ))" and "[[ EXPRESSION ]]" constructs.
Like the CONSEQUENTCOMMANDS list following the then statement, the ALTERNATECONSEQUENTCOMMANDS list following the else statement can hold any UNIXstyle command that returns an exit status. Another example, extending the one from Section 7.1.2.1:
anny ~> su Password: [root@elegance root]# if ! grep ^$USER /etc/passwd 1>/dev/null > then echo "your user account is not managed locally" > else echo "your account is managed from the local /etc/passwd file" > fi your account is managed from the local /etc/passwd file [root@elegance root]#
We switch to the root account to demonstrate the effect of the else statement your root is usually a local account while your own user account might be managed by a central system, such as an LDAP server. 7.2.1.2. Checking command line arguments Instead of setting a variable and then executing a script, it is frequently more elegant to put the values for the variables on the command line. We use the positional parameters $1, $2, ..., $N for this purpose. $# refers to the number of command line arguments. $0 refers to the name of the script. Chapter 7. Conditional statements 84
85
Bash Guide for Beginners 7.2.1.3. Testing the number of arguments The following example shows how to change the previous script so that it prints a message if more or less than 2 arguments are given:
anny ~> cat weight.sh #!/bin/bash # This script prints a message about your weight if you give it your # weight in kilos and hight in centimeters. if [ ! $# == 2 ]; then echo "Usage: $0 weight_in_kilos length_in_centimeters" exit fi weight="$1" height="$2" idealweight=$[$height 110] if [ $weight le $idealweight ] ; then echo "You should eat a bit more fat." else echo "You should eat a bit more fruit." fi anny ~> weight.sh 70 150 You should eat a bit more fruit. anny ~> weight.sh 70 150 33 Usage: ./weight.sh weight_in_kilos length_in_centimeters
The first argument is referred to as $1, the second as $2 and so on. The total number of arguments is stored in $#. Check out Section 7.2.5 for a more elegant way to print usage messages. 7.2.1.4. Testing that a file exists This test is done in a lot of scripts, because there's no use in starting a lot of programs if you know they're not going to work:
#!/bin/bash # This script gives information about a file. FILENAME="$1" echo "Properties for $FILENAME:" if [ f $FILENAME ]; then echo "Size is $(ls lh $FILENAME | awk '{ print $5 }')" echo "Type is $(file $FILENAME | cut d":" f2 )" echo "Inode number is $(ls i $FILENAME | cut d" " f1 )" echo "$(df h $FILENAME | grep v Mounted | awk '{ print "On",$1", \ which is mounted as the",$6,"partition."}')" else echo "File does not exist."
86
Note that the file is referred to using a variable; in this case it is the first argument to the script. Alternatively, when no arguments are given, file locations are usually stored in variables at the beginning of a script, and their content is referred to using these variables. Thus, when you want to change a file name in a script, you only need to do it once.
87
88
Bash Guide for Beginners We use the double brackets for testing an arithmetic expression, see Section 3.4.6. This is equivalent to the let statement. You will get stuck using angular brackets here, if you try something like $[$year % 400], because here, the angular brackets don't represent an actual command by themselves. Among other editors, gvim is one of those supporting colour schemes according to the file format; such editors are useful for detecting errors in your code.
case $space in [16]*) Message="All is quiet." ;; [78]*) Message="Start thinking about cleaning out some stuff. There's a partition that is $space % fu ;; 9[18]) Message="Better hurry with that new disk... One partition is $space % full." ;; 99) Message="I'm drowning here! There's a partition at $space %!" ;; *) Message="I seem to be running with an nonexitent amount of disk space..." ;; esac echo $Message | mail s "disk report `date`" anny anny ~/testdir> You have new mail. anny ~/testdir> tail 16 /var/spool/mail/anny From anny@octarine Tue Jan 14 22:10:47 2003 ReturnPath: <anny@octarine>
89
Of course you could have opened your mail program to check the results; this is just to demonstrate that the script sends a decent mail with "To:", "Subject:" and "From:" header lines. Many more examples using case statements can be found in your system's init script directory. The startup scripts use start and stop cases to run or stop system processes. A theoretical example can be found in the next section.
90
This script is called upon in the next one, which therefore exports its variables menu and animal:
anny ~/testdir> cat feed.sh #!/bin/bash # This script acts upon the exit status given by penguin.sh export menu="$1" export animal="$2" feed="/nethome/anny/testdir/penguin.sh" $feed $menu $animal case $? in 1) echo "Guard: You'd better give'm a fish, less they get violent..." ;; 2) echo "Guard: It's because of people like you that they are leaving earth all the time..." ;; 3) echo "Guard: Buy the food that the Zoo provides for the animals, you ***, how do you think we survive?" ;; *) echo "Guard: Don't forget the guide!" ;; esac anny ~/testdir> ./feed.sh apple penguin Tux don't like that. Tux wants fish! Guard: You'd better give'm a fish, less they get violent...
As you can see, exit status codes can be chosen freely. Existing commands usually have a series of defined codes; see the programmer's manual for each command for more information.
7.3. Summary
In this chapter we learned how to build conditions into our scripts so that different actions can be undertaken upon success or failure of a command. The actions can be determined using the if statement. This allows you to perform arithmetic and string comparisons, and testing of exit code, input and files needed by the script. Difficult definitions of conditions can best be put in a case statement.
91
7.4. Exercises
Here are some ideas to get you started using if in scripts: 1. Use an if/then/elif/else construct that prints information about the current month. The script should print the number of days in this month, and give information about leap years if the current month is February. 2. Do the same, using a case statement and an alternative use of the date command. 3. Modify /etc/profile so that you get a special greeting message when you connect to your system as root. 4. Edit the leaptest.sh script from Section 7.2.4 so that it requires one argument, the year. Test that exactly one argument is supplied. 5. Write a script called whichdaemon.sh that checks if the httpd and init daemons are running on your system. If an httpd is running, the script should print a message like, "This machine is running a web server." Use ps to check on processes. 6. Write a script that makes a backup of your home directory on a remote machine using scp. The script should report in a log file, for instance ~/log/homebackup.log. If you don't have a second machine to copy the backup to, use scp to test copying it to the localhost. This requires SSH keys between the two hosts, or else you have to supply a password. The creation of SSH keys is explained in man sshkeygen. The script should use tar cf for the creation of the backup and gzip or bzip2 for compressing the .tar file. Put all filenames in variables. Put the name of the remote server and the remote directory in a variable. This will make it easier to reuse the script or to make changes to it in the future. The script should check for the existence of a compressed archive. If this exists, remove it first in order to prevent output generation. The script should also check for available diskspace. Keep in mind that at any given moment you could have the data in your home directory, the data in the .tar file and the data in the compressed archive all together on your disk. If there is not enough diskspace, exit with an error message in the log file. The script should clean up the compressed archive before it exits.
92
e "Guard: \"Buy the food that the Zoo provides at the entry, you ***\"\n" e "Guard: \"You want to poison them, do you?\"\n"
94
More about escape characters can be found in Section 3.3.2. The following table gives an overview of sequences recognized by the echo command:
Table 81. Escape sequences used by the echo command Sequence \a \b \c \e \f \n \r \t \v \\ \ONNN \NNN \xHH Meaning Alert (bell). Backspace. Suppress trailing newline. Escape. Form feed. Newline. Carriage return. Horizontal tab. Vertical tab. Backslash. The eightbit character whose value is the octal value NNN (zero to three octal digits). The eightbit character whose value is the octal value NNN (one to three octal digits). The eightbit character whose value is the hexadecimal value (one or two hexadecimal digits).
For more information about the printf command and the way it allows you to format output, see the Bash info pages.
95
Table 82. Options to the read builtin Option a ANAME d DELIM e n NCHARS p PROMPT r s t TIMEOUT u FD Meaning The words are assigned to sequential indexes of the array variable ANAME, starting at 0. All elements are removed from ANAME before the assignment. Other NAME arguments are ignored. The first character of DELIM is used to terminate the input line, rather than newline. readline is used to obtain the line. read returns after reading NCHARS characters rather than waiting for a complete line of input. Display PROMPT, without a trailing newline, before attempting to read any input. The prompt is displayed only if input is coming from a terminal. If this option is given, backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslashnewline pair may not be used as a line continuation. Silent mode. If input is coming from a terminal, characters are not echoed. Cause read to time out and return failure if a complete line of input is not read within TIMEOUT seconds. This option has no effect if read is not reading input from the terminal or from a pipe. Read input from file descriptor FD.
96
Bash Guide for Beginners This is a straightforward example, improving on the leaptest.sh script from the previous chapter:
michel ~/test> cat leaptest.sh #!/bin/bash # This script will test if you have given a leap year or not. echo "Type the year that you want to check (4 digits), followed by [ENTER]:" read year if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 != "0") )); then echo "$year is a leap year." else echo "This is not a leap year." fi michel ~/test> leaptest.sh Type the year that you want to check (4 digits), followed by [ENTER]: 2000 2000 is a leap year.
n "Enter your name and press [ENTER]: " name n "Enter your gender and press [ENTER]: " n 1 gender
grep i "$name" "$friends" if [ $? == 0 ]; then echo "You are already registered, quitting." exit 1 elif [ "$gender" == "m" ]; then echo "You are added to Michel's friends list." exit 1 else echo n "How old are you? " read age if [ $age lt 25 ]; then echo n "Which colour of hair do you have? " read colour echo "$name $age $colour" >> "$friends" echo "You are added to Michel's friends list. Thank you so much!" else echo "You are added to Michel's friends list." exit 1
97
Note that no output is omitted here. The script only stores information about the people Michel is interested in, but it will always say you are added to the list, unless you are already in it. Other people can now start executing the script:
[anny@octarine tmp]$ friends.sh Hello, anny. This script will register you in Michel's friends database. Enter your name and press [ENTER]: anny Enter your gender and press [ENTER] :f How old are you? 22 Which colour of hair do you have? black You are added to Michel's friends list.
Of course, this situation is not ideal, since everybody can edit (but not delete) Michel's files. You can solve this problem using special access modes on the script file, see SUID and SGID in the Introduction to Linux guide.
Bash Guide for Beginners take TCP or UDP ports on networked hosts as file descriptors as well. The output below shows how the reserved file descriptors point to actual devices:
michel ~> ls l /dev/std* lrwxrwxrwx 1 root root lrwxrwxrwx 1 root root lrwxrwxrwx 1 root root
2 07:46 /dev/stderr > ../proc/self/fd/2 2 07:46 /dev/stdin > ../proc/self/fd/0 2 07:46 /dev/stdout > ../proc/self/fd/1
michel ~> ls l /proc/self/fd/[02] lrwx 1 michel michel 64 Jan 23 12:11 /proc/self/fd/0 > /dev/pts/6 lrwx 1 michel michel 64 Jan 23 12:11 /proc/self/fd/1 > /dev/pts/6 lrwx 1 michel michel 64 Jan 23 12:11 /proc/self/fd/2 > /dev/pts/6
You might want to check info MAKEDEV and info proc for more information about /proc subdirectories and the way your system handles standard file descriptors for each running process. When you run a script from the command line, nothing much changes because the child shell process will use the same file descriptors as the parent. When no such parent is available, for instance when you run a script using the cron facility, the standard file descriptors are pipes or other (temporary) files, unless some form of redirection is used. This is demonstrated in the example below, which shows output from a simple at script:
michel ~> date Fri Jan 24 11:05:50 CET 2003 michel ~> at 1107 warning: commands will be executed using (in order) a) $SHELL b) login shell c)/bin/sh at> ls l /proc/self/fd/ > /var/tmp/fdtest.at at> <EOT> job 10 at 20030124 11:07 michel ~> cat total 0 lrx lwx lwx lrx /var/tmp/fdtest.at 1 1 1 1 michel michel michel michel michel michel michel michel 64 64 64 64 Jan Jan Jan Jan 24 24 24 24 11:07 11:07 11:07 11:07 0 1 2 3 > > > > /var/spool/at/!0000c010959eb (deleted) /var/tmp/fdtest.at /var/spool/at/spool/a0000c010959eb /proc/21949/fd
99
Bash Guide for Beginners 8.2.3.2. Redirection of errors From the previous examples, it is clear that you can provide input and output files for a script (see Section 8.2.4 for more), but some tend to forget about redirecting errorsoutput which might be depended upon later on. Also, if you are lucky, errors will be mailed to you and eventual causes of failure might get revealed. If you are not as lucky, errors will cause your script to fail and won't be caught or sent anywhere, so that you can't start to do any worthwhile debugging. When redirecting errors, note that the order of precedence is significant. For example, this command, issued in /var/spool
ls l * 2 > /var/tmp/unaccessibleinspool
will redirect output of the ls command to the file unaccessibleinspool in /var/tmp. The command
ls l * > /var/tmp/spoollist 2>&1
will direct both standard input and standard error to the file spoollist. The command
ls l * 2>&1 > /var/tmp/spoollist
directs only the standard output to the destination file, because the standard error is copied to standard output before the standard output is redirected. For convenience, errors are often redirected to /dev/null, if it is sure they will not be needed. Hundreds of examples can be found in the startup scripts for your system. Bash allows for both standard output and standard error to be redirected to the file whose name is the result of the expansion of FILE with this construct: &> FILE This is the equivalent of > FILE 2>&1, the construct used in the previous set of examples. It is also often combined with redirection to /dev/null, for instance when you just want a command to execute, no matter what output or errors it gives.
The cat command first reads the file header.txt, next its standard input which is the output of the filter command, and last the footer.txt file. The special meaning of the hyphen as a commandline argument to refer to the standard input or standard output is a misconception that has crept into many programs. There might also be problems when specifying hyphen as the first argument, since it might be interpreted as an option to the preceding command. Using /dev/fd allows for uniformity and prevents confusion:
michel ~> filter body.txt | cat header.txt /dev/fd/0 footer.txt | lp
In this clean example, all output is additionally piped through lp to send it to the default printer. 8.2.4.2. Read and exec
8.2.4.2.1. Assigning file descriptors to files
Another way of looking at file descriptors is thinking of them as a way to assign a numeric value to a file. Instead of using the file name, you can use the file descriptor number. The exec builtin command is used to assign a file descriptor to a file. Use exec fdN> file for assigning file descriptor N to file for output, and exec fdN< file for assigning file descriptor N to file for input. After a file descriptor has been assigned to a file, it can be used with the shell redirection operators, as is demonstrated in the following example:
michel ~> exec 4>result.txt michel ~> filter body.txt | cat header.txt /dev/fd/0 footer.txt >&4 michel ~> cat result.txt This text is printed at the beginning of each print job and thanks the sysadmin for setting us up such a great printing infrastructure. Text to be filtered. This text is printed at the end of each print job.
File descriptor 5 Using this file descriptor might cause problems, see the Advanced BashScripting Guide, chapter 16. You are strongly advised not to use it.
101
The following is an example that shows how you can alternate between file input and command line input:
michel ~/testdir> cat sysnotes.sh #!/bin/bash # This script makes an index of important config files, puts them together in # a backup file and allows for adding comment for each file. CONFIG=/var/tmp/sysconfig.out rm "$CONFIG" 2>/dev/null echo "Output will be saved in $CONFIG." exec 7<&0 exec < /etc/passwd # Read the first line of /etc/passwd read rootpasswd echo "Saving root account info..." echo "Your root account info:" >> "$CONFIG" echo $rootpasswd >> "$CONFIG" exec 0<&7 7<& echo n "Enter comment or [ENTER] for no comment: " read comment; echo $comment >> "$CONFIG" echo "Saving hosts information..." # first prepare a hosts file not containing any comments TEMP="/var/tmp/hosts.tmp" cat /etc/hosts | grep v "^#" > "$TEMP" exec 7<&0 exec < "$TEMP" read ip1 name1 alias1 read ip2 name2 alias2 echo "Your local host configuration:" >> "$CONFIG" echo "$ip1 $name1 $alias1" >> "$CONFIG" echo "$ip2 $name2 $alias2" >> "$CONFIG" exec 0<&7 7<& echo n "Enter comment or [ENTER] for no comment: " read comment; echo $comment >> "$CONFIG" rm "$TEMP" michel ~/testdir> sysnotes.sh Output will be saved in /var/tmp/sysconfig.out. Saving root account info... Enter comment or [ENTER] for no comment: hint for password: blue lagoon Saving hosts information... Enter comment or [ENTER] for no comment: in central DNS
102
8.2.4.3. Closing file descriptors Since child processes inherit open file descriptors, it is good practice to close a file descriptor when it is no longer needed. This is done using the exec fd<& syntax. In the above example, file descriptor 7, which has been assigned to standard input, is closed each time the user needs to have access to the actual standard input device, usually the keyboard. The following is a simple example redirecting only standard error to a pipe:
michel ~> cat listdirs.sh #!/bin/bash # This script prints standard output unchanged, while standard error is # redirected for processing by awk. INPUTDIR="$1" exec 6>&1 ls "$INPUTDIR"/* 2>&1 >&6 6>& \ # Closes fd 6 for awk, but not for ls. | awk 'BEGIN { FS=":" } { print "YOU HAVE NO ACCESS TO" $2 }' 6>& exec 6>&
8.2.4.4. Here documents Frequently, your script might call on another program or script that requires input. The here document provides a way of instructing the shell to read input from the current source until a line containing only the search string is found (no trailing blanks). All of the lines read up to that point are then used as the standard input for a command. The result is that you don't need to call on separate files; you can use shellspecial characters, and it looks nicer than a bunch of echo's:
michel ~> cat startsurf.sh #!/bin/bash # This script provides an easy way for users to choose between browsers. echo "These are the web browsers on this system:" # Start here document
103
Although we talk about a here document, it is supposed to be a construct within the same script.
8.3. Summary
In this chapter, we learned how to provide user comments and how to prompt for user input. This is usually done using the echo/read combination. We also discussed how files can be used as input and output using file descriptors and redirection, and how this can be combined with getting input from the user.
8.4. Exercises
These exercises are practical applications of the constructs discussed in this chapter. When writing the scripts, you may test by using a test directory that does not contain too much data. Write each step, then test that portion of code, rather than writing everything at once. 1. Write a script that asks for the user's age. If it is equal to or higher than 16, print a message saying that this user is allowed to drink alcohol. If the user's age is below 16, print a message telling the user how many years he or she has to wait before legally being allowed to drink. As an extra, calculate how much beer an 18+ user has drunk statistically (100 liters/year) and print this information for the user. 2. Write a script that takes one file as an argument. Use a here document that presents the user with a couple of choices for compressing the file. Possible choices could be gzip, bzip2, compress and zip. 3. Write a script called homebackup that automates tar so the person executing the script always uses the desired options (cvp) and backup destination directory (/var/backups) to make a backup of his or her home directory. Implement the following features:
104
Bash Guide for Beginners Test for the number of arguments. The script should run without arguments. If any arguments are present, exit after printing a usage message. Determine whether the backups directory has enough free space to hold the backup. Ask the user whether a full or an incremental backup is wanted. If the user does not have a full backup file yet, print a message that a full backup will be taken. In case of an incremental backup, only do this if the full backup is not older than a week. Compress the backup using any compression tool. Inform the user that the script is doing this, because it might take some time, during which the user might start worrying if no output appears on the screen. Print a message informing the user about the size of the compressed backup. See info tar or Introduction to Linux, chapter 9: "Preparing your data" for background information. 4. Write a script called simpleuseradd.sh that adds a local user to the system. This script should: Take only one argument, or else exit after printing a usage message. Check /etc/passwd and decide on the first free user ID. Print a message containing this ID. Create a private group for this user, checking the /etc/group file. Print a message containing the group ID. Gather information from the operator user: a comment describing this user, choice from a list of shells (test for acceptability, else exit printing a message), expiration date for this account, extra groups of which the new user should be a member. With the obtained information, add a line to /etc/passwd, /etc/group and /etc/shadow; create the user's home directory (with correct permissions!); add the user to the desired secondary groups. Set the password for this user to a default known string. 5. Rewrite the script from Section 7.2.1.4 so that it reads input from the user instead of taking it from the first argument.
105
9.1.2. Examples
9.1.2.1. Using command substitution for specifying LIST items The first is a command line example, demonstrating the use of a for loop that makes a backup copy of each .xml file. After issuing the command, it is safe to start working on your sources:
[carol@octarine ~/articles] ls *.xml file1.xml file2.xml file3.xml [carol@octarine ~/articles] ls *.xml > list [carol@octarine ~/articles] for i in `cat list`; do cp "$i" "$i".bak ; done [carol@octarine ~/articles] ls *.xml* file1.xml file1.xml.bak file2.xml file2.xml.bak
file3.xml
file3.xml.bak
106
Bash Guide for Beginners This one lists the files in /sbin that are just plain text files, and possibly scripts:
for i in `ls /sbin`; do file /sbin/$i | grep ASCII; done
9.1.2.2. Using the content of a variable to specify LIST items The following is a specific application script for converting HTML files, compliant with a certain scheme, to PHP files. The conversion is done by taking out the first 25 and the last 21 lines, replacing these with two PHP tags that provide header and footer lines:
[carol@octarine ~/html] cat html2php.sh #!/bin/bash # specific conversion script for my html files to php LIST="$(ls *.html)" for i in "$LIST"; do NEWNAME=$(ls "$i" | sed e 's/html/php/') cat beginfile > "$NEWNAME" cat "$i" | sed e '1,25d' | tac | sed e '1,21d'| tac >> "$NEWNAME" cat endfile >> "$NEWNAME" done
Since we don't do a line count here, there is no way of knowing the line number from which to start deleting lines until reaching the end. The problem is solved using tac, which reverses the lines in a file.
9.2.2. Examples
9.2.2.1. Simple example using while Here is an example for the impatient:
#!/bin/bash # This script opens 4 terminal windows.
107
9.2.2.2. Nested while loops The example below was written to copy pictures that are made with a webcam to a web directory. Every five minutes a picture is taken. Every hour, a new directory is created, holding the images for that hour. Every day, a new directory is created containing 24 subdirectories. The script runs in the background.
Note the use of the true statement. This means: continue execution until we are forcibly interrupted (with kill or Ctrl+C). This small script can be used for simulation testing; it generates files:
108
Note the use of the date command to generate all kinds of file and directory names. 9.2.2.3. Using keyboard input to control the while loop This script can be interrupted by the user when a Ctrl+C sequence is entered:
#!/bin/bash # This script provides wisdom FORTUNE=/usr/games/fortune while true; do echo "On which topic do you want advice?" cat << topics politics startrek kernelnewbies sports bofhexcuses magic love literature drugs education topics echo echo n "Make your choice: " read topic echo echo "Free advice on the topic of $topic: " echo $FORTUNE $topic echo done
A here document is used to present the user with possible choices. And again, the true test repeats the commands from the CONSEQUENTCOMMANDS list over and over again. 9.2.2.4. Calculating an average This script calculates the average of user input, which is tested before it is processed: if input is not within range, a message is printed. If q is pressed, the loop exits:
#!/bin/bash # Calculate the average of a series of numbers.
109
Note how the variables in the last lines are left unquoted in order to do arithmetic.
9.3.2. Example
An improved picturesort.sh script (see Section 9.2.2.2), which tests for available disk space. If not enough disk space is available, remove pictures from the previous months:
110
Note the initialization of the HOUR and DISKFULL variables and the use of options with ls and date in order to obtain a correct listing for TOREMOVE.
Bash Guide for Beginners complies with the form command < file This kind of redirection also works with other kinds of loops.
ARCHIVENR=`date +%Y%m%d` DESTDIR="$PWD/archive$ARCHIVENR" mkdir $DESTDIR find $PWD type f a mtime +5 | while read file do gzip "$file"; mv "$file".gz "$DESTDIR" echo "$file archived" done
Files are compressed before they are moved into the archive directory.
112
echo n "Enter your choice, or 0 for exit: " read choice echo case $choice in 1) $FORTUNE politics ;; 2) $FORTUNE startrek ;; 3) $FORTUNE kernelnewbies ;; 4) echo "Sports are a waste of time, energy and money." echo "Go back to your keyboard." echo e "\t\t\t\t \"Unhealthy is my middle name\" Soggie." ;; 5) $FORTUNE bofhexcuses ;; 6) $FORTUNE magic ;; 7) $FORTUNE love ;; 8) $FORTUNE literature ;; 9) $FORTUNE drugs ;; 10) $FORTUNE education ;; 0) echo "OK, see you!" break ;; *) echo "That is not a valid choice, try a number from 0 to 10." ;; esac done
Mind that break exits the loop, not the script. This can be demonstrated by adding an echo command at the end of the script. This echo will also be executed upon input that causes break to be executed (when the user types "0").
113
Bash Guide for Beginners In nested loops, break allows for specification of which loop to exit. See the Bash info pages for more.
9.5.3. Examples
In the following example, file names are converted to lower case. If no conversion needs to be done, a continue statement restarts execution of the loop. These commands don't eat much system resources, and most likely, similar problems can be solved using sed and awk. However, it is useful to know about this kind of construction when executing heavy jobs, that might not even be necessary when tests are inserted at the correct locations in a script, sparing system resources.
[carol@octarine ~/test] cat tolower.sh #!/bin/bash
# This script converts all file names containing upper case characters into file# names containin LIST="$(ls)" for name in "$LIST"; do if [[ "$name" != *[[:upper:]]* ]]; then continue fi ORIG="$name" NEW=`echo $name | tr 'AZ' 'az'` mv "$ORIG" "$NEW" echo "new name for $ORIG is $NEW" done
This script has at least one disadvantage: it overwrites existing files. The noclobber option to Bash is only useful when redirection occurs. The b option to the mv command provides more security, but is only safe in case of one accidental overwrite, as is demonstrated in this test:
[carol@octarine ~/test] rm * [carol@octarine ~/test] touch test Test TEST [carol@octarine ~/test] bash x tolower.sh ++ ls + LIST=test Test TEST + [[ test != *[[:upper:]]* ]] + continue + [[ Test != *[[:upper:]]* ]] + ORIG=Test ++ echo Test
114
The tr is part of the textutils package; it can perform all kinds of character transformations.
115
Setting the PS3 prompt and adding a possibility to quit makes it better:
#!/bin/bash echo "This script can make any of the files in this directory private." echo "Enter the number of the file you want to protect:" PS3="Your choice: " QUIT="QUIT THIS PROGRAM I feel safe now." touch "$QUIT" select FILENAME in *; do case $FILENAME in "$QUIT") echo "Exiting." break ;; *) echo "You picked $FILENAME ($REPLY)" chmod gorwx "$FILENAME" ;; esac done rm "$QUIT"
9.6.2. Submenus
Any statement within a select construct can be another select loop, enabling (a) submenu(s) within a menu. By default, the PS3 variable is not changed when entering a nested select loop. If you want a different prompt in the submenu, be sure to set it at the appropriate time(s).
116
9.7.2. Examples
A shift statement is typically used when the number of arguments to a command is not known in advance, for instance when users can give as many arguments as they like. In such cases, the arguments are usually processed in a while loop with a test condition of (( $# )). This condition is true as long as the number of arguments is greater than zero. The $1 variable and the shift statement process each argument. The number of arguments is reduced each time shift is executed and eventually becomes zero, upon which the while loop exits. The example below, cleanup.sh, uses shift statements to process each file in the list generated by find:
#!/bin/bash # This script can clean up files that were last accessed over 365 days ago. USAGE="Usage: $0 dir1 dir2 dir3 ... dirN" if [ "$#" == "0" ]; then echo "$USAGE" exit 1 fi while (( "$#" )); do if [[ "$(ls $1)" == "" ]]; then echo "Empty directory, nothing to be done." shift else find $1 type f a atime +365 exec rm i {} \; fi shift done
9.8. Summary
In this chapter, we discussed how repetitive commands can be incorporated in loop constructs. Most common loops are built using the for, while or until statements, or a combination of these commands. Loops can be interrupted or reiterated using the break and continue statements. Chapter 9. Repetitive tasks 117
Bash Guide for Beginners The select is used for printing menus in interactive scripts. Looping through the command line arguments to a script can be done using the shift statement.
9.9. Exercises
Remember: when building scripts, work in steps and test each step before incorporating it in your script. 1. Create a script that will take a (recursive) copy of files in /etc so that a beginning system administrator can edit files without fear. 2. Write a script that takes exactly one argument, a directory name. If the number of arguments is more or less than one, print a usage message. If the argument is not a directory, print another message. For the given directory, print the five biggest files and the five files that were most recently modified. 3. Can you explain why it is so important to put the variables in between double quotes in the example from Section 9.4.2? 4. Write a script similar to the one in Section 9.5.1, but think of a way of quitting after the user has executed 3 loops. 5. Think of a better solution than move b for the script from Section 9.5.3 to prevent overwriting of existing files. For instance, test whether or not a file exists. Don't do unnecessary work! 6. Rewrite the whichdaemon.sh script from Section 7.2.4, so that it: Prints a list of servers to check, such as Apache, the SSH server, the NTP daemon, a name daemon, a power management daemon, and so on. For each choice the user can make, print some sensible information, like the name of the web server, NTP trace information, and so on. Optionally, build in a possibility for users to check other servers than the ones listed. For such cases, check that at least the given process is running. Review the script from Section 9.2.2.4. Note how character input other than q is processed. Rebuild this script so that it prints a message if characters are given as input.
118
There are cases when you want to avoid this kind of behavior, for instance when handling telephone and other numbers. Apart from integers and variables, you may also want to specify a variable that is a constant. This is often done at the beginning of a script, when the value of the constant is declared. After that, there are only references to the constant variable name, so that when the constant needs to be changed, it only has to be done once. A variable may also be a series of variables of any type, a socalled array of variables (VAR0VAR1, VAR2, ... VARN).
Bash Guide for Beginners a f i p r t x Variable is an array. Use function names only. The variable is to be treated as an integer; arithmetic evaluation is performed when the variable is assigned a value (see Section 3.4.6). Display the attributes and values of each variable. When p is used, additional options are ignored. Make variables readonly. These variables cannot then be assigned values by subsequent assignment statements, nor can they be unset. Give each variable the trace attribute. Mark each variable for export to subsequent commands via the environment.
Using + instead of turns off the attribute instead. When used in a function, declare creates local variables. The following example shows how assignment of a type to a variable influences the value.
[bob in ~] declare i VARIABLE=12 [bob in ~] VARIABLE=string [bob in ~] echo $VARIABLE 0 [bob in ~] declare p VARIABLE declare i VARIABLE="0"
Note that Bash has an option to declare a numeric value, but none for declaring string values. This is because, by default, if no specifications are given, a variable can hold any type of data:
[bob in ~] OTHERVAR=blah [bob in ~] declare p OTHERVAR declare OTHERVAR="blah"
As soon as you restrict assignment of values to a variable, it can only hold that type of data. Possible restrictions are either integer, constant or array. See the Bash info pages for information on return status.
10.1.3. Constants
In Bash, constants are created by making a variable readonly. The readonly builtin marks each specified variable as unchangeable. The syntax is: readonly OPTION VARIABLE(s) The values of these variables can then no longer be changed by subsequent assignment. If the f option is given, each variable refers to a shell function; see Chapter 11. If a is specified, each variable refers to an array of variables. If no arguments are given, or if p is supplied, a list of all readonly variables is displayed. Using the p option, the output can be reused as input.
120
Bash Guide for Beginners The return status is zero, unless an invalid option was specified, one of the variables or functions does not exist, or f was supplied for a variable name instead of for a function name.
[bob in ~] readonly TUX=penguinpower [bob in ~] TUX=Mickeysoft bash: TUX: readonly variable
121
Referring to the content of a member variable of an array without providing an index number is the same as referring to the content of the first element, the one referenced with index number zero.
Bash Guide for Beginners After long days of searching, I finally found this example operating at an Internet provider. It distributes Apache web server configuration files onto hosts in a web farm:
#!/bin/bash # $Id: chap10.xml,v 1.1 2004/04/26 13:32:31 tille Exp $ # $Log: chap10.xml,v $ # Revision 1.1 2004/04/26 13:32:31 tille # initial submission, reviewed by Tabatha. Ready for publish. # # Revision 1.2 2004/04/26 13:24:41 tille # updates by tabatha # # Revision 1.1.1.1 2004/02/11 16:59:50 tille # initiele bash import # # Revision 1.3 2003/02/05 09:52:53 mbounine # httpd restarting added. # # Revision 1.2 2003/02/05 08:11:32 mbounine # Bug fixes. # # Revision 1.1 2003/02/04 15:41:35 mbounine # Script for syncing httpd config between web farm hosts. # Initial release. # if [ $(whoami) != 'root' ]; then echo "Must be root to run $0" exit 1; fi if [ z $1 ]; then echo "Usage: $0 </path/to/httpd.conf>" exit 1 fi httpd_conf_new=$1 httpd_conf_path="/usr/local/apache/conf" login=htuser farm_hosts=(web03 web04 web05 web06 web07) for i in ${farm_hosts[@]}; do su $login c "scp $httpd_conf_new ${i}:${httpd_conf_path}" su $login c "ssh $i sudo /usr/local/apache/bin/apachectl graceful" done exit 0
First two tests are performed to check whether the correct user is running the script with the correct arguments. The names of the hosts that need to be configured are listed in the array farm_hosts. Then all these hosts are provided with the Apache configuration file, after which the daemon is restarted. Note the use of commands from the Secure Shell suite, encrypting the connections to remote hosts. Thanks, Eugene and colleague, for this contribution. Dan Richter contributed the following example. This is the problem he was confronted with: "...In my company, we have demos on our web site, and every week someone has to test all of them. So I have Chapter 10. More on variables 123
Bash Guide for Beginners a cron job that fills an array with the possible candidates, uses date +%W to find the week of the year, and does a modulo operation to find the correct index. The lucky person gets notified by email." And this was his way of solving it:
#!/bin/bash # This is gettesteraddress.sh # # First, we test whether bash supports arrays. # (Support for arrays was only added recently.) # whotest[0]='test' || (echo 'Failure: arrays not supported in this version of bash.' && exit 2) # # Our list of candidates. (Feel free to add or # remove candidates.) # wholist=( 'Bob Smith <[email protected]>' 'Jane L. Williams <[email protected]>' 'Eric S. Raymond <[email protected]>' 'Larry Wall <[email protected]>' 'Linus Torvalds <[email protected]>' ) # # Count the number of possible testers. # (Loop until we find an empty string.) # count=0 while [ "x${wholist[count]}" != "x" ] do count=$(( $count + 1 )) done # # Now we calculate whose turn it is. # week=`date '+%W'` # The week of the year (0..53). week=${week#0} # Remove possible leading zero. let "index = $week % $count" email=${wholist[index]} echo $email # week modulo count = the lucky person # Get the lucky person's email address. # Output the person's email address.
This script is then used in other scripts, such as this one, which uses a here document:
email=`gettesteraddress.sh` hostname=`hostname` # Find who to email. # This machine's name.
# # Send email to the right person. # mail $email s '[Demo Testing]' <<EOF The lucky tester this week is: $email Reminder: the list of demos is here: http://web.example.com:8080/DemoSites
124
[bob in ~] export TEST=a_string [bob in ~] echo ${TEST:test} a_string [bob in ~] echo ${TEST2:$TEST} a_string
If the hyphen () is replaced with the equal sign (=), the value is assigned to the parameter if it does not exist:
[bob in ~] echo $TEST2
125
The following syntax tests the existence of a variable. If it is not set, the expansion of WORD is printed to standard out and noninteractive shells quit. A demonstration:
[bob in ~] cat vartest.sh #!/bin/bash # This script tests whether a variable is set. # it exits printing a message. If not,
echo ${TESTVAR:?"There's so much I still wanted to do..."} echo "TESTVAR is set, we can proceed." [bob in testdir] ./vartest.sh ./vartest.sh: line 6: TESTVAR: There's so much I still wanted to do... [bob in testdir] export TESTVAR=present [bob in testdir] ./vartest.sh present TESTVAR is set, we can proceed.
Using "+" instead of the exclamation mark sets the variable to the expansion of WORD; if it does not exist, nothing happens. 10.3.3.2. Removing substrings To strip a number of characters, equal to OFFSET, from a variable, use this syntax: ${VAR:OFFSET:LENGTH} The LENGTH parameter defines how many characters to keep, starting from the first character after the offset point. If LENGTH is omitted, the remainder of the variable content is taken:
[bob in ~] export STRING="thisisaverylongname" [bob in ~] echo ${STRING:4} isaverylongname [bob in ~] echo ${STRING:6:5} avery
${VAR#WORD} and ${VAR##WORD} These constructs are used for deleting the pattern matching the expansion of WORD in VAR. WORD is expanded Chapter 10. More on variables 126
Bash Guide for Beginners to produce a pattern just as in file name expansion. If the pattern matches the beginning of the expanded value of VAR, then the result of the expansion is the expanded value of VAR with the shortest matching pattern ("#") or the longest matching pattern (indicated with "##"). If VAR is * or @, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If VAR is an array variable subscribed with "*" or "*", the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list. This is shown in the examples below:
[bob in ~] echo ${ARRAY[*]} one two one three one four [bob in ~] echo ${ARRAY[*]#one} two three four [bob in ~] echo ${ARRAY[*]#t} one wo one hree one four [bob in ~] echo ${ARRAY[*]#t*} one wo one hree one four [bob in ~] echo ${ARRAY[*]##t*} one one one four
The opposite effect is obtained using "%" and "%%", as in this example below. WORD should match a trailing portion of string:
[bob in ~] echo $STRING thisisaverylongname [bob in ~] echo ${STRING%name} thisisaverylong
10.3.3.3. Replacing parts of variable names This is done using the ${VAR/PATTERN/STRING} or ${VAR//PATTERN/STRING} syntax. The first form replaces only the first match, the second replaces all matches of PATTERN with STRING:
[bob in ~] echo ${STRING/name/string} thisisaverylongstring
127
10.4. Summary
Normally, a variable can hold any type of data, unless variables are declared explicitly. Constant variables are set using the readonly builtin command. An array holds a set of variables. If a type of data is declared, then all elements in the array will be set to hold only this type of data. Bash features allow for substitution and transformation of variables "on the fly".
10.5. Exercises
Here are some brain crackers: 1. Write a script that does the following: Display the name of the script being executed. Display the first, third and tenth argument given to the script. Display the total number of arguments passed to the script. If there were more than three positional parameters, use shift to move all the values 3 places to the left. Print all the values of the remaining arguments. Print the number of arguments. Test with zero, one, three and over ten arguments. 2. Write a script that implements a simple web browser (in text mode), using wget and links dump to display HTML pages to the user. The user has 3 choices: enter a URL, enter b for back and q to quit. The last 10 URLs entered by the user are stored in an array, from which the user can restore the URL by using the back functionality.
128
11.1. Introduction
11.1.1. What are functions?
Shell functions are a way to group commands for later execution, using a single name for this group, or routine. The name of the routine must be unique within the shell or script. All the commands that make up a function are executed like regular commands. When calling on a function as a simple command name, the list of commands associated with that function name is executed. A function is executed within the shell in which it has been declared: no new process is created to interpret the commands. Special builtin commands are found before shell functions during command lookup. The special builtins are: break, :, ., continue, eval, exec, exit, export, readonly, return, set, shift, trap and unset.
129
This is the sort of function that is typically configured in the user's shell resource configuration files. Functions are more flexible than aliases and provide a simple and easy way of adapting the user environment. Here's one for DOS users:
dir () { ls F color=auto lF color=always "$@" | less r }
This function is reused in the same script in other functions, which are reused in other scripts. The daemon function, for instance, is used in the majority of the startup scripts for starting a server process (on machines that use this system). 11.1.5.2. Setting the path This section might be found in your /etc/profile file. The function pathmunge is defined and then used to set the path for the root and other users:
pathmunge () { if ! echo $PATH | /bin/egrep q "(^|:)$1($|:)" ; then if [ "$2" = "after" ] ; then PATH=$PATH:$1 else PATH=$1:$PATH fi fi } # Path manipulation if [ `id u` = 0 ]; then pathmunge /sbin pathmunge /usr/sbin pathmunge /usr/local/sbin fi pathmunge /usr/X11R6/bin after unset pathmunge
The function takes its first argument to be a path name. If this path name is not yet in the current path, it is added. The second argument to the function defines if the path will be added in front or after the current PATH definition. Normal users only get /usr/X11R6/bin added to their paths, while root gets a couple of extra directories containing system commands. After being used, the function is unset so that it is not retained. 11.1.5.3. Remote backups The following example uses SSH keys for enabling the remote connection. It uses two functions, buplinux and bupbash, that each make a .tar file, which is then compressed and sent to a remote server. After that, the local copy is cleaned up. On Sunday, only bupbash is executed.
131
cd "$DIR" tar cf "$TAR" src/*.xml src/images/*.png src/images/*.eps echo "Compressing $TAR..." >> "$LOGFILE" bzip2 "$TAR" echo "...done." >> "$LOGFILE" echo "Copying to $SERVER..." >> "$LOGFILE" scp "$BZIP" "$SERVER:$RDIR" > /dev/null 2>&1 echo "...done." >> "$LOGFILE" echo e "Done backing up Linux course:\nSource files, PNG and EPS images.\nRubbish removed." >> " rm "$BZIP" } bupbash() { DIR="/nethome/lydia/xml/db/" TAR="Bash.tar" BZIP="$TAR.bz2" FILES="bashprogramming/" SERVER="rincewind" RDIR="/var/www/intra/lydia/html/training/" cd "$DIR" tar cf "$TAR" "$FILES" echo "Compressing $TAR..." >> "$LOGFILE" bzip2 "$TAR" echo "...done." >> "$LOGFILE" echo "Copying to $SERVER..." >> "$LOGFILE" scp "$BZIP" "$SERVER:$RDIR" > /dev/null 2>&1 echo "...done." >> "$LOGFILE" echo e "Done backing up Bash course:\n$FILES\nRubbish removed." >> "$LOGFILE" rm "$BZIP" } DAY=`date +%w` if [ "$DAY" lt "2" ]; then echo "It is `date +%A`, only backing up Bash course." >> "$LOGFILE" bupbash else buplinux bupbash fi
This script runs from cron, meaning without user interaction, so we redirect standard error from the scp Chapter 11. Functions 132
Bash Guide for Beginners command to /dev/null. It might be argued that all the separate steps can be combined in a command such as tar c dir_to_backup/ | bzip2 | ssh server "cat > backup.tar.bz2" However, if you are interested in intermediate results, which might be recovered upon failure of the script, this is not what you want. The expression command &> file is equivalent to command > file 2>&1
11.2. Summary
Functions provide an easy way of grouping commands that you need to execute repetitively. When a function is running, the positional parameters are changed to those of the function. When it stops, they are reset to those of the calling program. While this was a short chapter, it contains important knowledge needed for achieving the ultimate state of laziness that is the typical goal of any system administrator.
11.3. Exercises
Here are some useful things you can do using functions: 1. Add a function to your ~/.bashrc config file that automates the printing of man pages. The result should be that you type something like printman <command>, upon which the first appropriate man page rolls out of your printer. Check using a pseudo printer device for testing purposes. As an extra, build in a possibility for the user to supply the section number of the man page he or she wants to print. 2. Create a subdirectory in your home directory in which you can store function definitions. Put a couple of functions in that directory. Useful functions might be, amongs others, that you have the same commands as on DOS or a commercial UNIX when working with Linux, or vice versa. These functions should then be imported in your shell environment when ~/.bashrc is read.
133
12.1. Signals
12.1.1. Introduction
12.1.1.1. Finding the signal man page Your system contains a man page listing all the available signals, but depending on your operating system, it might be opened in a different way. On most Linux systems, this will be man 7 signal. When in doubt, locate the exact man page and section using commands like man k signal | grep list or apropos signal | grep list Signal names can be found using kill l. 12.1.1.2. Signals to your Bash shell In the absence of any traps, an interactive Bash shell ignores SIGTERM and SIGQUIT. SIGINT is caught and handled, and if job control is active, SIGTTIN, SIGTTOU and SIGTSTP are also ignored. Commands that are run as the result of a command substitution also ignore these signals, when keyboard generated. SIGHUP by default exits a shell. An interactive shell will send a SIGHUP to all jobs, running or stopped; see the documentation on the disown builtin if you want to disable this default behavior for a particular process. Use the huponexit option for killing all jobs upon receiving a SIGHUP signal, using the shopt builtin. 12.1.1.3. Sending signals using the shell The following signals can be sent using the Bash shell:
Table 121. Control signals in Bash Standard key combination Ctrl+C Meaning The interrupt signal. 134
Bash Guide for Beginners Ctrl+S Ctrl+Q Ctrl+Y Ctrl+Z Suspend output (XOFF) Resume output (XON) The delayed suspend character. Causes a running process to be stopped when it attempts to read input from the terminal. Control is returned to the shell, the user can foreground, background or kill the process. The suspend character. Stops a running program and returns control to the shell.
Terminal settings Check your stty settings. Suspend and resume of output is usually disabled if you are using "modern" terminal emulations. The standard xterm supports Ctrl+S and Ctrl+Q by default.
Table 122. Common kill signals Signal name SIGHUP SIGINT SIGKILL SIGTERM SIGSTOP Signal value 1 2 9 15 17,19,23 Effect Hangup Interrupt from keyboard Kill signal Termination signal Stop the process
SIGKILL and SIGSTOP SIGKILL and SIGSTOP can not be caught, blocked or ignored. When killing a process or series of processes, it is common sense to start trying with the least dangerous signal, SIGTERM. If that does not work, use the INT orKILL signals. For instance, when a process does not die using Ctrl+C, it is best to use the kill 9 on that process ID:
maud: ~> ps ef | grep stuck_process maud 5607 2214 0 20:05 pts/5 maud: ~> kill 9 5607 maud: ~> ps ef | grep stuck_process maud 5614 2214 0 20:15 pts/5
00:00:02 stuck_process
135
When a process starts up several instances, killall might be easier. It takes the same option as the kill command, but applies on all instances of a given process. Test this command before using it in a production environment, since it might not work as expected on some of the commercial Unices.
12.2. Traps
12.2.1. General
There might be situations when you don't want users of your scripts to exit untimely using keyboard abort sequences, for example because input has to be provided or cleanup has to be done. The trap statement catches these sequences and can be programmed to execute a list of commands upon catching those signals. The syntax for the trap statement is straightforward: trap [COMMANDS] [SIGNALS] This instructs the trap command to catch the listed SIGNALS, which may be signal names with or without the SIG prefix, or signal numbers. If a signal is 0 or EXIT, the COMMANDS are executed when the shell exits. If one of the signals is DEBUG, the list of COMMANDS is executed after every simple command. A signal may also be specified as ERR; in that case COMMANDS are executed each time a simple command exits with a nonzero status. Note that these commands will not be executed when the nonzero exit status comes from part of an if statement, or from a while or until loop. Neither will they be executed if a logical AND (&&) or OR (||) result in a nonzero exit code, or when a command's return status is inverted using the ! operator. The return status of the trap command itself is zero unless an invalid signal specification is encountered. The trap command takes a couple of options, which are documented in the Bash info pages. Here is a very simple example, catching Ctrl+C from the user, upon which a message is printed. When you try to kill this program without specifying the KILL signal, nothing will happen:
#!/bin/bash # traptest.sh trap "echo Booh!" SIGINT SIGTERM echo "pid is $$" while : do sleep 60 done # This is the same as "while true". # This script is not really doing anything.
136
12.2.3.2. Removing rubbish upon exit The whatis command relies on a database which is regularly built using the makewhatis.cron script with cron:
#!/bin/bash LOCKFILE=/var/lock/makewhatis.lock # Previous makewhatis should execute successfully: [ f $LOCKFILE ] && exit 0 # Upon exit, remove lockfile. trap "{ rm f $LOCKFILE ; exit 255; }" EXIT touch $LOCKFILE makewhatis u w exit 0
12.3. Summary
Signals can be sent to your programs using the kill command or keyboard shortcuts. These signals can be caught, upon which action can be performed, using the trap statement.
12.4. Exercises
A couple of practical examples: 1. Create a script that writes a boot image to a diskette using the dd utility. If the user tries to interrupt the script using Ctrl+C, display a message that this action will make the diskette unusable. 2. Write a script that automates the installation of a thirdparty package of your choice. The package must be downloaded from the Internet. It must be decompressed, unarchived and compiled if these actions are appropriate. Only the actual installation of the package should be uninterruptable.
137
Table A1. Common Shell Features Command > >> < << | & ; * ? [] () `` "" '' \ $var $$ $0 $n $* # bg break cd continue echo eval exec Meaning Redirect output Append to file Redirect input "Here" document (redirect input) Pipe output Run process in background. Separate commands on same line Match any character(s) in filename Match single character in filename Match any characters enclosed Execute in subshell Substitute output of enclosed command Partial quote (allows variable and command expansion) Full quote (no expansion) Quote following character Use value for variable Process id Command name nth argument (n from 0 to 9) All arguments as a simple word Begin comment Background execution Break from loop statements Change directories Resume a program loop Display output Evaluate arguments Execute a new shell 138
Bash Guide for Beginners fg jobs kill newgrp shift stop suspend time umask unset wait Foreground execution Show active jobs Terminate running jobs Change to a new group Shift positional parameters Suspend a background job Suspend a foreground job Time a command Set or list file permissions Erase variable or function definitions Wait for a background job to finish
Table A2. Differing Shell Features sh $ bash $ >| > file 2>&1 &> file or > file 2>&1 {} `command` `command` or $(command) $HOME $HOME ~ ~+, ~, dirs ksh Meaning/Action Default user $ % prompt >| >! Force redirection Redirect stdout > file >& file and stderr to 2>&1 file Expand elements {} in list Substitute output $(command) `command` of enclosed command $HOME $home Home directory Home directory ~ ~ symbol Access directory ~+, ~ =, =N stack 139 csh
Bash Guide for Beginners var=value VAR=value export var export VAR=value ${nnnn} "$@" $# "$@" $# var=value export var=val ${nn} "$@" $# set var=value setenv var val Variable assignment Set environment variable More than 9 arguments can be referenced All arguments as separate words Number of $#argv arguments Exit status of the most recently $status executed command PID of most recently backgrounded process Current options source Read commands file in file Name x stands alias x y for command y switch or Choose case alternatives End a loop end statement End case or endsw switch exit Exit with a (expr) status Loop through foreach variables Ignore substitution noglob characters for filename generation Display hashed hashstat commands (tracked aliases) Remember rehash command locations Forget command unhash locations 140
$?
$?
$?
$! $ . file
hash
hash
Bash Guide for Beginners history ArrowUp+Enter or !! !str history r r str history List previous commands Redo previous !! command Redo last !str command that starts with "str" Replace "x" with "y" in most recent command !cmd:s/x/y/ starting with "cmd", then execute. Sample if ($i==5) condition test endif End if statement Set resource limit limits Print working dirs directory Read from $< terminal onintr Ignore interrupts unalias Remove aliases Begin until loop Begin while while loop
!cmd:s/x/y/
r x=y cmd
if [ $i eq if [ $i eq 5 ] 5] fi fi ulimit pwd read trap 2 until while/do ulimit pwd read trap 2 unalias until while/do
The Bourne Again SHell has many more features not listed here. This table is just to give you an idea of how this shell incorporates all useful ideas from other shells: there are no blanks in the column for bash. More information on features found only in Bash can be retrieved from the Bash info pages, in the "Bash Features" section. More information: You should at least read one manual, being the manual of your shell. The preferred choice would be info bash, bash being the GNU shell and easiest for beginners. Print it out and take it home, study it whenever you have 5 minutes.
141
Copyright (C) 2000 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 021111307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
B.1. Preamble
The purpose of this License is to make a manual, textbook, or other written document "free" in the sense of freedom: to assure everyone the effective freedom to copy and redistribute it, with or without modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not being considered responsible for modifications made by others. This License is a kind of "copyleft", which means that derivative works of the document must themselves be free in the same sense. It complements the GNU General Public License, which is a copyleft license designed for free software. We have designed this License in order to use it for manuals for free software, because free software needs free documentation: a free program should come with manuals providing the same freedoms that the software does. But this License is not limited to software manuals; it can be used for any textual work, regardless of subject matter or whether it is published as a printed book. We recommend this License principally for works whose purpose is instruction or reference.
142
Bash Guide for Beginners A "Transparent" copy of the Document means a machinereadable copy, represented in a format whose specification is available to the general public, whose contents can be viewed and edited directly and straightforwardly with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some widely available drawing editor, and that is suitable for input to text formatters or for automatic translation to a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent file format whose markup has been designed to thwart or discourage subsequent modification by readers is not Transparent. A copy that is not "Transparent" is called "Opaque". Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly available DTD, and standardconforming simple HTML designed for human modification. Opaque formats include PostScript, PDF, proprietary formats that can be read and edited only by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally available, and the machinegenerated HTML produced by some word processors for output purposes only. The "Title Page" means, for a printed book, the title page itself, plus such following pages as are needed to hold, legibly, the material this License requires to appear in the title page. For works in formats which do not have any title page as such, "Title Page" means the text near the most prominent appearance of the work's title, preceding the beginning of the body of the text.
Bash Guide for Beginners take reasonably prudent steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated location until at least one year after the last time you distribute an Opaque copy (directly or through your agents or retailers) of that edition to the public. It is requested, but not required, that you contact the authors of the Document well before redistributing any large number of copies, to give them a chance to provide you with an updated version of the Document.
B.5. Modifications
You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3 above, provided that you release the Modified Version under precisely this License, with the Modified Version filling the role of the Document, thus licensing distribution and modification of the Modified Version to whoever possesses a copy of it. In addition, you must do these things in the Modified Version: A. Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from those of previous versions (which should, if there were any, be listed in the History section of the Document). You may use the same title as a previous version if the original publisher of that version gives permission. B. List on the Title Page, as authors, one or more persons or entities responsible for authorship of the modifications in the Modified Version, together with at least five of the principal authors of the Document (all of its principal authors, if it has less than five). C. State on the Title page the name of the publisher of the Modified Version, as the publisher. D. Preserve all the copyright notices of the Document. E. Add an appropriate copyright notice for your modifications adjacent to the other copyright notices. F. Include, immediately after the copyright notices, a license notice giving the public permission to use the Modified Version under the terms of this License, in the form shown in the Addendum below. G. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Document's license notice. H. Include an unaltered copy of this License. I. Preserve the section entitled "History", and its title, and add to it an item stating at least the title, year, new authors, and publisher of the Modified Version as given on the Title Page. If there is no section entitled "History" in the Document, create one stating the title, year, authors, and publisher of the Document as given on its Title Page, then add an item describing the Modified Version as stated in the previous sentence. J. Preserve the network location, if any, given in the Document for public access to a Transparent copy of the Document, and likewise the network locations given in the Document for previous versions it was based on. These may be placed in the "History" section. You may omit a network location for a work that was published at least four years before the Document itself, or if the original publisher of the version it refers to gives permission. K. In any section entitled "Acknowledgements" or "Dedications", preserve the section's title, and preserve in the section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein. L. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section numbers or the equivalent are not considered part of the section titles. M. Delete any section entitled "Endorsements". Such a section may not be included in the Modified Version. N. Do not retitle any existing section as "Endorsements" or to conflict in title with any Invariant Section. If the Modified Version includes new frontmatter sections or appendices that qualify as Secondary Sections and contain no material copied from the Document, you may at your option designate some or all of these Appendix B. GNU Free Documentation License 144
Bash Guide for Beginners sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version's license notice. These titles must be distinct from any other section titles. You may add a section entitled "Endorsements", provided it contains nothing but endorsements of your Modified Version by various partiesfor example, statements of peer review or that the text has been approved by an organization as the authoritative definition of a standard. You may add a passage of up to five words as a FrontCover Text, and a passage of up to 25 words as a BackCover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of FrontCover Text and one of BackCover Text may be added by (or through arrangements made by) any one entity. If the Document already includes a cover text for the same cover, previously added by you or by arrangement made by the same entity you are acting on behalf of, you may not add another; but you may replace the old one, on explicit permission from the previous publisher that added the old one. The author(s) and publisher(s) of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any Modified Version.
145
B.9. Translation
Translation is considered a kind of modification, so you may distribute translations of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special permission from their copyright holders, but you may include translations of some or all Invariant Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License provided that you also include the original English version of this License. In case of a disagreement between the translation and the original English version of this License, the original English version will prevail.
B.10. Termination
You may not copy, modify, sublicense, or distribute the Document except as expressly provided for under this License. Any other attempt to copy, modify, sublicense or distribute the Document is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance.
146
Copyright (c) YEAR YOUR NAME. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being LIST THEIR TITLES, with the FrontCover Texts being LIST, and with the BackCover Texts being LIST. A copy of the license is included in the section entitled "GNU Free Documentation License". If you have no Invariant Sections, write "with no Invariant Sections" instead of saying which ones are invariant. If you have no FrontCover Texts, write "no FrontCover Texts" instead of "FrontCover Texts being LIST"; likewise for BackCover Texts. If your document contains nontrivial examples of program code, we recommend releasing these examples in parallel under your choice of free software license, such as the GNU General Public License, to permit their use in free software.
147
Glossary
This section contains an alphabetical overview of common UNIX commands. More information about the usage can be found in the man or info pages.
A
a2ps Format files for printing on a PostScript printer. acroread PDF viewer. adduser Create a new user or update default new user information. alias Create a shell alias for a command. anacron Execute commands periodically, does not assume continuously running machine. apropos Search the whatis database for strings. aptget APT package handling utility. aspell Spell checker. at, atq, atrm Queue, examine or delete jobs for later execution. aumix Adjust audio mixer. (g)awk Pattern scanning and processing language.
B
bash Bourne Again SHell. batch Queue, examine or delete jobs for later execution. bg Run a job in the background. bitmap Bitmap editor and converter utilities for the X window System. bzip2 A blocksorting file compressor.
C
cat Concatenate files and print to standard output. Glossary 148
Bash Guide for Beginners cd Change directory. cdp/cdplay An interactive textmode program for controlling and playing audio CD Roms under Linux. cdparanoia An audio CD reading utility which includes extra data verification features. cdrecord Record a CDR. chattr Change file attributes. chgrp Change group ownership. chkconfig Update or query run level information for system services. chmod Change file access permissions. chown Change file owner and group. compress Compress files. cp Copy files and directories. crontab Maintain crontab files. csh Open a C shell. cut Remove sections from each line of file(s).
D
date Print or set system date and time. dd Convert and copy a file (disk dump). df Report file system disk usage. dhcpcd DHCP client daemon. diff Find differences between two files. dig Send domain name query packets to name servers. dmesg Print or control the kernel ring buffer. du Estimate file space usage.
Glossary
149
E
echo Display a line of text. ediff Diff to English translator. egrep Extended grep. eject Unmount and eject removable media. emacs Start the Emacs editor. exec Invoke subprocess(es). exit Exit current shell. export Add function(s) to the shell environment.
F
fax2ps Convert a TIFF facsimile to PostScript. fdformat Format floppy disk. fdisk Partition table manipulator for Linux. fetchmail Fetch mail from a POP, IMAP, ETRN or ODMRcapable server. fg Bring a job in the foreground. file Determine file type. find Find files. formail Mail (re)formatter. fortune Print a random, hopefully interesting adage. ftp Transfer files (unsafe unless anonymous account is used!)services.
G
galeon Graphical web browser. gdm Gnome Display Manager. (min/a)getty Glossary 150
Bash Guide for Beginners Control console devices. gimp Image manipulation program. grep Print lines matching a pattern. groff Emulate nroff command with groff. grub The grub shell. gv A PostScript and PDF viewer. gzip Compress or expand files.
H
halt Stop the system. head Output the first part of files. help Display help on a shell builtin command. host DNS lookup utility. httpd Apache hypertext transfer protocol server.
I
id Print real and effective UIDs and GIDs. ifconfig Configure network interface or show configuration. info Read Info documents. init Process control initialization. iostat Display I/O statistics. ip Display/change network interface status. ipchains IP firewall administration. iptables IP packet filter administration.
J
jar Glossary 151
Bash Guide for Beginners Java archive tool. jobs List backgrounded tasks.
K
kdm Desktop manager for KDE. kill(all) Terminate process(es). ksh Open a Korn shell.
L
ldapmodify Modify an LDAP entry. ldapsearch LDAP search tool. less more with features. lilo Linux boot loader. links Text mode WWW browser. ln Make links between files. loadkeys Load keyboard translation tables. locate Find files. logout Close current shell. lp Send requests to the LP print service. lpc Line printer control program. lpq Print spool queue examination program. lpr Offline print. lprm Remove print requests. ls List directory content. lynx Text mode WWW browser.
Glossary
152
M
mail Send and receive mail. man Read man pages. mcopy Copy MSDOS files to/from Unix. mdir Display an MSDOS directory. memusage Display memory usage. memusagestat Display memory usage statistics. mesg Control write access to your terminal. mformat Add an MSDOS file system to a lowlevel formatted floppy disk. mkbootdisk Creates a standalone boot floppy for the running system. mkdir Create directory. mkisofs Create a hybrid ISO9660 filesystem. more Filter for displaying text one screen at the time. mount Mount a file system or display information about mounted file systems. mozilla Web browser. mt Control magnetic tape drive operation. mtr Network diagnostic tool. mv Rename files.
N
named Internet domain name server. ncftp Browser program for ftp services (insecure!). netstat Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. nfsstat Print statistics about networked file systems. nice Run a program with modified scheduling priority. Glossary 153
Bash Guide for Beginners nmap Network exploration tool and security scanner. ntsysv Simple interface for configuring run levels.
P
passwd Change password. pdf2ps Ghostscript PDF to PostScript translator. perl Practical Extraction and Report Language. pg Page through text output. ping Send echo request to a host. pr Convert text files for printing. printenv Print all or part of environment. procmail Autonomous mail processor. ps Report process status. pstree Display a tree of processes. pwd Print present working directory.
Q
quota Display disk usage and limits.
R
rcp Remote copy (unsafe!) rdesktop Remote Desktop Protocol client. reboot Stop and restart the system. renice Alter priority of a running process. rlogin Remote login (telnet, insecure!). rm Remove a file. Glossary 154
Bash Guide for Beginners rmdir Remove a directory. roff A survey of the roff typesetting system. rpm RPM Package Manager. rsh Remote shell (insecure!).
S
scp Secure remote copy. screen Screen manager with VT100 emulation. set Display, set or change variable. setterm Set terminal attributes. sftp Secure (encrypted) ftp. sh Open a standard shell. shutdown Bring the system down. sleep Wait for a given period. slocate Security Enhanced version of the GNU Locate. slrnn text mode Usenet client. snort Network intrusion detection tool. sort Sort lines of text files. ssh Secure shell. sshkeygen Authentication key generation. stty Change and print terminal line settings. su Switch user.
T
tac Concatenate and print files in reverse. tail Output the last part of files. Glossary 155
Bash Guide for Beginners talk Talk to a user. tar Archiving utility. tcsh Open a Turbo C shell. telnet User interface to the TELNET protocol (insecure!). tex Text formatting and typesetting. time Time a simple command or give resource usage. tin News reading program. top Display top CPU processes. touch Change file timestamps. traceroute Print the route packets take to network host. tripwire A file integrity checker for UNIX systems. troff Format documents. twm Tab Window Manager for the X Window System.
U
ulimit Controll resources. umask Set user file creation mask. umount Unmount a file system. uncompress Decompress compressed files. uniq Remove duplicate lines from a sorted file. update Kernel daemon to flush dirty buffers back to disk. uptime Display system uptime and average load. userdel Delete a user account and related files.
V
vi(m) Start the vi (improved) editor. Glossary 156
Bash Guide for Beginners vimtutor The Vim tutor. vmstat Report virtual memory statistics.
W
w Show who is logged on and what they are doing. wall Send a message to everybody's terminal. wc Print the number of bytes, words and lines in files. which Shows the full path of (shell) commands. who Show who is logged on. who am i Print effective user ID. whois Query a whois or nicname database. write Send a message to another user.
X
xauth X authority file utility. xcdroast Graphical front end to cdrecord. xclock Analog/digital clock for X. xconsole Monitor system console messages with X. xdm X Display Manager with support for XDMCP, host chooser. xdvi DVI viewer. xfs X font server. xhost Server access control program for X xinetd The extended Internet services daemon. xload System load average display for X. xlsfonts Server font list displayer for X. xmms Audio player for X. Glossary 157
Bash Guide for Beginners xpdf PDF viewer. xterm Terminal emulator for X.
Z
zcat Compress or expand files. zgrep Search possibly compressed files for a regular expression. zmore Filter for viewing compressed text.
Glossary
158