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 gentle art of shell scripting Mendel Cooper Brindle−Phlogiston Associates [email protected] 29 September 2002 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. 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. 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.......................................................................................................................................................7 Chapter 3. Special Characters...........................................................................................................................8 Chapter 4. Introduction to Variables and Parameters..................................................................................24 4.1. Variable Substitution......................................................................................................................24 4.2. Variable Assignment.......................................................................................................................26 4.3. Bash Variables Are Untyped..........................................................................................................27 4.4. Special Variable Types...................................................................................................................28 Chapter 5. Quoting...........................................................................................................................................33 Chapter 6. Exit and Exit Status.......................................................................................................................39 Chapter 7. Tests................................................................................................................................................41 7.1. Test Constructs...............................................................................................................................41 7.2. File test operators............................................................................................................................47 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.......................................................................................................................78 9.2.1. Manipulating strings using awk............................................................................................82 9.2.2. Further Discussion.................................................................................................................83 9.3. Parameter Substitution....................................................................................................................83 9.4. Typing variables: declare or typeset...............................................................................................91 9.5. Indirect References to Variables.....................................................................................................93 9.6. $RANDOM: generate random integer............................................................................................94 9.7. The Double Parentheses Construct.................................................................................................99 Chapter 10. Loops and Branches..................................................................................................................101 10.1. Loops..........................................................................................................................................101 10.2. Nested Loops..............................................................................................................................111 10.3. Loop Control...............................................................................................................................112 i Advanced Bash−Scripting Guide Table of Contents Chapter 10. Loops and Branches 10.4. Testing and Branching................................................................................................................114 Chapter 11. Internal Commands and Builtins.............................................................................................122 11.1. Job Control Commands..............................................................................................................141 Chapter 12. External Filters, Programs and Commands...........................................................................145 12.1. Basic Commands........................................................................................................................145 12.2. Complex Commands...................................................................................................................148 12.3. Time / Date Commands..............................................................................................................155 12.4. Text Processing Commands........................................................................................................157 12.5. File and Archiving Commands...................................................................................................173 12.6. Communications Commands......................................................................................................187 12.7. Terminal Control Commands.....................................................................................................191 12.8. Math Commands.........................................................................................................................192 12.9. Miscellaneous Commands..........................................................................................................201 Chapter 13. System and Administrative Commands..................................................................................211
Recommended publications
  • Towards Web-Based Delta Synchronization for Cloud Storage Services
    Towards Web-based Delta Synchronization for Cloud Storage Services He Xiao Zhenhua Li ∗ Ennan Zhai Tianyin Xu Tsinghua University Tsinghua University Yale University UIUC Yang Li Yunhao Liu Quanlu Zhang Yao Liu Tsinghua University Tsinghua University Microsoft Research SUNY Binghamton Abstract savings in the presence of users’ file edits [29, 39, 40]. Delta synchronization (sync) is crucial for network-level Unfortunately, today delta sync is only available for efficiency of cloud storage services. Practical delta sync PC clients and mobile apps, but not for the web—the most pervasive and OS-independent access method [37]. techniques are, however, only available for PC clients 0 and mobile apps, but not web browsers—the most per- After a file f is edited into a new version f by users, vasive and OS-independent access method. To under- Dropbox’s PC client will apply delta sync to automati- stand the obstacles of web-based delta sync, we imple- cally upload only the altered bits to the cloud; in contrast, Dropbox’s web interface requires users to manually up- ment a delta sync solution, WebRsync, using state-of- 0 1 the-art web techniques based on rsync, the de facto delta load the entire content of f to the cloud. This gap sig- sync protocol for PC clients. Our measurements show nificantly affects web-based user experiences in terms of that WebRsync severely suffers from the inefficiency of both sync speed and traffic cost. JavaScript execution inside web browsers, thus leading Web is a fairly popular access method for cloud stor- to frequent stagnation and even hanging.
    [Show full text]
  • The Linux Kernel Module Programming Guide
    The Linux Kernel Module Programming Guide Peter Jay Salzman Michael Burian Ori Pomerantz Copyright © 2001 Peter Jay Salzman 2007−05−18 ver 2.6.4 The Linux Kernel Module Programming Guide is a free book; you may reproduce and/or modify it under the terms of the Open Software License, version 1.1. You can obtain a copy of this license at http://opensource.org/licenses/osl.php. This book is distributed in the hope it will be useful, but without any warranty, without even the implied warranty of merchantability or fitness for a particular purpose. The author encourages wide distribution of this book for personal or commercial use, provided the above copyright notice remains intact and the method adheres to the provisions of the Open Software License. In summary, you may copy and distribute this book free of charge or for a profit. No explicit permission is required from the author for reproduction of this book in any medium, physical or electronic. Derivative works and translations of this document must be placed under the Open Software License, and the original copyright notice must remain intact. If you have contributed new material to this book, you must make the material and source code available for your revisions. Please make revisions and updates available directly to the document maintainer, Peter Jay Salzman <[email protected]>. This will allow for the merging of updates and provide consistent revisions to the Linux community. If you publish or distribute this book commercially, donations, royalties, and/or printed copies are greatly appreciated by the author and the Linux Documentation Project (LDP).
    [Show full text]
  • Linux on the Road
    Linux on the Road Linux with Laptops, Notebooks, PDAs, Mobile Phones and Other Portable Devices Werner Heuser <wehe[AT]tuxmobil.org> Linux Mobile Edition Edition Version 3.22 TuxMobil Berlin Copyright © 2000-2011 Werner Heuser 2011-12-12 Revision History Revision 3.22 2011-12-12 Revised by: wh The address of the opensuse-mobile mailing list has been added, a section power management for graphics cards has been added, a short description of Intel's LinuxPowerTop project has been added, all references to Suspend2 have been changed to TuxOnIce, links to OpenSync and Funambol syncronization packages have been added, some notes about SSDs have been added, many URLs have been checked and some minor improvements have been made. Revision 3.21 2005-11-14 Revised by: wh Some more typos have been fixed. Revision 3.20 2005-11-14 Revised by: wh Some typos have been fixed. Revision 3.19 2005-11-14 Revised by: wh A link to keytouch has been added, minor changes have been made. Revision 3.18 2005-10-10 Revised by: wh Some URLs have been updated, spelling has been corrected, minor changes have been made. Revision 3.17.1 2005-09-28 Revised by: sh A technical and a language review have been performed by Sebastian Henschel. Numerous bugs have been fixed and many URLs have been updated. Revision 3.17 2005-08-28 Revised by: wh Some more tools added to external monitor/projector section, link to Zaurus Development with Damn Small Linux added to cross-compile section, some additions about acoustic management for hard disks added, references to X.org added to X11 sections, link to laptop-mode-tools added, some URLs updated, spelling cleaned, minor changes.
    [Show full text]
  • Beyond Compare User Guide
    Copyright © 2012 Scooter Software, Inc. Beyond Compare Copyright © 2012 Scooter Software, Inc. All rights reserved. No parts of this work may be reproduced in any form or by any means - graphic, electronic, or mechanical, including photocopying, recording, taping, or information storage and retrieval systems - without the written permission of the publisher. Products that are referred to in this document may be either trademarks and/or registered trademarks of the respective owners. The publisher and the author make no claim to these trademarks. While every precaution has been taken in the preparation of this document, the publisher and the author assume no responsibility for errors or omissions, or for damages resulting from the use of information contained in this document or from the use of programs and source code that may accompany it. In no event shall the publisher and the author be liable for any loss of profit or any other commercial damage caused or alleged to have been caused directly or indirectly by this document. Published: July 2012 Contents 3 Table of Contents Part 1 Welcome 7 1 What's. .N..e..w............................................................................................................................. 8 2 Standa..r.d.. .v..s. .P..r..o..................................................................................................................... 9 Part 2 Using Beyond Compare 11 1 Home. .V...i.e..w..........................................................................................................................
    [Show full text]
  • Software to Compare Source Code
    Software to compare source code WinMerge is an Open Source differencing and merging tool for Windows. WinMerge can compare both folders and files, presenting differences in a visual text format that WinMerge is an open source project, which means that the program is ​Download WinMerge · ​Screenshots · ​About · ​Documentation. Diff Checker is an online diff tool to compare text to find the difference between two text files.​PDF diff · ​Images · ​Sign up · ​Login. This is one of the reasons why many software developers use this source code compare tool. If you need a standalone of CodeCompare you. ExamDiff is a freeware Windows tool to visually compare text files. Version Control System that allows joint software development remotely over the Internet. Download Notepad++ Compare plugin for free. map of compared files; Moved line detection; Easy navigation between . Other Useful Business Software . Get latest updates about Open Source Projects, Conferences and. Devart Software. |. clicks. | () Code Compare is a powerful tool for file and folder comparison and merging. Code Compare is an Moved blocks of code are detected if source code is reordered in files. Difference. Buggy source codes will be a nagging problem throughout a developer's career In looking for software tools to use for code comparisons, you'll run into a lot of. Fast downloads of the latest free software! Code Compare can be integrated with all popular source control systems: TFS, SVN, Git, Mercurial. This article compares computer software tools that compare files, and in many cases directories .. WinDiff, No, No, No, No. WinMerge, Yes, No, Mercurial, Subversion, Visual Source Safe, Rational ClearCase, Yes, Yes.
    [Show full text]
  • Linux Fundamentals (GL120) U8583S This Is a Challenging Course That Focuses on the Fundamental Tools and Concepts of Linux and Unix
    Course data sheet Linux Fundamentals (GL120) U8583S This is a challenging course that focuses on the fundamental tools and concepts of Linux and Unix. Students gain HPE course number U8583S proficiency using the command line. Beginners develop a Course length 5 days solid foundation in Unix, while advanced users discover Delivery mode ILT, vILT patterns and fill in gaps in their knowledge. The course View schedule, local material is designed to provide extensive hands-on View now pricing, and register experience. Topics include basic file manipulation; basic and View related courses View now advanced filesystem features; I/O redirection and pipes; text manipulation and regular expressions; managing jobs and processes; vi, the standard Unix editor; automating tasks with Why HPE Education Services? shell scripts; managing software; secure remote • IDC MarketScape leader 5 years running for IT education and training* administration; and more. • Recognized by IDC for leading with global coverage, unmatched technical Prerequisites Supported distributions expertise, and targeted education consulting services* Students should be comfortable with • Red Hat Enterprise Linux 7 • Key partnerships with industry leaders computers. No familiarity with Linux or other OpenStack®, VMware®, Linux®, Microsoft®, • SUSE Linux Enterprise 12 ITIL, PMI, CSA, and SUSE Unix operating systems is required. • Complete continuum of training delivery • Ubuntu 16.04 LTS options—self-paced eLearning, custom education consulting, traditional classroom, video on-demand
    [Show full text]
  • Using Text Animated Transitions to Support Navigation in Document Histories
    Using Text Animated Transitions to Support Navigation in Document Histories Fanny Chevalier Pierre Dragicevic Anastasia Bezerianos Jean-Daniel Fekete Microsoft-INRIA INRIA Ecole´ Centrale Paris INRIA [email protected] [email protected] [email protected] [email protected] Figure 1. Detail of an animated transition between two revisions of the Wikipedia article User interfaces. ABSTRACT others [20]. Supporting change awareness is not only es- This article examines the benefits of using text animated sential for writing articles, but also for programming code transitions for navigating in the revision history of textual where changes can profoundly impact the quality of a pro- documents. We propose an animation technique for smoothly gram. Millions of people, such as programmers, researchers transitioning between different text revisions, then present or Wikipedia contributors, now rely on revision control sys- the Diffamation system. Diffamation supports rapid explo- tems [22] to perform their computer-based daily activity. ration of revision histories by combining text animated tran- sitions with simple navigation and visualization tools. We While the problem of storing and retrieving revisions has finally describe a user study showing that smooth text anima- been mostly solved from the technical standpoint [22], cur- tion allows users to track changes in the evolution of textual rent revision management systems only support two tasks: documents more effectively than flipping pages. retrieving a specified revision (by number, date, name etc.) and comparing two revisions by computing “diffs”. But with ACM Classification Keywords the popularity of sites where anyone can edit articles, users H.5.1 Multimedia Information Systems: [animations]; H.5.2 are often interested in more complex tasks, such as under- User Interfaces: [Graphical user interfaces (GUI)] standing the edition history of an article or keeping track of General Terms specific portions of an article they are contributing to.
    [Show full text]
  • Research Computing and Cyberinfrastructure Team
    Research Computing and CyberInfrastructure team ! Working with the Linux Shell on the CWRU HPC 31 January 2019 7 February 2019 ! Presenter Emily Dragowsky KSL Data Center RCCI Team: Roger Bielefeld, Mike Warfe, Hadrian Djohari! Brian Christian, Emily Dragowsky, Jeremy Fondran, Cal Frye,! Sanjaya Gajurel, Matt Garvey, Theresa Griegger, Cindy Martin, ! Sean Maxwell, Jeno Mozes, Nasir Yilmaz, Lee Zickel Preface: Prepare your environment • User account ! # .bashrc ## cli essentials ! if [ -t 1 ] then bind '"\e[A": history-search-backward' bind '"\e[B": history-search-forward' bind '"\eOA": history-search-backward' bind '"\eOB": history-search-forward' fi ! This is too useful to pass up! Working with Linux • Preamble • Intro Session Linux Review: Finding our way • Files & Directories: Sample work flow • Shell Commands • Pipes & Redirection • Scripting Foundations • Shell & environment variables • Control Structures • Regular expressions & text manipulation • Recap & Look Ahead Rider Cluster Components ! rider.case.edu ondemand.case.edu University Firewall ! Admin Head Nodes SLURM Science Nodes Master DMZ Resource ! Data Manager Transfer Disk Storage Nodes Batch nodes GPU nodes SMP nodes Running a job: where is it? slide from Hadrian Djohari Storing Data on the HPC table from Nasir Yilmaz How data moves across campus • Buildings on campus are each assigned to a zone. Data connections go from every building to the distribution switch at the center of the zone and from there to the data centers at Kelvin Smith Library and Crawford Hall. slide
    [Show full text]
  • Openbsd Gaming Resource
    OPENBSD GAMING RESOURCE A continually updated resource for playing video games on OpenBSD. Mr. Satterly Updated August 7, 2021 P11U17A3B8 III Title: OpenBSD Gaming Resource Author: Mr. Satterly Publisher: Mr. Satterly Date: Updated August 7, 2021 Copyright: Creative Commons Zero 1.0 Universal Email: [email protected] Website: https://MrSatterly.com/ Contents 1 Introduction1 2 Ways to play the games2 2.1 Base system........................ 2 2.2 Ports/Editors........................ 3 2.3 Ports/Emulators...................... 3 Arcade emulation..................... 4 Computer emulation................... 4 Game console emulation................. 4 Operating system emulation .............. 7 2.4 Ports/Games........................ 8 Game engines....................... 8 Interactive fiction..................... 9 2.5 Ports/Math......................... 10 2.6 Ports/Net.......................... 10 2.7 Ports/Shells ........................ 12 2.8 Ports/WWW ........................ 12 3 Notable games 14 3.1 Free games ........................ 14 A-I.............................. 14 J-R.............................. 22 S-Z.............................. 26 3.2 Non-free games...................... 31 4 Getting the games 33 4.1 Games............................ 33 5 Former ways to play games 37 6 What next? 38 Appendices 39 A Clones, models, and variants 39 Index 51 IV 1 Introduction I use this document to help organize my thoughts, files, and links on how to play games on OpenBSD. It helps me to remember what I have gone through while finding new games. The biggest reason to read or at least skim this document is because how can you search for something you do not know exists? I will show you ways to play games, what free and non-free games are available, and give links to help you get started on downloading them.
    [Show full text]
  • Bash Guide for Beginners
    Bash Guide for Beginners Machtelt Garrels Garrels BVBA <tille wants no spam _at_ garrels dot be> Version 1.11 Last updated 20081227 Edition Bash Guide for Beginners Table of Contents Introduction.........................................................................................................................................................1 1. Why this guide?...................................................................................................................................1 2. Who should read this book?.................................................................................................................1 3. New versions, translations and availability.........................................................................................2 4. Revision History..................................................................................................................................2 5. Contributions.......................................................................................................................................3 6. Feedback..............................................................................................................................................3 7. Copyright information.........................................................................................................................3 8. What do you need?...............................................................................................................................4 9. Conventions used in this
    [Show full text]
  • Linux Cheat Sheet
    1 of 4 ########################################### # 1.1. File Commands. # Name: Bash CheatSheet # # # # A little overlook of the Bash basics # ls # lists your files # # ls -l # lists your files in 'long format' # Usage: A Helpful Guide # ls -a # lists all files, including hidden files # # ln -s <filename> <link> # creates symbolic link to file # Author: J. Le Coupanec # touch <filename> # creates or updates your file # Date: 2014/11/04 # cat > <filename> # places standard input into file # Edited: 2015/8/18 – Michael Stobb # more <filename> # shows the first part of a file (q to quit) ########################################### head <filename> # outputs the first 10 lines of file tail <filename> # outputs the last 10 lines of file (-f too) # 0. Shortcuts. emacs <filename> # lets you create and edit a file mv <filename1> <filename2> # moves a file cp <filename1> <filename2> # copies a file CTRL+A # move to beginning of line rm <filename> # removes a file CTRL+B # moves backward one character diff <filename1> <filename2> # compares files, and shows where differ CTRL+C # halts the current command wc <filename> # tells you how many lines, words there are CTRL+D # deletes one character backward or logs out of current session chmod -options <filename> # lets you change the permissions on files CTRL+E # moves to end of line gzip <filename> # compresses files CTRL+F # moves forward one character gunzip <filename> # uncompresses files compressed by gzip CTRL+G # aborts the current editing command and ring the terminal bell gzcat <filename> #
    [Show full text]
  • A Zahlensysteme
    A Zahlensysteme Außer dem Dezimalsystem sind das Dual-,dasOktal- und das Hexadezimalsystem gebräuchlich. Ferner spielt das Binär codierte Dezimalsystem (BCD) bei manchen Anwendungen eine Rolle. Bei diesem sind die einzelnen Dezimalstellen für sich dual dargestellt. Die folgende Tabelle enthält die Werte von 0 bis dezimal 255. Be- quemlichkeitshalber sind auch die zugeordneten ASCII-Zeichen aufgeführt. dezimal dual oktal hex BCD ASCII 0 0 0 0 0 nul 11111soh 2102210stx 3113311etx 4 100 4 4 100 eot 5 101 5 5 101 enq 6 110 6 6 110 ack 7 111 7 7 111 bel 8 1000 10 8 1000 bs 9 1001 11 9 1001 ht 10 1010 12 a 1.0 lf 11 101 13 b 1.1 vt 12 1100 14 c 1.10 ff 13 1101 15 d 1.11 cr 14 1110 16 e 1.100 so 15 1111 17 f 1.101 si 16 10000 20 10 1.110 dle 17 10001 21 11 1.111 dc1 18 10010 22 12 1.1000 dc2 19 10011 23 13 1.1001 dc3 20 10100 24 14 10.0 dc4 21 10101 25 15 10.1 nak 22 10110 26 16 10.10 syn 430 A Zahlensysteme 23 10111 27 17 10.11 etb 24 11000 30 18 10.100 can 25 11001 31 19 10.101 em 26 11010 32 1a 10.110 sub 27 11011 33 1b 10.111 esc 28 11100 34 1c 10.1000 fs 29 11101 35 1d 10.1001 gs 30 11110 36 1e 11.0 rs 31 11111 37 1f 11.1 us 32 100000 40 20 11.10 space 33 100001 41 21 11.11 ! 34 100010 42 22 11.100 ” 35 100011 43 23 11.101 # 36 100100 44 24 11.110 $ 37 100101 45 25 11.111 % 38 100110 46 26 11.1000 & 39 100111 47 27 11.1001 ’ 40 101000 50 28 100.0 ( 41 101001 51 29 100.1 ) 42 101010 52 2a 100.10 * 43 101011 53 2b 100.11 + 44 101100 54 2c 100.100 , 45 101101 55 2d 100.101 - 46 101110 56 2e 100.110 .
    [Show full text]