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]> 3.9 15 May 2006 Revision History Revision 3.7 23 Oct 2005 Revised by: mc 'WHORTLEBERRY' release: Bugfix Update. Revision 3.8 26 Feb 2006 Revised by: mc 'BLAEBERRY' release: Minor Update. Revision 3.9 15 May 2006 Revised by: mc 'SPICEBERRY' 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 snippets 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. 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..................................................................................25 4.1. Variable Substitution......................................................................................................................25 4.2. Variable Assignment.......................................................................................................................27 4.3. Bash Variables Are Untyped..........................................................................................................29 4.4. Special Variable Types...................................................................................................................30 Chapter 5. Quoting...........................................................................................................................................34 5.1. Quoting Variables...........................................................................................................................34 5.2. Escaping..........................................................................................................................................35 Chapter 6. Exit and Exit Status.......................................................................................................................41 Chapter 7. Tests................................................................................................................................................43 7.1. Test Constructs...............................................................................................................................43 7.2. File test operators............................................................................................................................49 7.3. Other Comparison Operators..........................................................................................................52 7.4. Nested if/then Condition Tests.......................................................................................................57 7.5. Testing Your Knowledge of Tests..................................................................................................57 Chapter 8. Operations and Related Topics....................................................................................................59 8.1. Operators.........................................................................................................................................59 8.2. Numerical Constants.......................................................................................................................65 Part 3. Beyond the Basics.................................................................................................................................67 Chapter 9. Variables Revisited........................................................................................................................68 9.1. Internal Variables............................................................................................................................68 9.2. Manipulating Strings.......................................................................................................................84 9.2.1. Manipulating strings using awk............................................................................................90 9.2.2. Further Discussion.................................................................................................................91 9.3. Parameter Substitution....................................................................................................................91 9.4. Typing variables: declare or typeset.............................................................................................100 9.5. Indirect References to Variables...................................................................................................102 9.6. $RANDOM: generate random integer..........................................................................................105 9.7. The Double Parentheses Construct...............................................................................................114 i Advanced Bash−Scripting Guide Table of Contents Chapter 10. Loops and Branches..................................................................................................................116 10.1. Loops..........................................................................................................................................116 10.2. Nested Loops..............................................................................................................................127 10.3. Loop Control...............................................................................................................................127 10.4. Testing and Branching................................................................................................................131 Chapter 11. Internal Commands and Builtins.............................................................................................138 11.1. Job Control Commands..............................................................................................................162 Chapter 12. External Filters, Programs and Commands...........................................................................167 12.1. Basic Commands........................................................................................................................167 12.2. Complex Commands...................................................................................................................172 12.3. Time / Date Commands..............................................................................................................181 12.4. Text Processing Commands........................................................................................................184 12.5. File and Archiving Commands...................................................................................................203 12.6. Communications Commands......................................................................................................219 12.7. Terminal Control Commands.....................................................................................................233 12.8. Math Commands.........................................................................................................................234 12.9. Miscellaneous Commands..........................................................................................................243 Chapter 13. System and Administrative Commands..................................................................................255 13.1. Analyzing a System Script..........................................................................................................281 Chapter 14. Command Substitution.............................................................................................................283 Chapter 15. Arithmetic Expansion................................................................................................................289 Chapter 16. I/O Redirection...........................................................................................................................290
Recommended publications
  • GNU/Linux AI & Alife HOWTO
    GNU/Linux AI & Alife HOWTO GNU/Linux AI & Alife HOWTO Table of Contents GNU/Linux AI & Alife HOWTO......................................................................................................................1 by John Eikenberry..................................................................................................................................1 1. Introduction..........................................................................................................................................1 2. Symbolic Systems (GOFAI)................................................................................................................1 3. Connectionism.....................................................................................................................................1 4. Evolutionary Computing......................................................................................................................1 5. Alife & Complex Systems...................................................................................................................1 6. Agents & Robotics...............................................................................................................................1 7. Statistical & Machine Learning...........................................................................................................2 8. Missing & Dead...................................................................................................................................2 1. Introduction.........................................................................................................................................2
    [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]
  • 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]
  • 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]
  • Evolution of Autopoiesis and Multicellularity in the Game of Life
    Evolution of Autopoiesis and Peter D. Turney* Ronin Institute Multicellularity in the Game of Life [email protected] Keywords Evolution, autopoiesis, multicellularity, Abstract Recently we introduced a model of symbiosis, Model-S, cellular automata, diversity, symbiosis based on the evolution of seed patterns in Conwayʼs Game of Life. In the model, the fitness of a seed pattern is measured by one-on-one competitions in the Immigration Game, a two-player variation of the Downloaded from http://direct.mit.edu/artl/article-pdf/27/1/26/1925167/artl_a_00334.pdf by guest on 25 September 2021 Game of Life. Our previous article showed that Model-S can serve as a highly abstract, simplified model of biological life: (1) The initial seed pattern is analogous to a genome. (2) The changes as the game runs are analogous to the development of the phenome. (3) Tournament selection in Model-S is analogous to natural selection in biology. (4) The Immigration Game in Model-S is analogous to competition in biology. (5) The first three layers in Model-S are analogous to biological reproduction. (6) The fusion of seed patterns in Model-S is analogous to symbiosis. The current article takes this analogy two steps further: (7) Autopoietic structures in the Game of Life (still lifes, oscillators, and spaceships—collectively known as ashes) are analogous to cells in biology. (8) The seed patterns in the Game of Life give rise to multiple, diverse, cooperating autopoietic structures, analogous to multicellular biological life. We use the apgsearch software (Ash Pattern Generator Search), developed by Adam Goucher for the study of ashes, to analyze autopoiesis and multicellularity in Model-S.
    [Show full text]
  • Gnu Coreutils Core GNU Utilities for Version 5.93, 2 November 2005
    gnu Coreutils Core GNU utilities for version 5.93, 2 November 2005 David MacKenzie et al. This manual documents version 5.93 of the gnu core utilities, including the standard pro- grams for text and file manipulation. Copyright c 1994, 1995, 1996, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 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 no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License”. Chapter 1: Introduction 1 1 Introduction This manual is a work in progress: many sections make no attempt to explain basic concepts in a way suitable for novices. Thus, if you are interested, please get involved in improving this manual. The entire gnu community will benefit. The gnu utilities documented here are mostly compatible with the POSIX standard. Please report bugs to [email protected]. Remember to include the version number, machine architecture, input files, and any other information needed to reproduce the bug: your input, what you expected, what you got, and why it is wrong. Diffs are welcome, but please include a description of the problem as well, since this is sometimes difficult to infer. See section “Bugs” in Using and Porting GNU CC. This manual was originally derived from the Unix man pages in the distributions, which were written by David MacKenzie and updated by Jim Meyering.
    [Show full text]
  • Cfengine V2 Reference Edition 2.2.10 for Version 2.2.10
    cfengine v2 reference Edition 2.2.10 for version 2.2.10 Mark Burgess Faculty of Engineering, Oslo University College, Norway Copyright c 2008 Mark Burgess This manual corresponds to CFENGINE Edition 2.2.10 for version 2.2.10 as last updated 21 January 2009. Chapter 1: Introduction to reference manual 1 1 Introduction to reference manual The purpose of the cfengine reference manual is to collect together and document the raw facts about the different components of cfengine. Once you have become proficient in the use of cfengine, you will no longer have need of the tutorial. The reference manual, on the other hand, changes with each version of cfengine. You will be able to use it online, or in printed form to find out the details you require to implement configurations in practice. 1.1 Installation In order to install cfengine, you should first ensure that the following packages are installed. OpenSSL Open source Secure Sockets Layer for encryption. URL: http://www.openssl.org BerkeleyDB (version 3.2 or later) Light-weight flat-file database system. URL: http://www.oracle.com/technology/products/berkeley-db/index.html The preferred method of installation is then tar zxf cfengine-x.x.x.tar.gz cd cfengine-x.x.x ./configure make make install This results in binaries being installed in `/usr/local/sbin'. Since this is not necessarily a local file system on all hosts, users are encouraged to keep local copies of the binaries on each host, inside the cfengine trusted work directory. 1.2 Work directory In order to achieve the desired simplifications, it was decided to reserve a private work area for the cfengine tool-set.
    [Show full text]
  • Owner's Manual Series 1 Hard Drive
    AMS-150HD/ AMS-150HD-SSD/ AMS-150-SSD AMS-300HD/ AMS-300HD-SSD/ AMS-300-SSD OWNER’S MANUAL SERIES 1 HARD DRIVE NOTICE: The information contained within this manual is correct at time of printing, but due to the continuing development of prod ucts, changes in specifications are inevitable. Ameri-Shred reserves the right to implement such changes without prior notice. Service Department: 888.270.6879 TABLE OF CONTENTS UNLOADING/UNPACKING ......................................................................................................................... 3 ELECTRICAL INSTALLATION ........................................................................................................................ 4 NAMEPLATE (LOG SHREDDER SPECIFICS) ................................................................................................... 4 SAFETY WARNINGS .................................................................................................................................... 5 SHREDDER OPERATION .............................................................................................................................. 6 START UP PROCEDURE ............................................................................................................................... 7 SERVER DRIVE JAM WARNING ................................................................................................................... 8 CLEARING A JAM .......................................................................................................................................
    [Show full text]
  • Anti Forensics Analysis of File Wiping Tools
    Anti Forensics Analysis of File Wiping Tools A Thesis Submitted in Partial Fulfillment of the Requirements for the Degree of Master of Science in Cyber Security by Narendra Panwar 14MS013 Under the Supervision of Dr. Babu M. Mehtre Associate Professor Center For Cyber Security, Institute For Development And Research In Banking Technology, Hyderabad (Established by Reserve Bank of India) COMPUTER SCIENCE AND ENGINEERING DEPARTMENT SARDAR PATEL UNIVERSITY OF POLICE, SECURITY AND CRIMINAL JUSTICE JODHPUR – 342304, INDIA May, 2016 UNDERTAKING I declare that the work presented in this thesis titled “Anti Forensics Analysis of File Wiping Tools”, submitted to the Computer Science and Engineering Department, Sardar Patel Uni- versity of Police, Security and Criminal Justice, Jodhpur, for the award of the Master of Science degree in Cyber Security, is my original work. I have not plagiarized or submitted the same work for the award of any other degree. In case this undertaking is found in- correct, I accept that my degree may be unconditionally withdrawn. May, 2016 Hyderabad (Narendra Panwar) ii CERTIFICATE Certified that the work contained in the thesis titled “Anti Forensics Analysis of File Wiping Tools”, by Narendra Panwar, Registration Number 14MS013 has been carried out under my supervision and that this work has not been submitted elsewhere for a degree. Dr. Babu M. Mehtre Associate Professor Center For Cyber Security, Institute For Development and Research in Banking Technology, Hyderabad May, 2016 iii Acknowledgment The success of this project work and thesis completion required a lot of guidance. I would first like to thank my supervisor, Dr. Babu M.
    [Show full text]