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]> 2.2 31 October 2003 Revision History Revision 0.1 14 June 2000 Revised by: mc Initial release. Revision 0.2 30 October 2000 Revised by: mc Bugs fixed, plus much additional material and more example scripts. Revision 0.3 12 February 2001 Revised by: mc Another major update. Revision 0.4 08 July 2001 Revised by: mc More bugfixes, much more material, more scripts − a complete revision and expansion of the book. Revision 0.5 03 September 2001 Revised by: mc Major update. Bugfixes, material added, chapters and sections reorganized. Revision 1.0 14 October 2001 Revised by: mc Bugfixes, reorganization, material added. Stable release. Revision 1.1 06 January 2002 Revised by: mc Bugfixes, material and scripts added. Revision 1.2 31 March 2002 Revised by: mc Bugfixes, material and scripts added. Revision 1.3 02 June 2002 Revised by: mc 'TANGERINE' release: A few bugfixes, much more material and scripts added. Revision 1.4 16 June 2002 Revised by: mc 'MANGO' release: Quite a number of typos fixed, more material and scripts added. Revision 1.5 13 July 2002 Revised by: mc 'PAPAYA' release: A few bugfixes, much more material and scripts added. Revision 1.6 29 September 2002 Revised by: mc 'POMEGRANATE' release: some bugfixes, more material, one more script added. Revision 1.7 05 January 2003 Revised by: mc 'COCONUT' release: a couple of bugfixes, more material, one more script. Revision 1.8 10 May 2003 Revised by: mc 'BREADFRUIT' release: a number of bugfixes, more scripts and material. Revision 1.9 21 June 2003 Revised by: mc 'PERSIMMON' release: bugfixes and more material. Revision 2.0 24 August 2003 Revised by: mc 'GOOSEBERRY' release: Major update. Revision 2.1 14 September 2003 Revised by: mc 'HUCKLEBERRY' release: bugfixes and more material. Revision 2.2 31 October 2003 Revised by: mc 'CRANBERRY' release: Major 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. 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............................................................................................................................5 2.2. Preliminary Exercises.......................................................................................................................5 Part 2. Basics.......................................................................................................................................................6 Chapter 3. Special Characters...........................................................................................................................7 Chapter 4. Introduction to Variables and Parameters..................................................................................23 4.1. Variable Substitution......................................................................................................................23 4.2. Variable Assignment.......................................................................................................................25 4.3. Bash Variables Are Untyped..........................................................................................................26 4.4. Special Variable Types...................................................................................................................28 Chapter 5. Quoting...........................................................................................................................................32 Chapter 6. Exit and Exit Status.......................................................................................................................38 Chapter 7. Tests................................................................................................................................................40 7.1. Test Constructs...............................................................................................................................40 7.2. File test operators............................................................................................................................46 7.3. Comparison operators (binary).......................................................................................................49 7.4. Nested if/then Condition Tests.......................................................................................................54 7.5. Testing Your Knowledge of Tests..................................................................................................54 Chapter 8. Operations and Related Topics....................................................................................................55 8.1. Operators.........................................................................................................................................55 8.2. Numerical Constants.......................................................................................................................61 Part 3. Beyond the Basics.................................................................................................................................63 Chapter 9. Variables Revisited........................................................................................................................64 9.1. Internal Variables............................................................................................................................64 9.2. Manipulating Strings.......................................................................................................................80 9.2.1. Manipulating strings using awk............................................................................................84 9.2.2. Further Discussion.................................................................................................................85 9.3. Parameter Substitution....................................................................................................................85 9.4. Typing variables: declare or typeset...............................................................................................93 9.5. Indirect References to Variables.....................................................................................................95 9.6. $RANDOM: generate random integer............................................................................................97 9.7. The Double Parentheses Construct...............................................................................................105 Chapter 10. Loops and Branches..................................................................................................................107 10.1. Loops..........................................................................................................................................107 10.2. Nested Loops..............................................................................................................................117 10.3. Loop Control...............................................................................................................................118 i Advanced Bash−Scripting Guide Table of Contents Chapter 10. Loops and Branches 10.4. Testing and Branching................................................................................................................121 Chapter 11. Internal Commands and Builtins.............................................................................................129 11.1. Job Control Commands..............................................................................................................150 Chapter 12. External Filters, Programs and Commands...........................................................................154 12.1. Basic Commands........................................................................................................................154 12.2. Complex Commands...................................................................................................................157 12.3. Time / Date Commands..............................................................................................................164 12.4. Text Processing Commands........................................................................................................166 12.5. File and Archiving Commands...................................................................................................183
Recommended publications
  • Practice Test Version 1.8 LPI 117-101: Practice Exam QUESTION NO: 1 CORRECT TEXT
    LPI 117-101 117-101 LPI 101 General Linux, Part I Practice Test Version 1.8 LPI 117-101: Practice Exam QUESTION NO: 1 CORRECT TEXT You suspect that a new ethernet card might be conflicting with another device. Which file should you check within the /proc tree to learn which IRQs are being used by which kernel drives? Answer: interrupts QUESTION NO: 2 How many SCSI ids for peripherals can SCSI-1 support? A. 5 B. 6 C. 7 D. 8 Answer: C Explanation: SCSI-1 support total 7 peripherals. There are several different types of SCSI devices. The original SCSI specification is commonly referred to as SCSI-1. The newer specification, SCSI-2, offers increased speed and performance, as well as new commands. Fast SCSI increases throughput to more than 10MB per second. Fast-Wide SCSI provides a wider data path and throughput of up to 40MB per second and up to 15 devices. There there are Ultra-SCSI and Ultra-Wide-SCSI QUESTION NO: 3 You need to install a fax server. Which type of fax/modem should you install to insure Linux compatibility? Test-King.com A. External Serial Fax/modem B. External USB Fax/modem C. Internal ISA Fax/modem D. Internal PCI Fax/modem Answer: A QUESTION NO: 4 You are running Linux 2.0.36 and you need to add a USB mouse to your system. Which of the following statements is true? "Welcome to Certification's Main Event" - www.test-king.com 2 LPI 117-101: Practice Exam A. You need to rebuild the kernel.
    [Show full text]
  • Plw Sc/Nt/Ntr/Ls
    K Service Source PLW SC/NT/NTR/LS Personal LaserWriter SC, Personal LaserWriter NT, Personal LaserWriter NTR, Personal LaserWriter LS, Personal LaserWriter LS/L K Service Source Basics PLW SC/NT/NTR/LS Basics Product Information - 1 Product Information The printers covered in this manual are • Personal LaserWriter SC • Personal LaserWriter NT • Personal LaserWriter NTR • Personal LaserWriter LS • Personal LaserWriter LS/L Compatibility Not all parts are compatible among the five models. Refer to Illustrated Parts for compatibility cross references. The cassette feeder tray and its associated parts are optional on the LS, LS/L, and NTR models. Basics Paper Paths - 2 Paper Paths There are four paper paths in the Personal LaserWriter. Paper is fed from the cassette or multipurpose tray and delivered to the face-down or face-up delivery trays. Note: Face signifies image side. Default delivery is face- down at the top of the printer. Basics LS–LS/L Identification - 3 LS–LS/L Identification I/O Board Bracket The LS/L is a cost-reduced version of the LS but is sold and packaged under the same LS name. Parts are not necessarily interchangeable between the two models. Power Switch External distinguishing characteristics: • LS: The power switch is on the left rear of printer; the rear cover has an opening for an I/O board bracket and Personal LaserWriter LS displays the family number M2000. • LS/L: The power switch is on the right rear of printer; Solid Rear Cover the rear cover is solid plastic and displays the family number M2002. Power Switch Personal LaserWriter LS/L Basics Sensing System Theory - 4 Sensing System Theory There are six sensors in the PS11 Personal LaserWriter: four PS12 paper sensors and two printer-open sensors.
    [Show full text]
  • Control of Protein Orientation on Gold Nanoparticles † ∥ † § † ∥ ⊥ Wayne Lin, Thomas Insley, Marcus D
    Article pubs.acs.org/JPCC Control of Protein Orientation on Gold Nanoparticles † ∥ † § † ∥ ⊥ Wayne Lin, Thomas Insley, Marcus D. Tuttle, Lingyang Zhu, Deborah A. Berthold, Petr Kral,́, † ‡ # † Chad M. Rienstra,*, , , and Catherine J. Murphy*, † ‡ Department of Chemistry and Department of Biochemistry, University of Illinois at Urbana−Champaign, 600 South Matthews Avenue, Urbana, Illinois 61801, United States § School of Chemical Sciences, University of Illinois at Urbana−Champaign, 505 South Matthews Avenue, Urbana, Illinois 61801, United States ∥ ⊥ Department of Chemistry and Department of Physics, University of Illinois at Chicago, 845 West Taylor Street, Chicago, Illinois 60607, United States # Center for Biophysics and Computational Biology, University of Illinois at Urbana−Champaign, 607 South Matthews Avenue, Urbana, Illinois 61801, United States *S Supporting Information ABSTRACT: Gold nanoparticles (Au NPs) have attracted much attention due to their potential applications in nanomedicine. While numerous studies have quantified biomolecular adsorption to Au NPs in terms of equilibrium binding constants, far less is known about biomolecular orientation on nanoparticle surfaces. In this study, the binding of the protein α-synuclein to citrate and (16- mercaptohexadecyl)trimethylammonium bromide (MTAB)-coated 12 nm Au NPs is examined by heteronuclear single quantum coherence NMR spectroscopy to provide site-specific measurements of protein−nanoparticle binding. Molecular dynamics simulations support the orientation assignments, which
    [Show full text]
  • File System, Files, and *Tab /Etc/Fstab
    File system, files, and *tab File system files directories volumes, file systems mounting points local versus networked file systems 1 /etc/fstab Specifies what is to be mounted where and how fs_spec: describes block special device for remote filesystem to be mounted fs_file: describes the mount point fs_vfstype: describes the type of file system fs_mntops: describes the mount options associated with the filesystem 2 /etc/fstab cont. fs_freq: used by the dump command fs_passno: used by fsck to determine the order in which checks are done at boot time. Root file systems should be specified as 1, others should be 2. Value 0 means that file system does not need to be checked 3 /etc/fstab 4 from blocks to mounting points metadata inodes directories superblocks 5 mounting file systems mounting e.g., mount -a unmounting manually or during shutdown umount 6 /etc/mtab see what is mounted 7 Network File System Access file system (FS) over a network looks like a local file system to user e.g. mount user FS rather than duplicating it (which would be a disaster) Developed by Sun Microsystems (mid 80s) history for NFS: NFS, NFSv2, NFSv3, NFSv4 RFC 3530 (from 2003) take a look to see what these RFCs are like!) 8 Network File System How does this actually work? server needs to export the system client needs to mount the system server: /etc/exports file client: /etc/fstab file 9 Network File System Security concerns UID GID What problems could arise? 10 Network File System example from our raid system (what is a RAID again?) Example of exports file from
    [Show full text]
  • Filesystem Hierarchy Standard
    Filesystem Hierarchy Standard LSB Workgroup, The Linux Foundation Filesystem Hierarchy Standard LSB Workgroup, The Linux Foundation Version 3.0 Publication date March 19, 2015 Copyright © 2015 The Linux Foundation Copyright © 1994-2004 Daniel Quinlan Copyright © 2001-2004 Paul 'Rusty' Russell Copyright © 2003-2004 Christopher Yeoh Abstract This standard consists of a set of requirements and guidelines for file and directory placement under UNIX-like operating systems. The guidelines are intended to support interoperability of applications, system administration tools, development tools, and scripts as well as greater uniformity of documentation for these systems. All trademarks and copyrights are owned by their owners, unless specifically noted otherwise. Use of a term in this document should not be regarded as affecting the validity of any trademark or service mark. Permission is granted to make and distribute verbatim copies of this standard provided the copyright and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this standard under the conditions for verbatim copying, provided also that the title page is labeled as modified including a reference to the original standard, provided that information on retrieving the original standard is included, and provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this standard into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the copyright holder. Dedication This release is dedicated to the memory of Christopher Yeoh, a long-time friend and colleague, and one of the original editors of the FHS.
    [Show full text]
  • Oracle® Linux 7 Managing File Systems
    Oracle® Linux 7 Managing File Systems F32760-07 August 2021 Oracle Legal Notices Copyright © 2020, 2021, Oracle and/or its affiliates. This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited. The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing. If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, then the following notice is applicable: U.S. GOVERNMENT END USERS: Oracle programs (including any operating system, integrated software, any programs embedded, installed or activated on delivered hardware, and modifications of such programs) and Oracle computer documentation or other Oracle data delivered to or accessed by U.S. Government end users are "commercial computer software" or "commercial computer software documentation" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, the use, reproduction, duplication, release, display, disclosure, modification, preparation of derivative works, and/or adaptation of i) Oracle programs (including any operating system, integrated software, any programs embedded, installed or activated on delivered hardware, and modifications of such programs), ii) Oracle computer documentation and/or iii) other Oracle data, is subject to the rights and limitations specified in the license contained in the applicable contract.
    [Show full text]
  • Name Synopsis Description Options
    UMOUNT(8) System Administration UMOUNT(8) NAME umount − unmount file systems SYNOPSIS umount −a [−dflnrv][−t fstype][−O option...] umount [−dflnrv]{directory|device}... umount −h|−V DESCRIPTION The umount command detaches the mentioned file system(s) from the file hierarchy. A file system is spec- ified by giving the directory where it has been mounted. Giving the special device on which the file system livesmay also work, but is obsolete, mainly because it will fail in case this device was mounted on more than one directory. Note that a file system cannot be unmounted when it is ’busy’ - for example, when there are open files on it, or when some process has its working directory there, or when a swap file on it is in use. The offending process could evenbe umount itself - it opens libc, and libc in its turn may open for example locale files. Alazy unmount avoids this problem, but it may introduce another issues. See −−lazy description below. OPTIONS −a, −−all All of the filesystems described in /proc/self/mountinfo (or in deprecated /etc/mtab) are un- mounted, except the proc, devfs, devpts, sysfs, rpc_pipefs and nfsd filesystems. This list of the filesystems may be replaced by −−types umount option. −A, −−all−targets Unmount all mountpoints in the current namespace for the specified filesystem. The filesystem can be specified by one of the mountpoints or the device name (or UUID, etc.). When this option is used together with −−recursive,then all nested mounts within the filesystem are recursively un- mounted. This option is only supported on systems where /etc/mtab is a symlink to /proc/mounts.
    [Show full text]
  • Managing Network File Systems in Oracle® Solaris 11.4
    Managing Network File Systems in ® Oracle Solaris 11.4 Part No: E61004 August 2021 Managing Network File Systems in Oracle Solaris 11.4 Part No: E61004 Copyright © 2002, 2021, Oracle and/or its affiliates. This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited. The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing. If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, then the following notice is applicable: U.S. GOVERNMENT END USERS: Oracle programs (including any operating system, integrated software, any programs embedded, installed or activated on delivered hardware, and modifications of such programs) and Oracle computer documentation or other Oracle data delivered to or accessed by U.S. Government end users are "commercial computer software" or "commercial computer software documentation" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, the use, reproduction, duplication, release, display, disclosure, modification, preparation of derivative works, and/or adaptation of i) Oracle programs (including any operating system, integrated software, any programs embedded, installed or activated on delivered hardware, and modifications of such programs), ii) Oracle computer documentation and/or iii) other Oracle data, is subject to the rights and limitations specified in the license contained in the applicable contract.
    [Show full text]
  • System Analysis and Tuning Guide System Analysis and Tuning Guide SUSE Linux Enterprise Server 15 SP1
    SUSE Linux Enterprise Server 15 SP1 System Analysis and Tuning Guide System Analysis and Tuning Guide SUSE Linux Enterprise Server 15 SP1 An administrator's guide for problem detection, resolution and optimization. Find how to inspect and optimize your system by means of monitoring tools and how to eciently manage resources. Also contains an overview of common problems and solutions and of additional help and documentation resources. Publication Date: September 24, 2021 SUSE LLC 1800 South Novell Place Provo, UT 84606 USA https://documentation.suse.com Copyright © 2006– 2021 SUSE LLC and contributors. All rights reserved. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or (at your option) version 1.3; with the Invariant Section being this copyright notice and license. A copy of the license version 1.2 is included in the section entitled “GNU Free Documentation License”. For SUSE trademarks, see https://www.suse.com/company/legal/ . All other third-party trademarks are the property of their respective owners. Trademark symbols (®, ™ etc.) denote trademarks of SUSE and its aliates. Asterisks (*) denote third-party trademarks. All information found in this book has been compiled with utmost attention to detail. However, this does not guarantee complete accuracy. Neither SUSE LLC, its aliates, the authors nor the translators shall be held liable for possible errors or the consequences thereof. Contents About This Guide xii 1 Available Documentation xiii
    [Show full text]
  • Mostly-Static Decentralized Information Flow Control by Andrew C
    Mostly-Static Decentralized Information Flow Control by Andrew C. Myers Submitted to the Department of Electrical Engineering and Computer Science in partial ful®llment of the requirements for the degree of Doctor of Philosophy at the Massachusetts Institute of Technology February 1999 c Massachusetts Institute of Technology 1999. All rights reserved. Author :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Department of Electrical Engineering and Computer Science January 7, 1999 Certi®ed by :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Barbara Liskov Ford Professor of Engineering Thesis Supervisor Accepted by ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Arthur C. Smith Chairman, Departmental Committee on Graduate Students Mostly-Static Decentralized Information Flow Control by Andrew C. Myers Submitted to the Department of Electrical Engineering and Computer Science on January 7, 1999, in partial ful®llment of the requirements for the degree of Doctor of Philosophy Abstract The growing use of mobile code in downloaded programs such as applets and servlets has increased interest in robust mechanisms for ensuring privacy and secrecy. Common security mechanisms such as sandboxing and access control are either too restrictive or too weakÐthey prevent applications from sharing data usefully, or allow private information to leak. For example, security mechanisms in Java prevent many useful applications while still permitting Trojan horse applets
    [Show full text]
  • On-Demand Host-Container Filesystem Sharing
    TECHNION – ISRAEL INSTITUTE OF TECHNOLOGY On-Demand Host-Container Filesystem Sharing Bassam Yassin and Guy Barshatski Supervisor: Noam Shalev NSSL Labs Winter 2016 Implementing and adding a new feature to Docker open source project which allows a host to add extra volume to a running container 1 Contents List Of Figures ............................................................................................................................ 3 1 Abstract ................................................................................................................................. 4 2 Introduction ........................................................................................................................... 5 3 Background Theory ................................................................................................................ 6 3.1 Device .............................................................................................................................. 6 3.1.1 Character devices ..................................................................................................... 6 3.1.2 Block devices ............................................................................................................ 6 3.2 Unix-Like Filesystem ....................................................................................................... 7 3.3 Namespaces and Cgroups ............................................................................................... 9 3.3.1 Namespaces .............................................................................................................
    [Show full text]
  • Effects of Surfactants and Polyelectrolytes on the Interaction
    Article pubs.acs.org/Langmuir Effects of Surfactants and Polyelectrolytes on the Interaction between a Negatively Charged Surface and a Hydrophobic Polymer Surface † † ‡ § ∥ Michael V. Rapp, Stephen H. Donaldson, Jr., Matthew A. Gebbie, Yonas Gizaw, Peter Koenig, § † ‡ Yuri Roiter, and Jacob N. Israelachvili*, , † Department of Chemical Engineering, University of California, Santa Barbara, California 93106-5080, United States ‡ Materials Department, University of California, Santa Barbara, California 93106-5050, United States § Winton Hill Business Center, The Procter & Gamble Company, 6210 Center Hill Avenue, Cincinnati, Ohio 45224, United States ∥ Modeling and Simulation/Computational Chemistry, The Procter & Gamble Company, 8611 Beckett Road, West Chester, Ohio 45069, United States *S Supporting Information ABSTRACT: We have measured and characterized how three classes of surface-active molecules self-assemble at, and modulate the interfacial forces between, a negatively charged mica surface and a hydrophobic end-grafted polydimethylsiloxane (PDMS) polymer surface in solution. We provide a broad overview of how chemical and structural properties of surfactant molecules result in different self-assembled structures at polymer and mineral surfaces, by studying three characteristic surfactants: (1) an anionic aliphatic surfactant, sodium dodecyl sulfate (SDS), (2) a cationic aliphatic surfactant, myristyltrimethylammonium bro- mide (MTAB), and (3) a silicone polyelectrolyte with a long-chain PDMS midblock and multiple cationic end groups.
    [Show full text]