Linux Shell Scripting –Intro/Review

Total Page:16

File Type:pdf, Size:1020Kb

Linux Shell Scripting –Intro/Review Linux shell scripting –intro/review David Morgan © David Morgan 2012-17 You should already know how to log in run programs at the command line use pipelines and redirection ( | < > ) put jobs in the background ( & ) create and edit files make scripts executable ( chmod ) © David Morgan 2012-17 1 UNIX software tools do one thing well process lines of text, not binary use regular expressions default to standard I/O aren’t chatty give same output format as got from input let someone else do the hard part © David Morgan 2012-17 let someone do the hard part -- subcontractors “The shell’s dependence on other programs to do most of the work is arguably a defect, but also inarguably a strength: you get the concise notation of a scripting language plus the speed and efficiency of programs written in C (etc.) --Robbins and Beebe p. ix specialized subcontractor examples – sed – for dynamic editing – find – for selective, arbitrary file processing – bc – for arbitrary precision math they lack the shell’s programming constructs the shell lacks their specialized capabilities strategy: employ them in mutual alliance © David Morgan 2012-17 2 Command shell definition a shell is just another program, like cal or ls defined by what it does – cal: prints a calendar – ls: prints a file list – shell: prompts for commands, tries to run them running commands for users is what, precisely, makes a shell a shell (and not an editor, browser, or calculator) © David Morgan 2012-17 What features does a shell have? Primary! Essential! Must have ability to cause a command to run … distant second Nice to have whatever other features were coded into it by that shell’s author. If any. I don’t know. © David Morgan 2012-17 3 Scripting in context Brief feature summary for bash: Command processing – parse – expand – execute Scripting: a possible feature that a shell may implement I/O redirection bash, for example, does (but don’t take it for granted.) Piping Environment control Background processing Shell scripts © David Morgan 2012-17 dshell – a feature -poor shell causes commands to run (so, it’s a shell!) nothing else (certainly not scripting) © David Morgan 2012-17 4 bash – a feature -rich shell causes commands to run (so, it’s a shell!) but has a zillion times more code doing tons of useful extra things (all optional) © David Morgan 2012-17 Some other, tutorial shells dshell – my dirt-simple shell simpleshell – http://teaching.idallen.org csimpleshell – http://rik0.altervista.org/snippets/csimpleshell.html mini-shell – http://code.google.com/p/mini-shell © David Morgan 2012-17 5 Bash source code © David Morgan 2012-17 Prominent “production ” shells Korn shell from AT&T C shell Berkeley Bourne again shell GNU Z shell Princeton student Almquist shell Ken Almquist © David Morgan 2012-17 6 Shells installed on our systems ksh tcsh bash zsh mksh dash yash © David Morgan 2012-17 ksh KSH-93 is the most recent version of the KornShell by David Korn of AT&T Bell Laboratories. KornShell is a shell programming language, which is upward compatible with "sh" (the Bourne Shell). tcsh Tcsh is an enhanced but completely compatible version of csh, the C shell. Tcsh is a command language interpreter which can be used both as an interactive login shell and as a shell script command processor. Tcsh includes a command line editor, programmable word completion, spelling correction, a history mechanism, job control and a C language like syntax. © David Morgan 2012-17 7 bash The GNU Bourne Again shell (Bash) is a shell or command language interpreter that is compatible with the Bourne shell (sh). Bash incorporates useful features from the Korn shell (ksh) and the C shell (csh). Most sh scripts can be run by bash without modification. zsh The zsh shell is a command interpreter usable as an interactive login shell and as a shell script command processor. Zsh resembles the ksh shell (the Korn shell), but includes many enhancements. Zsh supports command line editing, built-in spelling correction, programmable command completion, shell functions (with autoloading), a history mechanism, and more. © David Morgan 2012-17 mksh mksh is the MirBSD enhanced version of the Public Domain Korn shell (pdksh), a bourne-compatible shell which is largely similar to the original AT&T Korn shell. It includes bug fixes and feature improvements in order to produce a modern, robust shell good for interactive and especially script use, being a bourne shell replacement, pdksh successor and an alternative to the C shell. dash DASH is a POSIX-compliant implementation of /bin/sh that aims to be as small as possible. It does this without sacrificing speed where possible. In fact, it is significantly faster than bash (the GNU Bourne-Again SHell) for most tasks. © David Morgan 2012-17 8 yash Yash is a command line shell that conforms to the POSIX.1 (IEEE Std 1003.1, 2008 Edition) standard for the most part. Yash also has its own features beyond POSIX, such as: * global aliases * random numbers * socket redirections and other special redirections * right prompt * command completion © David Morgan 2012-17 Bash conformant -izability --posix or -o posix +B get rid of brace expansion calling bash as /bin/sh softlink © David Morgan 2012-17 9 Which shell to script for? #!/path/to/shell © David Morgan 2012-17 10.
Recommended publications
  • Beginning Portable Shell Scripting from Novice to Professional
    Beginning Portable Shell Scripting From Novice to Professional Peter Seebach 10436fmfinal 1 10/23/08 10:40:24 PM Beginning Portable Shell Scripting: From Novice to Professional Copyright © 2008 by Peter Seebach All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-1043-6 ISBN-10 (pbk): 1-4302-1043-5 ISBN-13 (electronic): 978-1-4302-1044-3 ISBN-10 (electronic): 1-4302-1044-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Frank Pohlmann Technical Reviewer: Gary V. Vaughan Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Richard Dal Porto Copy Editor: Kim Benbow Associate Production Director: Kari Brooks-Copony Production Editor: Katie Stence Compositor: Linda Weidemann, Wolf Creek Press Proofreader: Dan Shaw Indexer: Broccoli Information Management Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013.
    [Show full text]
  • Introduction to Unix
    Introduction to Unix Rob Funk <[email protected]> University Technology Services Workstation Support http://wks.uts.ohio-state.edu/ University Technology Services Course Objectives • basic background in Unix structure • knowledge of getting started • directory navigation and control • file maintenance and display commands • shells • Unix features • text processing University Technology Services Course Objectives Useful commands • working with files • system resources • printing • vi editor University Technology Services In the Introduction to UNIX document 3 • shell programming • Unix command summary tables • short Unix bibliography (also see web site) We will not, however, be covering these topics in the lecture. Numbers on slides indicate page number in book. University Technology Services History of Unix 7–8 1960s multics project (MIT, GE, AT&T) 1970s AT&T Bell Labs 1970s/80s UC Berkeley 1980s DOS imitated many Unix ideas Commercial Unix fragmentation GNU Project 1990s Linux now Unix is widespread and available from many sources, both free and commercial University Technology Services Unix Systems 7–8 SunOS/Solaris Sun Microsystems Digital Unix (Tru64) Digital/Compaq HP-UX Hewlett Packard Irix SGI UNICOS Cray NetBSD, FreeBSD UC Berkeley / the Net Linux Linus Torvalds / the Net University Technology Services Unix Philosophy • Multiuser / Multitasking • Toolbox approach • Flexibility / Freedom • Conciseness • Everything is a file • File system has places, processes have life • Designed by programmers for programmers University Technology Services
    [Show full text]
  • Ardpower Documentation Release V1.2.0
    ardPower Documentation Release v1.2.0 Anirban Roy Das Sep 27, 2017 Contents 1 Introduction 1 2 Screenshot 3 3 Documentaion 5 3.1 Overview.................................................5 3.2 Installation................................................7 3.3 Usage................................................... 12 4 Indices and tables 13 i ii CHAPTER 1 Introduction Its a custom Oh-My-Zsh Theme inspired by many custom themes suited for a perfect ZSH Environment under Byobu with Tmux Backend. 1 ardPower Documentation, Release v1.2.0 2 Chapter 1. Introduction 3 ardPower Documentation, Release v1.2.0 CHAPTER 2 Screenshot 4 Chapter 2. Screenshot CHAPTER 3 Documentaion You can also find PDF version of the documentation here. Overview We will start with understanding the individual components of an entire CLI. Generally we don’t put much attention to what we do. We just fire up a terminal or some say sheel and start typing our commands and get the result. That’s all. But there are a lot of things that goes behind all this. Terminal The very first component is the Terminal. So what is a Terminal? A terminal emulator, terminal application, term, or tty for short, is a program that emulates a video ter- minal within some other display architecture. Though typically synonymous with a shell or text terminal, the term terminal covers all remote terminals, including graphical interfaces. A terminal emulator inside a graphical user interface is often called a terminal window.A terminal window allows the user access to a text terminal and all its applications such as command-line interfaces (CLI) and text user interface (TUI) applications.
    [Show full text]
  • Unix Shell Programming – by Dinesh Kumar S
    1 Unix Shell Programming – by Dinesh Kumar S PDF by http://www.k2pdf.com Contents Chapters Topic Page no. Chapter 1 Introduction 3 Chapter 2 SSH Client 4 Chapter 3 Unix Shells 8 Chapter 4 Text Editors 11 Chapter 5 A Beginning to Shell Scripting 19 Chapter 6 Operators 33 Chapter 7 Variables Manipulation (Advance) 39 Chapter 8 Conditional Statements 43 Chapter 9 Looping Statements 47 Chapter 10 Control Statements 74 Chapter 11 Functions 79 2 Unix Shell Programming – by Dinesh Kumar S Chapter 1 Introduction Linux : It is an operating system based on UNIX . Kernel : It is the backbone of Linux OS, which is used to manage resources of Linux OS like memory, I/O, software, hardware management processes. User Shell Script Kernel PC h/w User writes script. Script contains instructions. Kernel interprets the instruction in machine language. As per the instruction kernel controls the PC hardware. Shell script : It’s a collection of OS commands or instructions. Advantages of Shell Script : Script is always a platform independent. Performance will be faster than programming languages. Very easy to debug. 3 Unix Shell Programming – by Dinesh Kumar S Chapter 2 SSH Client Secure Shell (or) SSH is a network protocol that is used to exchange or share information between two different networks. This is used on Linux & UNIX systems to access SHELL accounts. All the information exchanged/transmitted between networks is encrypted . It uses public key cryptography to authenticate remote computer user. Free Serial, Telnet, and SSH client Putty Tera Term Putty : It is a terminal emulator application which acts as client for SSH, Telnet, rLogin.
    [Show full text]
  • Introduction to Unix
    Introduction to Unix BorisBoris KonevKonev [email protected] http://www.csc.liv.ac.uk/~konev Important information Instructor: – Boris Konev – Office: 1.12 – Email: [email protected] – Web resources: www.csc.liv.ac.uk/~konev/COMP110 NoNo lectureslectures onon Monday!!Monday!! Objectives • Basic structure of the operating system • What makes Unix special? • Unix System Design • Essential commands • Essential concepts • Shells • Shell programming • Text processing • Unix utilities Why Bother Studying Unix? • Most powerful computers (supercomputers) use the Unix operating system • Unix runs the Internet • Unix is used as an embedded system in hardware – networking, user appliances (DVD players) • A knowledge of Unix is likely to be helpful in your future career, regardless of where you pursue it. History of Unix 1960s “multics” project (MIT, GE, AT&T) 1970s AT&T Bell Labs 1970s/80s UC Berkeley 1980s DOS imitated many Unix ideas Commercial Unix fragmentation GNU Project 1990s Linux now Unix is widespread and available from many sources, both free and commercial Unix Systems • Commercial – SunOS/Solaris Sun Microsystems – Digital Unix (Tru64) Digital/Compaq – HP-UX Hewlett Packard – Irix SGI – UNICOS Cray – Mac OS X Apple – … • Free – NetBSD, FreeBSD, … – Linux – … What makes Unix so special? (1) • Portability (runs on almost every hardware) • Old and popular – it is easy to find information and get help • Simplicity, elegance & flexibility • Time-sharing: multi-tasking & multi-user environment • Unix is very stable – computers running
    [Show full text]
  • Augmented Unix Userland
    Project MXC-403 Augmented Unix Userland Major Qualifying Project Submitted to the Faculty of Worcester Polytechnic Institute in partial fulfillment of the requirements for the Degree in Bachelor of Science in Computer Science By Sam Abradi [email protected] Ian Naval [email protected] Fredric Silberberg [email protected] Submitted On: April 30, 2015 Project Advisor: Professor Michael Ciaraldi [email protected] This report represents work of WPI undergraduate students submitted to the faculty as evidence of a degree requirement. WPI routinely publishes these reports on its web site without editorial or peer review. For more information about the projects program at WPI, see http: // www. wpi. edu/ Academics/ Projects . Abstract The Unix philosophy has resulted in stringing together simple general purpose utili- ties to accomplish complex tasks. However, using text as a communication medium between these programs requires several formatting utilities to transform output from one program into valid input to another. These transformations can be very complicated and confusing. This project looked at two alternative shell designs that use different communication methods between programs, simplifying interprogram communication and leveraging existing technologies to make developing new utilities and shell scripts easier. i Contents Abstracti List of Figuresv List of Tables vi 1 Introduction1 1.1 AUU Terminology.............................1 1.2 Motivation.................................1 1.3 The AUU Project.............................3 2 JSON Shell4 2.1 Motivation: Why JSON?.........................4 2.2 JSON Protocol Design..........................4 Streams..................................4 Standards and Definitions........................5 Example Use Case: ps..........................6 2.3 Tributary.................................8 Tributary Selectors............................8 Using Tributary to Standardize Output.................9 2.4 Utilities Implemented..........................
    [Show full text]
  • La Kornshell: Lenguaje De Programación Y Comando
    La KornShell: Lenguaje de Programación y Comando Una guía para programadores de Shell Scripts de Unix Jesús Alberto Vidal Cortés http://inicia.es/de/chube [email protected] www.kornshell.com Madrid, Agosto de 2001 Índice PARTE I: INTRODUCCIÓN......................................................................................................... 11 1. SOBRE EL LENGUAJE KORNSHELL ........................................................................................... 12 ¿Qué es una Shell?.............................................................................................................. 12 Beneficios del uso de ksh..................................................................................................... 13 Mejoras como Lenguaje de Comandos ................................................................................................................ 13 Mejoras como Lenguaje de Programación........................................................................................................... 14 Versiones a las que se aplica este manual............................................................................. 15 ¿Cómo obtener ksh?............................................................................................................ 16 2. NOTACIÓN UTILIZADA EN ESTE MANUAL..................................................................................... 18 General ............................................................................................................................... 18 Teclas sobre
    [Show full text]
  • Squeezing Water from Stone: Kornshell in 2019
    Squeezing Water from Stone: KornShell in 2019 Siteshwar Vashisht @SiteshwarV main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);} ● Winner of IOCCC (The International Obfuscated C Code Contest) one line entry in 1987. ● Termed as best one liner ever received. ● Written by David Korn. What this talk is about ? ● It’s about AT&T KornShell (ksh, not mksh or pdksh). ● Brief status update from new upstream maintainers (Kurtis Rader and Siteshwar Vashisht). ● No deep dive in the source code. ● It is slightly extended version of my talk from FOSDEM 2019 and All Systems Go! 2019. /whois situ ● Siteshwar Vashisht. ● Based in Brno, Czech Republic. ● Maintainer of bash and ksh at Red Hat. ● Previously involved with fish shell and Sailfish OS. ● Current upstream maintainer of ksh. What is KornShell ? ● Direct descendant of Bourne shell. ● Initial development based on original bourne shell. ● Roots back to late 70’s and early 80’s. Spot the bug! #!/bin/bash echo $'hello world' | while read foo do bar="$foo" done echo "bar: " "$bar" $bar is not available outside loop. Bash forks last command in a new process and $bar is lost. Advantages ● Superior language specification (Support for Discipline functions, namespaces, compound data type, enums etc.). ● Better POSIX compliance. ● Fastest POSIX shell. Show me the numbers! $ cat test.sh for ((i=0;i<10000;i++)) do foo=$(echo "bar baz") done $ time bash test.sh $ time zsh test.sh $ time ksh test.sh real 0m2.977s real 0m2.433s real 0m0.078s user 0m2.032s user 0m1.570s user 0m0.062s sys 0m1.167s sys 0m1.233s sys 0m0.015s A 20 year old bug State of ksh in 2017 ● Previous developers have left.
    [Show full text]
  • Common Desktop Environment: Desktop Kornshell User's Guide
    Common Desktop Environment: Desktop KornShell User’s Guide Sun Microsystems, Inc. 901 San Antonio Road Palo Alto, CA 94303 U.S.A. Part No: 805-3917–10 October 1998 Copyright 1998 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California 94303-4900 U.S.A. All rights reserved. This product or document is protected by copyright and distributed under licenses restricting its use, copying, distribution, and decompilation. No part of this product or document may be reproduced in any form by any means without prior written authorization of Sun and its licensors, if any. Third-party software, including font technology, is copyrighted and licensed from Sun suppliers. Parts of the product may be derived from Berkeley BSD systems, licensed from the University of California. UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open Company, Ltd. Sun, Sun Microsystems, the Sun logo, SunDocs, Java, the Java Coffee Cup logo, and Solaris are trademarks, registered trademarks, or service marks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the U.S. and other countries. Products bearing SPARC trademarks are based upon an architecture developed by Sun Microsystems, Inc. The OPEN LOOK and SunTM Graphical User Interface was developed by Sun Microsystems, Inc. for its users and licensees. Sun acknowledges the pioneering efforts of Xerox in researching and developing the concept of visual or graphical user interfaces for the computer industry. Sun holds a non-exclusive license from Xerox to the Xerox Graphical User Interface, which license also covers Sun’s licensees who implement OPEN LOOK GUIs and otherwise comply with Sun’s written license agreements.
    [Show full text]
  • Squeezing Water from Stone: Kornshell in 2019
    Squeezing Water from Stone: KornShell in 2019 Siteshwar Vashisht @SiteshwarV main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);} ● Winner of IOCCC (The International Obfuscated C Code Contest) one line entry in 1987. ● Termed as best one liner ever received. ● Written by David Korn. What this talk is about ? ● It’s about AT&T KornShell (ksh, not mksh or pdksh). ● Brief status update from new upstream maintainers (Kurtis Rader and Siteshwar Vashisht). ● No deep dive in the source code. ● It is slightly extended version of my talk from FOSDEM 2019. /whois situ ● Siteshwar Vashisht. ● Based in Brno, Czech Republic. ● Maintainer of bash and ksh at Red Hat. ● Previously involved with fish shell and Sailfish OS. ● Current upstream maintainer of ksh. What is KornShell ? ● Direct descendant of Bourne shell. ● Initial development based on original bourne shell. ● Roots back to late 70’s and early 80’s. Spot the bug! #!/bin/bash echo $'hello world' | while read foo do bar="$foo" done echo "bar: " "$bar" $bar is not available outside loop. Bash forks last command in a new process and $bar is lost. Advantages ● Superior language specification (Support for Discipline functions, namespaces, compound data type, enums etc.). ● Better POSIX compliance. ● Fastest POSIX shell. Show me the numbers! $ cat test.sh for ((i=0;i<10000;i++)) do foo=$(echo "bar baz") done $ time bash test.sh $ time zsh test.sh $ time ksh test.sh real 0m2.977s real 0m2.433s real 0m0.078s user 0m2.032s user 0m1.570s user 0m0.062s sys 0m1.167s sys 0m1.233s sys 0m0.015s A 20 year old bug State of ksh in 2017 ● Previous developers have left.
    [Show full text]
  • Squeezing Water from Stone: Kornshell in 2019
    Squeezing Water from Stone: KornShell in 2019 main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);} ●Winner of IOCCC (The International Obfuscated C Code Contest) one line entry in 1987. ●Termed as best one liner ever received. ●Written by David Korn. What this talk is about ? ●It’s about AT&T KornShell (and not mksh/pdksh). ●Brief status update from new upstream maintainers (Kurtis Rader and Siteshwar Vashisht). ●No deep dive in the source code. /whois situ ●Siteshwar Vashisht. ●Based in Brno, Czech Republic. ●Maintainer of bash and ksh at Red Hat. ●Previously involved with fish shell and Sailfish OS. ●Current upstream maintainer of ksh. What is KornShell ? ●Direct descendant of Bourne shell. ●Initial development based on original bourne shell. ●Roots back to late 70s/early 80s. Advantages ●Superior language specification (Support for Discipline functions, namespaces, compound data type, enums etc.). ●Better POSIX compliance. ●Fastest POSIX shell. A 20 year old bug State of ksh in 2017 ●Previous developers have left. ●Codebase has origins going back to the 80s. ●Old Tooling. ●No revision control history. ●Almost no comments in the source code. ●Spaghetti code. ●Bad test coverage. ●Lots of old bugs. ●And ... How to maintain the unmaintainable ? One of my most productive days was throwing away 1,000 lines of code - Ken Thompson We threw away more than 500,000 lines of code. Lines of code (ksh93v-) Lines of code (current) What did we drop ? ●Support for non-POSIX operating systems. ●Reimplementation of POSIX functions. ●Entire subsystems like the AST vmalloc and locale. ●Lots of code that was not used by ksh.
    [Show full text]
  • A User's Guide to the Z-Shell
    A User’s Guide to the Z-Shell Peter Stephenson 2003/03/23 2 Contents 1 A short introduction 11 1.1 Other shells and other guides . 12 1.2 Versions of zsh . 13 1.3 Conventions . 14 1.4 Acknowledgments . 15 2 What to put in your startup files 17 2.1 Types of shell: interactive and login shells . 17 2.1.1 What is a login shell? Simple tests . 18 2.2 All the startup files . 19 2.3 Options . 21 2.4 Parameters . 21 2.4.1 Arrays . 23 2.5 What to put in your startup files . 24 2.5.1 Compatibility options: SH_WORD_SPLIT and others . 24 2.5.2 Options for csh junkies . 32 2.5.3 The history mechanism: types of history . 34 2.5.4 Setting up history . 36 2.5.5 History options . 37 2.5.6 Prompts . 39 2.5.7 Named directories . 42 2.5.8 ‘Go faster’ options for power users . 43 2.5.9 aliases . 45 2.5.10 Environment variables . 46 3 4 CONTENTS 2.5.11 Path . 47 2.5.12 Mail . 48 2.5.13 Other path-like things . 49 2.5.14 Version-specific things . 50 2.5.15 Everything else . 50 3 Dealing with basic shell syntax 51 3.1 External commands . 51 3.2 Builtin commands . 53 3.2.1 Builtins for printing . 53 3.2.2 Other builtins just for speed . 56 3.2.3 Builtins which change the shell’s state . 57 3.2.4 cd and friends . 59 3.2.5 Command control and information commands .
    [Show full text]