Advanced Bash-Scripting Guide

Total Page:16

File Type:pdf, Size:1020Kb

Advanced Bash-Scripting Guide Advanced Bash-Scripting Guide An in-depth exploration of the art of shell scripting Mendel Cooper <[email protected]> 5.5 23 November 2008 Revision History Revision 5.3 05 May 2008 Revised by: mc 'GOLDENBERRY' release: Minor Update. Revision 5.4 21 July 2008 Revised by: mc 'ANGLEBERRY' release: Major Update. Revision 5.5 23 Nov 2008 Revised by: mc 'FARKLEBERRY' release: Minor Update. This tutorial assumes no previous knowledge of scripting or programming, but progresses rapidly toward an intermediate/advanced level of instruction . all the while sneaking in little nuggets of UNIX® wisdom and lore. It serves as a textbook, a manual for self-study, and a reference and source of knowledge on shell scripting techniques. The exercises and heavily-commented examples invite active reader participation, under the premise that the only way to really learn scripting is to write scripts. This book is suitable for classroom use as a general introduction to programming concepts. The latest update of this document, as an archived, bzip2-ed "tarball" including both the SGML source and rendered HTML, may be downloaded from the author's home site. A pdf version is also available ( pdf mirror site). See the change log for a revision history. Dedication For Anita, the source of all the magic Advanced Bash-Scripting Guide Table of Contents Chapter 1. Why Shell Programming?...............................................................................................................1 Chapter 2. Starting Off With a Sha-Bang........................................................................................................3 2.1. Invoking the script............................................................................................................................6 2.2. Preliminary Exercises.......................................................................................................................6 Part 2. Basics.......................................................................................................................................................7 Chapter 3. Special Characters...........................................................................................................................8 Chapter 4. Introduction to Variables and Parameters..................................................................................28 4.1. Variable Substitution......................................................................................................................28 4.2. Variable Assignment.......................................................................................................................31 4.3. Bash Variables Are Untyped..........................................................................................................32 4.4. Special Variable Types...................................................................................................................33 Chapter 5. Quoting...........................................................................................................................................39 5.1. Quoting Variables...........................................................................................................................39 5.2. Escaping..........................................................................................................................................41 Chapter 6. Exit and Exit Status.......................................................................................................................47 Chapter 7. Tests................................................................................................................................................49 7.1. Test Constructs...............................................................................................................................49 7.2. File test operators............................................................................................................................56 7.3. Other Comparison Operators..........................................................................................................59 7.4. Nested if/then Condition Tests.......................................................................................................64 7.5. Testing Your Knowledge of Tests..................................................................................................65 Chapter 8. Operations and Related Topics....................................................................................................66 8.1. Operators.........................................................................................................................................66 8.2. Numerical Constants.......................................................................................................................72 Part 3. Beyond the Basics.................................................................................................................................74 Chapter 9. Variables Revisited........................................................................................................................75 9.1. Internal Variables............................................................................................................................75 9.2. Manipulating Strings.......................................................................................................................92 9.2.1. Manipulating strings using awk..........................................................................................100 9.2.2. Further Reference................................................................................................................100 9.3. Parameter Substitution..................................................................................................................101 9.4. Typing variables: declare or typeset.............................................................................................110 9.4.1. Another use for declare.......................................................................................................112 9.5. Indirect References.......................................................................................................................112 9.6. $RANDOM: generate random integer..........................................................................................116 9.7. The Double-Parentheses Construct...............................................................................................127 i Advanced Bash-Scripting Guide Table of Contents Chapter 10. Loops and Branches..................................................................................................................129 10.1. Loops..........................................................................................................................................129 10.2. Nested Loops..............................................................................................................................142 10.3. Loop Control...............................................................................................................................143 10.4. Testing and Branching................................................................................................................146 Chapter 11. Command Substitution.............................................................................................................155 Chapter 12. Arithmetic Expansion................................................................................................................161 Chapter 13. Recess Time................................................................................................................................162 Part 4. Commands..........................................................................................................................................163 Chapter 14. Internal Commands and Builtins.............................................................................................171 14.1. Job Control Commands..............................................................................................................199 Chapter 15. External Filters, Programs and Commands...........................................................................204 15.1. Basic Commands........................................................................................................................204 15.2. Complex Commands...................................................................................................................209 15.3. Time / Date Commands..............................................................................................................219 15.4. Text Processing Commands........................................................................................................223 15.5. File and Archiving Commands...................................................................................................245 15.6. Communications Commands......................................................................................................263 15.7. Terminal Control Commands.....................................................................................................277 15.8. Math Commands.........................................................................................................................278 15.9. Miscellaneous Commands..........................................................................................................289
Recommended publications
  • Fourth Amendment--Requiring Probable Cause for Searches and Seizures Under the Plain View Doctrine Elsie Romero
    Journal of Criminal Law and Criminology Volume 78 Article 3 Issue 4 Winter Winter 1988 Fourth Amendment--Requiring Probable Cause for Searches and Seizures under the Plain View Doctrine Elsie Romero Follow this and additional works at: https://scholarlycommons.law.northwestern.edu/jclc Part of the Criminal Law Commons, Criminology Commons, and the Criminology and Criminal Justice Commons Recommended Citation Elsie Romero, Fourth Amendment--Requiring Probable Cause for Searches and Seizures under the Plain View Doctrine, 78 J. Crim. L. & Criminology 763 (1987-1988) This Supreme Court Review is brought to you for free and open access by Northwestern University School of Law Scholarly Commons. It has been accepted for inclusion in Journal of Criminal Law and Criminology by an authorized editor of Northwestern University School of Law Scholarly Commons. 0091-4169/88/7804-763 THE JOURNAL OF CRIMINAL LAw & CRIMINOLOGY Vol. 78, No. 4 Copyright @ 1988 by Northwestern University, School of Law Printed in U.S.A. FOURTH AMENDMENT-REQUIRING PROBABLE CAUSE FOR SEARCHES AND SEIZURES UNDER THE PLAIN VIEW DOCTRINE Arizona v. Hicks, 107 S. Ct. 1149 (1987). I. INTRODUCTION The fourth amendment to the United States Constitution pro- tects individuals against arbitrary and unreasonable searches and seizures. 1 Fourth amendment protection has repeatedly been found to include a general requirement of a warrant based on probable cause for any search or seizure by a law enforcement agent.2 How- ever, there exist a limited number of "specifically established and
    [Show full text]
  • 101 Useful Linux Commands - Haydenjames.Io
    101 Useful Linux Commands - haydenjames.io Some of these commands require elevated permissions (sudo) to run. Enjoy! 1. Execute the previous command used: !! 2. Execute a previous command starting with a specific letter. Example: !s 3. Short way to copy or backup a file before you edit it. For example, copy nginx.conf cp nginx.conf{,.bak} 4. Toggle between current directory and last directory cd - 5. Move to parent (higher level) directory. Note the space! cd .. 6. Go to home directory cd ~ 7. Go to home directory cd $HOME 8. Go to home directory (when used alone) cd 9. Set permissions to 755. Corresponds to these permissions: (-rwx-r-x-r-x), arranged in this sequence: (owner-group-other) chmod 755 <filename> 10. Add execute permission to all users. chmod a+x <filename> 11. Changes ownership of a file or directory to . chown <username> 12. Make a backup copy of a file (named file.backup) cp <file> <file>.backup 13. Copy file1, use it to create file2 cp <file1> <file2> 14. Copy directory1 and all its contents (recursively) into directory2 cp -r <directory1> <directory2>/ 15. Display date date 16. Zero the sdb drive. You may want to use GParted to format the drive afterward. You need elevated permissions to run this (sudo). dd if=/dev/zero of=/dev/sdb 17. Display disk space usage df -h 18. Take detailed messages from OS and input to text file dmesg>dmesg.txt 19. Display a LOT of system information. I usually pipe output to less. You need elevated permissions to run this (sudo).
    [Show full text]
  • Reglas De Congo: Palo Monte Mayombe) a Book by Lydia Cabrera an English Translation from the Spanish
    THE KONGO RULE: THE PALO MONTE MAYOMBE WISDOM SOCIETY (REGLAS DE CONGO: PALO MONTE MAYOMBE) A BOOK BY LYDIA CABRERA AN ENGLISH TRANSLATION FROM THE SPANISH Donato Fhunsu A dissertation submitted to the faculty of the University of North Carolina at Chapel Hill in partial fulfillment of the requirements for the degree of Doctor of Philosophy in the Department of English and Comparative Literature (Comparative Literature). Chapel Hill 2016 Approved by: Inger S. B. Brodey Todd Ramón Ochoa Marsha S. Collins Tanya L. Shields Madeline G. Levine © 2016 Donato Fhunsu ALL RIGHTS RESERVED ii ABSTRACT Donato Fhunsu: The Kongo Rule: The Palo Monte Mayombe Wisdom Society (Reglas de Congo: Palo Monte Mayombe) A Book by Lydia Cabrera An English Translation from the Spanish (Under the direction of Inger S. B. Brodey and Todd Ramón Ochoa) This dissertation is a critical analysis and annotated translation, from Spanish into English, of the book Reglas de Congo: Palo Monte Mayombe, by the Cuban anthropologist, artist, and writer Lydia Cabrera (1899-1991). Cabrera’s text is a hybrid ethnographic book of religion, slave narratives (oral history), and folklore (songs, poetry) that she devoted to a group of Afro-Cubans known as “los Congos de Cuba,” descendants of the Africans who were brought to the Caribbean island of Cuba during the trans-Atlantic Ocean African slave trade from the former Kongo Kingdom, which occupied the present-day southwestern part of Congo-Kinshasa, Congo-Brazzaville, Cabinda, and northern Angola. The Kongo Kingdom had formal contact with Christianity through the Kingdom of Portugal as early as the 1490s.
    [Show full text]
  • The Book of Common Prayer
    The Book of Common Prayer and Administration of the Sacraments and Other Rites and Ceremonies of the Church Together with The Psalter or Psalms of David According to the use of The Episcopal Church Church Publishing Incorporated, New York Certificate I certify that this edition of The Book of Common Prayer has been compared with a certified copy of the Standard Book, as the Canon directs, and that it conforms thereto. Gregory Michael Howe Custodian of the Standard Book of Common Prayer January, 2007 Table of Contents The Ratification of the Book of Common Prayer 8 The Preface 9 Concerning the Service of the Church 13 The Calendar of the Church Year 15 The Daily Office Daily Morning Prayer: Rite One 37 Daily Evening Prayer: Rite One 61 Daily Morning Prayer: Rite Two 75 Noonday Prayer 103 Order of Worship for the Evening 108 Daily Evening Prayer: Rite Two 115 Compline 127 Daily Devotions for Individuals and Families 137 Table of Suggested Canticles 144 The Great Litany 148 The Collects: Traditional Seasons of the Year 159 Holy Days 185 Common of Saints 195 Various Occasions 199 The Collects: Contemporary Seasons of the Year 211 Holy Days 237 Common of Saints 246 Various Occasions 251 Proper Liturgies for Special Days Ash Wednesday 264 Palm Sunday 270 Maundy Thursday 274 Good Friday 276 Holy Saturday 283 The Great Vigil of Easter 285 Holy Baptism 299 The Holy Eucharist An Exhortation 316 A Penitential Order: Rite One 319 The Holy Eucharist: Rite One 323 A Penitential Order: Rite Two 351 The Holy Eucharist: Rite Two 355 Prayers of the People
    [Show full text]
  • On Miracles and Medicine: Negotiating
    View metadata, citation and similar papers at core.ac.uk brought to you by CORE provided by Vanderbilt Electronic Thesis and Dissertation Archive ON MIRACLES AND MEDICINE: NEGOTIATING RELIGIOUS VALUES AT THE END OF LIFE By Trevor M. Bibler Dissertation Submitted to the Faculty of the Graduate School of Vanderbilt University in partial fulfillment of the requirements for the degree of DOCTOR OF PHILOSOPHY in Religion December, 2014 Nashville, Tennessee Approved: Larry R. Churchill, PhD Victor A. Anderson, PhD Dr. Keith Meador Elizabeth Heitman, PhD Copyright © 2014 by Trevor M. Bibler All Rights Reserved ii To my family and all the people I love. iii ACKNOWLEDGEMENTS I owe a great debt to all of the scholars and teachers who have helped me become the person I am today. Faculty from the University of Northern Iowa’s Department of Philosophy and World Religions showed me the importance of critical thinking and historical understanding. The faculty at Vanderbilt Divinity School showed me how to ask good questions. And members of my dissertation committee reminded me that answering questions—or, at least trying to answer questions—requires not just careful thought, but nerve as well. I am also grateful for the love and support my wife and family have given. I can tell that they love and care about me. Every day I think about them. I have also been fortunate to make a number of great friends while in Nashville. I owe them thanks too. Whatever value this dissertation has is due to these remarkable, loving, and supportive people. iv TABLE OF CONTENTS DEDICATION.............................................................................................................................
    [Show full text]
  • Wildlife Management Activities and Practices
    WILDLIFE MANAGEMENT ACTIVITIES AND PRACTICES COMPREHENSIVE WILDLIFE MANAGEMENT PLANNING GUIDELINES for the Edwards Plateau and Cross Timbers & Prairies Ecological Regions Revised April 2010 The following Texas Parks & Wildlife Department staff have contributed to this document: Mike Krueger, Technical Guidance Biologist – Lampasas Mike Reagan, Technical Guidance Biologist -- Wimberley Jim Dillard, Technical Guidance Biologist -- Mineral Wells (Retired) Kirby Brown, Private Lands and Habitat Program Director (Retired) Linda Campbell, Program Director, Private Lands & Public Hunting Program--Austin Linda McMurry, Private Lands and Public Hunting Program Assistant -- Austin With Additional Contributions From: Kevin Schwausch, Private Lands Biologist -- Burnet Terry Turney, Rare Species Biologist--San Marcos Trey Carpenter, Manager, Granger Wildlife Management Area Dale Prochaska, Private Lands Biologist – Kerr Wildlife Management Area Nathan Rains, Private Lands Biologist – Cleburne TABLE OF CONTENTS Comprehensive Wildlife Management Planning Guidelines Edwards Plateau and Cross Timbers & Prairies Ecological Regions Introduction Specific Habitat Management Practices HABITAT CONTROL EROSION CONTROL PREDATOR CONTROL PROVIDING SUPPLEMENTAL WATER PROVIDING SUPPLEMENTAL FOOD PROVIDING SUPPLEMENTAL SHELTER CENSUS APPENDICES APPENDIX A: General Habitat Management Considerations, Recommendations, and Intensity Levels APPENDIX B: Determining Qualification for Wildlife Management Use APPENDIX C: Wildlife Management Plan Overview APPENDIX D: Livestock
    [Show full text]
  • GNU Coreutils Cheat Sheet (V1.00) Created by Peteris Krumins ([email protected], -- Good Coders Code, Great Coders Reuse)
    GNU Coreutils Cheat Sheet (v1.00) Created by Peteris Krumins ([email protected], www.catonmat.net -- good coders code, great coders reuse) Utility Description Utility Description arch Print machine hardware name nproc Print the number of processors base64 Base64 encode/decode strings or files od Dump files in octal and other formats basename Strip directory and suffix from file names paste Merge lines of files cat Concatenate files and print on the standard output pathchk Check whether file names are valid or portable chcon Change SELinux context of file pinky Lightweight finger chgrp Change group ownership of files pr Convert text files for printing chmod Change permission modes of files printenv Print all or part of environment chown Change user and group ownership of files printf Format and print data chroot Run command or shell with special root directory ptx Permuted index for GNU, with keywords in their context cksum Print CRC checksum and byte counts pwd Print current directory comm Compare two sorted files line by line readlink Display value of a symbolic link cp Copy files realpath Print the resolved file name csplit Split a file into context-determined pieces rm Delete files cut Remove parts of lines of files rmdir Remove directories date Print or set the system date and time runcon Run command with specified security context dd Convert a file while copying it seq Print sequence of numbers to standard output df Summarize free disk space setuidgid Run a command with the UID and GID of a specified user dir Briefly list directory
    [Show full text]
  • Constraints in Dynamic Symbolic Execution: Bitvectors Or Integers?
    Constraints in Dynamic Symbolic Execution: Bitvectors or Integers? Timotej Kapus, Martin Nowack, and Cristian Cadar Imperial College London, UK ft.kapus,m.nowack,[email protected] Abstract. Dynamic symbolic execution is a technique that analyses programs by gathering mathematical constraints along execution paths. To achieve bit-level precision, one must use the theory of bitvectors. However, other theories might achieve higher performance, justifying in some cases the possible loss of precision. In this paper, we explore the impact of using the theory of integers on the precision and performance of dynamic symbolic execution of C programs. In particular, we compare an implementation of the symbolic executor KLEE using a partial solver based on the theory of integers, with a standard implementation of KLEE using a solver based on the theory of bitvectors, both employing the popular SMT solver Z3. To our surprise, our evaluation on a synthetic sort benchmark, the ECA set of Test-Comp 2019 benchmarks, and GNU Coreutils revealed that for most applications the integer solver did not lead to any loss of precision, but the overall performance difference was rarely significant. 1 Introduction Dynamic symbolic execution is a popular program analysis technique that aims to systematically explore all the paths in a program. It has been very successful in bug finding and test case generation [3, 4]. The research community and industry have produced many tools performing symbolic execution, such as CREST [5], FuzzBALL [9], KLEE [2], PEX [14], and SAGE [6], among others. To illustrate how dynamic symbolic execution works, consider the program shown in Figure 1a.
    [Show full text]
  • The St.Johns News
    THE ST.JOHNS NEWS :xni. —NO. 1 THE ST. JOHNS MEWS, THUMDAY APTEKNOOM-^UGUST 10.1911. BRarrpj OWE DOI.tAB A TEAM at dtr. Mia. (Iiwm C. HmiA wmm tofar o Um am aieapsi, aai Attraeioi I at Lamalmm a^ CIiaub H. of Do- BO IttUo Attoatioa. UAit. Ho a Jm Iooyoo tliroo IHtlo Bat tto crowalag oYoat wao tto' <rAa4MAM«iitorK. Joamm Uo Kmn —. GLMBimf torse moo la whiofc aaae tot fanaors MtsAWth H om Aoioom aii 4 Poom wore Alloered to paitldpate. Doukt- I IT ML? W^nor. lea A pmfssoieaal starter wooia tore ! PUMomI -oonrteoo will to toM At ttoMaht tto whole Affair ooaMwtot Ir- OR WRIJLB T«r RdM IIIIST tto to«M> oa I^Molos oirooc tklo Af* tnisizEss retpilar. but tto haroos toew tto)r| KTIESmilLM tinistn. AMd tto tatiifsat wode la ------------ . wore tto eoater at Attraetloe aad ANIKAL Pll lit Joluia rooMtOTT. ! wMt down t?Mk nMl up mbIh tn i »mi HKLD RALLY AT PAIR ttRilUMRHja oMnaor that was sure waoaah elAA>| PARIHH WILL TRLIRRATE BIA. You smat osrtalaly would aat. aa ICLINTBR COfniTT MTIIRAY NCI LAHT TRrRHRAT. sy. aad tto «rtuv. At^. J. M. Ho^o. ■miR JITMLBR. ywu know from expertaaoe that lf| FMESMEDBrillTO now A farwier la Oreoabuoli, aaro orl* you aaa tto aaimal aad haap both epaa ^ ABBMTIATHMI ------------ > toacr of considerable trottlas up ead j opan bafore maktag a bargain, you; ORNBIBT N. 6.1NBDMN ____ __ down the good roods of hie townohlp I adll ataad a chaace to laaa. FNF PHIMi mniFD (IIITI Tton why not exarelae tto a UDTOIffEILOKE UMIHILS UUI I Thursdar was Alsipljr a practloo REV.
    [Show full text]
  • Jackson State University Department of Computer Science CSC 438-01/539-01 Systems and Software Security, Spring 2014 Instructor: Dr
    Jackson State University Department of Computer Science CSC 438-01/539-01 Systems and Software Security, Spring 2014 Instructor: Dr. Natarajan Meghanathan Project 1: Exploring UNIX Access Control in a Virtual Machine Environment Due: February 26, 2014, 7.30 PM The objective of this project is to explore the different UNIX access control commands and their features. You will do this project in a virtual machine environment. If you already have a virtual machine installed (either in VM Player or Virtual Box, you can skip the following steps and proceed to Page 4). Installing VirtualBox 4.2 and Ubuntu OS Go to https://www.virtualbox.org/wiki/Downloads and download VirtualBox for your operating system. If you work on a lab computer, you need to use the Ubuntu VM .iso file that is stored on the local machine. If you work on your personal computer, you need to download the Ubuntu .iso file from the website listed in Step # 1 and continue. You may use the following steps for installing the Ubuntu VM on the virtualbox. 1. The Ubuntu installation file is located on the desktop of your PC (it can be downloaded from http://www.ubuntu.com/download/ubuntu/download if the .iso file cannot be located on your desktop). 2. On the VirtualBox Manager screen click on “New” 1 3. When prompted, put your J # for the name of the VM and select “Linux” as OS (when you choose Linux as OS, the program should automatically choose Ubuntu as Version, if not select Ubuntu) and click Next. 4.
    [Show full text]
  • SHRED DOCUMENTATION ZONGE Data Processing GDP Data
    SHRED DOCUMENTATION ZONGE Data Processing GDP Data Reformat Program version 3.2x Barry Sanders Mykle Raymond John Rykala November, 1996 Zonge Engineering & Research Organization, Inc. 3322 East Fort Lowell Road, Tucson, AZ 85716 USA Tel:(520) 327-5501 Fax:(520) 325-1588 Email:[email protected] GDP DATA PROCESSING MANUAL TABLE OF CONTENTS SHRED ............................................................................ page Introduction.............................................................................5 Usage .......................................................................................5 Software Operation.................................................................7 Calculation of Receiver Location ......................................................... 8 Survey Configurations ........................................................................... 8 Rx Definitions ........................................................................................ 8 Sorting the data file ................................................................................ 9 Splitting the data file .............................................................................. 9 Data Processing Flags.......................................................................... 10 Comment and Program Control Lines ................................................ 11 Transmitter Current Corrections ......................................................... 11 AMT Correlation Coefficient Filter...................................................
    [Show full text]
  • Cómo Citar El Artículo Número Completo Más Información Del
    Anagramas -Rumbos y sentidos de la comunicación- ISSN: 1692-2522 Sello Editorial - Universidad de Medellín Nitrihual-Valdebenito, Luis; Fierro-Bustos, Juan Manuel; Reyes-Velásquez, Carlos; Henríquez-Morales, Francisco Conspiración y nuda vida ¿The X-Files, I want to believe: mundo posible o mundo presente?* Anagramas -Rumbos y sentidos de la comunicación-, vol. 16, núm. 31, 2017, Septiembre-Diciembre, pp. 91-112 Sello Editorial - Universidad de Medellín DOI: https://doi.org/10.22395/angr.v16n31a3 Disponible en: https://www.redalyc.org/articulo.oa?id=491555087004 Cómo citar el artículo Número completo Sistema de Información Científica Redalyc Más información del artículo Red de Revistas Científicas de América Latina y el Caribe, España y Portugal Página de la revista en redalyc.org Proyecto académico sin fines de lucro, desarrollado bajo la iniciativa de acceso abierto Universidad de Medellín Conspiración y nuda vida ¿The X-Files, I want to believe: mundo posible o mundo presente?* Luis Nitrihual Valdebenito** Juan Manuel Fierro Bustos*** Carlos Reyes Velásquez**** Francisco Henríquez Morales***** Recibido: 2017-04-22 Enviado a pares: 2017-05-25 Aprobado por pares: 2017-07-18 Aceptado: 2017-07-25 DOI: 10.22395/angr.v16n31a3 Resumen El presente artículo problematiza, en general, la conspiración como una estructura política y narrativa fundamental de la Modernidad. A través de un análisis fílmico de la serie The X-Files, I want to believe, planteamos como objetivo revelar como el tópico de la conspiración, en un nivel superficial, es un articulador de la trama de la serie, pero en un nivel profundo y amplio entendemos la conspiración como una estrategia articuladora de la vida moderna y, en este marco, del control biopolítico.
    [Show full text]