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]> 4.0 18 June 2006 Revision History 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. Revision 4.0 18 May 2006 Revised by: mc 'WINTERBERRY' 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. 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. Command Substitution.............................................................................................................138 Chapter 12. Arithmetic Expansion................................................................................................................144 Chapter 13. Recess Time................................................................................................................................145 Part 4. Commands..........................................................................................................................................146 Chapter 14. Internal Commands and Builtins.............................................................................................154 14.1. Job Control Commands..............................................................................................................179 Chapter 15. External Filters, Programs and Commands...........................................................................183 15.1. Basic Commands........................................................................................................................183 15.2. Complex Commands...................................................................................................................188 15.3. Time / Date Commands..............................................................................................................197 15.4. Text Processing Commands........................................................................................................200 15.5. File and Archiving Commands...................................................................................................220 15.6. Communications Commands......................................................................................................236 15.7. Terminal Control Commands.....................................................................................................249 15.8. Math Commands.........................................................................................................................250 15.9. Miscellaneous Commands..........................................................................................................260 Chapter 16. System and Administrative Commands..................................................................................272
Recommended publications
  • KING-MPIA-DOC-DET-004 Short Title KING 70 Cm Rocon Issue 2.169 Date June 18, 2021
    KING { 70cm Telescope { RoCon Software Manual Doc. No. KING-MPIA-DOC-DET-004 Short Title KING 70 cm Rocon Issue 2.169 Date June 18, 2021 Prepared . .Richard . .J. Mathar . .. .([email protected], . .382) . .June . 18, . .2021 . Name Date Signature Approved ...................................................................N. Surname Name Date Signature Released . .N. Surname . Name Date Signature ii KING-MPIA-DOC-DET-004 { KING 70 cm Rocon { Issue 2.169 Change Record Issue Date Sect. Reason/Initiation/Documents/Remarks 1.055 2020-02-24 all created 2.169 June 18, 2021 all SVN version 44M KING-MPIA-DOC-DET-004 { KING 70 cm Rocon { Issue 2.169 iii Contents 1 OVERVIEW 1 1.1 Acronyms..........................................1 1.2 References..........................................2 2 OBSERVER 2 2.1 Basics of Operation.....................................2 2.2 Info pages..........................................2 2.3 Prerequisites........................................ 19 2.3.1 ccdread....................................... 19 2.3.2 Temperature log.................................. 22 2.4 FITS output........................................ 22 2.5 Temperatures........................................ 23 2.6 PyRAF........................................... 24 2.7 Jupyter........................................... 24 2.8 Pointing Model....................................... 25 2.8.1 Refractive index.................................. 25 2.8.2 Model Fit...................................... 26 2.9 Dome Slit Model.....................................
    [Show full text]
  • News from the Society for Astronomical Sciences
    eu News from the Society for Astronomical Sciences Vol. 14 No.3 (November, 2016) Getting Ready for SAS- 2017 The 2017 SAS Symposium will be held on June 15-16-17, 2017, at the Ontario Airport Hotel. There will be Work- shops on topics of interest to the small-telescope research community, Technical Papers on research results and project plans, and our Sponsors will have new products on display. The SAS Program Committee is pre- paring the details of the Symposium and looking into some new features. Here’s what you can start doing now: Block the dates (June 15-16-17, 2017) on your calendar. Make travel reservations. Keep working on your projects, and decide which one you would like to present. Start preparing your Abstract Invite your colleagues who are interested in small-telescope re- Reminders ... search activities. Membership Renewal: Even if you Workshop Videos: Video recordings of can’t attend the annual Symposium, most of the Workshops from recent years are available from SAS. If you Kudos or Criticisms? we value your support of the Society for Astronomical Sciences, and your were registered for the Workshop, then the recording is free. If you were not a We are looking forward to seeing re- interest in small-telescope science. registered attendee, then the price is sults on a wide diversity of projects You can renew your membership on $50 per workshop. Contact Bob and objects at SAS-2017! If you have the SAS website (SocAstroSci.org), by Buchheim ([email protected]) for any questions or ideas for the Sympo- going to the MEMBERSHIP/REGISTRATION the details.
    [Show full text]
  • Ucam Installation Manual
    UCam Installation Manual Version 0.3 Stewart McLay 5th March 2010 Acknowledgements Professor Vikram Dhillon who was P.I. for the UltraCam instrument the project from which UCam was born. The UCam development team has involved several members of UK ATC staff. Valuable contributions were made by: David Atkinson, Steven Beard, Derek Ives, Stewart McLay, Chris Tierney and Andy Vick. The Enhanced Machine Controller (EMC) project for their knowledge base wiki which has been a valuable source of information for installing RTAI on Debian Linux. 2 Table of Contents 1Introduction........................................................................................................................................5 1.1About UltraCam..........................................................................................................................5 1.2About ARC Controller................................................................................................................5 1.3About UK Astronomy Technology Centre.................................................................................6 1.4Acronyms And Abbreviations.....................................................................................................6 1.5Stylistic Conventions..................................................................................................................6 2How To Install Debian Linux.............................................................................................................8 2.1Debian Releases..........................................................................................................................8
    [Show full text]
  • Data Reduction Helpdesk First Responder Guidelines Kathleen Labrie
    Data Reduction Helpdesk First Responder Guidelines Kathleen Labrie Science Users Support Department V2.0 – 14 November 2019 Revision History V1.0 – 16 January 2018 Kathleen Labrie V1.1 – 12 March 2018 Kathleen Labrie V2.0 – 14 November 2019 Kathleen Labrie Document ID: DPSG-USER-101_DRHDFirstResponderGuidelines Document Purpose This document aims to help the Tier 1 assignee respond to the data reduction tickets in a manner that will gather the necessary information for understanding and debugging the problem reported by the users in a timely manner. Intended Audience This document is intended primarily for Tier 1 Data Reduction Helpdesk assignee, who are often NGO members. Members of the Science Users Support Department new to handling data reduction tickets should also read this. Table of Contents 1. Introduction ............................................................................................................................. 2 2. Preparations – Assembling your First-aid Kit .......................................................................... 2 2.1 Choosing a platform ....................................................................................................... 2 2.2 AstroConda .................................................................... Error! Bookmark not defined. 2.3 IRAF and Gemini IRAF .................................................................................................. 3 2.3.1 IRAF ..........................................................................................................................
    [Show full text]
  • The Significant Properties of Software: a Study
    The Significant Properties of Software: A Study Brian Matthews, Brian McIlwrath, David Giaretta, Esther Conway STFC Rutherford Appleton Laboratory Chilton OX11 0QX UK December 2008 Significant Properties of Software Revision History: Version Date Authors Sections Affected / Comments 0.1 18/02/2008 BMM Outline 0.5 05/03/2008 BMM First Draft 0.6 09/03/2008 DG DG added section on OAIS/CASPAR 0.7 11/03/2008 BMM Added section on StarLink + revisions. 0.8 23/03/2008 BMcI, EC, BMM Expanded use cases 1.0 28/03/2008 BMM, BMcI First Complete release version 1.1 23/12/2008 BMM Final Revision 2 Significant Properties of Software Executive Summary ...................................................................................................... 5 Recommendations ..................................................................................................................... 6 1 Background to the Study ....................................................................................... 9 1.1 Introduction ................................................................................................................... 9 1.2 Significant Properties .................................................................................................. 10 2 Scope of Study .................................................................................................... 12 2.1 Definition of Software ................................................................................................ 12 2.2 Diversity of Software .................................................................................................
    [Show full text]
  • Pyraf FAQ Pyraf Pyraf FAQ
    1/12/2018 PyRAF FAQ PyRAF PyRAF FAQ Last edited: 6 Jan 2017. 0. Support Status Due to reduced budgets for HST, we can only provide minimal pyraf support. If you have a problem that cannot be solved quickly by our first tier support, we may be unable to help. This is unfortunate, but it is the best we can do with the resources available. 1. Installation 1.1. How do I download and install PyRAF? 1.2. I am on a Mac. Can I use PyRAF with/without running X11? 1.3. Can I run PyRAF without IRAF? 1.4. What are the errors about Python.h / numpy.h ? 1.5. Can I run PyRAF with the 64-bit version of IRAF (v2.15.*)? 1.6. Does PyRAF run on OSX Mountain Lion (10.8)? 1.7. How do I run PyRAF on Windows? 1.8. Does PyRAF work under Python 3? 1.9. What is "X11/X.h: No such file or directory"? 1.10. Does PyRAF work within IPython Notebook? 1.11. Do you support other downloads of PyRAF? 2. Startup 2.1. How do I execute a Python script during startup? 2.2. How can I make my PyRAF session start up faster? 2.3. Why do I get messages about the "CL script cache" when I start up? 2.4. Why does PyRAF fail on startup with a message about togl.so? 2.5. How can I tell PyRAF that I want to run without graphics? 2.6. Why do I see "Could not import aqutil"? What's that? 2.7.
    [Show full text]
  • PAPI Documentation Release 2.0.20200122115013
    PAPI Documentation Release 2.0.20200122115013 J.M.Ibáñez-Mengual (IAA-CSIC) Feb 04, 2020 CONTENTS 1 Caveat 3 2 Contents 5 2.1 Installation & Configuration.....................................5 2.1.1 Requirements and Supported Platforms..........................5 2.1.1.1 System Requirements...............................5 2.1.1.2 Software Requirements..............................5 2.1.2 Download..........................................6 2.1.3 Environment Installation..................................6 2.1.4 PAPI Installation......................................8 2.1.5 Installing for developers..................................8 2.1.6 Building the documentation................................9 2.1.7 Bug reports.........................................9 2.1.8 Release Notes........................................9 2.2 PANIC Quick-Look Tool (PQL)...................................9 2.2.1 Purpose...........................................9 2.2.2 FITS files and headers................................... 10 2.2.3 Starting PQL........................................ 10 2.2.4 Configuration files..................................... 11 2.2.5 PQL’s main window.................................... 12 2.2.5.1 Menu bar...................................... 12 2.2.5.2 Tool bar...................................... 12 2.2.5.3 Main panel..................................... 12 2.2.5.4 Current night checkbox.............................. 14 2.2.5.5 Use GEIRS file.................................. 14 2.2.5.6 Filename filter..................................
    [Show full text]
  • Spring 2006 a Guide to Using Cygwin, DS9, and IRAF
    ASTR 330/FC 30 – Spring 2006 A Guide to using Cygwin, DS9, and IRAF – PART I I. Introduction This document has been designed to introduce you to the basics of logging into cecilia, the MHC astronomy department server, using Cygwin, an X-terminal emulator. Once you have successfully logged in, we will learn some of the basics of the software package IRAF (Image Reduction and Analysis Facility) in preparation for your project involving spectroscopic data reductions. II. Logging Into Cecilia using Cygwin At many of the PCs available in the Library and Dwight Hall at Mount Holyoke College, one will find the X-terminal emulator, Cygwin, located on the C: drive under “My Computer”. Cygwin is a Linux-like envirnment for Windows. A convenient guide for using Cygwin is located at http://cygwin.com/cygwin-ug-net/cygwin-ug-net.html. Procedure 1. Double-click on the computer icon on the desktop. This will open up a folder. 2. Double-click on the Local Disk (C:). 3. Locate and double-click on the folder entitled “cygwin”. 4. Locate and double-click on the file “cygwin.bat”. This will open a terminal window. 5. In this terminal window, at the prompt, type: $ startx and then wait… a) If you see a firewall message, click on “UNBLOCK” to allow communication between cecilia and your PC. b) You will be greeted with 3 screens. To keep your screen relatively free of clutter, you will want to get rid of the 2 screens on the right. Simply type “exit” at the prompts. c) Place the left mouse button in the upper right hand corner of the remaining window.
    [Show full text]
  • IRAF Paso a Paso
    IRAF Paso a Paso Forewords This is a brief, introductory course on IRAF, written by a IRAF user, rather than by an IRAF programmer. I have tried to clarify those points which may present problems to first-time users. The course is focused on the IRAF structure and language, and won't provide any info on how to process and analyze astronomical images and data. I suggest that you try the various examples presented here, experiment freely with similar IRAF tasks, different combinations of parameters, on a variety of image formats and types, etc. These pages might also be used as a quick reference, even if they are not exhaustive or complete at all. This manual is in constant evolution, as existing topics may be revised or new contents added. I will be happy to receive comments, critics and suggestions on how to improve this presentation. Also, if you would like me to add (or expand on) a specific subject, just send me an email: [email protected] This course is found in: http://www.iac.es/sieinvens/SINFIN/CursoIraf/Intro.php Note : This presentation has been formatted using Cascading Style Sheets (CSS). If you are using a non standard-compliant browser, you may notice inconsistencies or errors in the rendering of these pages. Table of Contents 1. Basic Notions 1. A quick ID of IRAF 2. Getting started happily 3. Do I need anything else? 4. Know and customize your IRAF environment 5. Quick look at the IRAF structure 2. Packages and tasks 1. Start: load and use packages 2.
    [Show full text]
  • Astroconda Documentation Release 0.0.1
    AstroConda Documentation Release 0.0.1 Space Telescope Science Institute Apr 23, 2021 Contents 1 System Requirements 3 2 Powered by Conda 5 2.1 Getting Started..............................................6 2.2 Selecting a Software Stack........................................7 2.3 Updating a Software Stack........................................8 2.4 Further Reading............................................. 10 2.5 Pipeline Releases............................................. 12 2.6 Compatibility Notices.......................................... 14 2.7 F.A.Q................................................... 18 2.8 Contributing Guide............................................ 23 2.9 Packages................................................. 30 2.10 Resources................................................. 73 2.11 General Disclaimer............................................ 74 i ii AstroConda Documentation, Release 0.0.1 AstroConda is a free Conda channel maintained by the Space Telescope Science Institute (STScI) in Baltimore, Mary- land. This channel provides tools and utilities required to process and analyze data from the Hubble Space Telescope (HST), James Webb Space Telescope (JWST), and others. Contents 1 AstroConda Documentation, Release 0.0.1 2 Contents CHAPTER 1 System Requirements • 64-bit Intel/AMD processor (x86_64) • 64-bit Linux (glibc 2.12) or Mac OS X ( 10.7) • BASH or ZSH as your default shell environment (T/CSH is NOT supported) 3 AstroConda Documentation, Release 0.0.1 4 Chapter 1. System Requirements CHAPTER
    [Show full text]
  • ASTRONOMICAL DATA PROCESSING Using DS9, Python, and IRAF ASTR 230 - 2021 Spring Semester DUE DATE: TUESDAY, April 13, 7PM
    ASTRONOMICAL DATA PROCESSING using DS9, python, and IRAF ASTR 230 - 2021 Spring Semester DUE DATE: TUESDAY, April 13, 7PM 1 Overview and Purpose. This laboratory is intended to give you a working knowledge of astronomical image processing. Astronomy is at a crossroads, and there are several ways to reduce images. One of the primary packages developed for decades by the National Observatories for imaging and spectral reduction is known as IRAF (Image Reduction and Analysis Facility). IRAF is powerful and there is a package to do just about anything you want. However, its internal programming language has never caught on, its ways of plotting data are rather arcane, and its system administration is not necessarily straightforward. Most importantly, funding to support it has dried up, program developers see it as based on an old framework, and its future is unclear. Nonetheless, it is the language that remains prevalent at many observatories around the world. If you are using a professional telescope, there is a good chance that IRAF is there, and if it is an NOAO facility, it probably is how you assess whether or not your data are any good. You are at a significant disadvantage if you have no facility with its workings. IDL is a powerful analysis package that has enjoyed widespread usage in many areas of astronomy. However, it is not free, not owned by astronomical interests, and its use seems to be slowly waning. The statistics package R is free and is as powerful as IDL, but astronomers are generally unfamiliar with it. MATLAB is similar to R and to IDL, with a more mathematical flavor, and Mathematica is another option along those lines.
    [Show full text]