Bash Shell Scripts

Total Page:16

File Type:pdf, Size:1020Kb

Bash Shell Scripts Bash Shell Scripting: Getting Started SELF2015 Beau Sanders Greenville Technical College https://beausanders.org/self15 150609 SELF2015 • 1 About me • Academic Program Director Computer Technology Department Greenville Technical College Greenville, South Carolina • Instructor for 13 years • RHCE, RHCSA, RHCT • Certified Red Hat Academy Instructor • Teach Linux Essentials, Administration, Network Applications, Security, and Scripting 150609 SELF2015 • 2 Assumptions • You can handle the Linux command prompt • You understand the need to make administration easier and more manageable • You know a little programming • You know some Linux commands • You understand command structure in Linux 150609 SELF2015 • 3 Some Basic Linux Commands • cat • less • ls • more • ps • chmod • pwd • chown • cp • chgrp • mv • useradd • rm • usermod • mkdir • logger • head • wget • tail • mail 150609 SELF2015 • 4 Linux Command Structure 150609 SELF2015 • 5 Bash Shell Scripting: Getting Started Overview of BASH 150609 SELF2015 • 6 The bash shell • The shell is the most commonly used program in Linux • The shell is what you see when you log in or open a terminal, and is what you use to start most every command • The most commonly used shell in Linux is the bash shell • Bourne Again SHell 150609 SELF2015 • 7 Interactive Shells vs. Shell Scripts • The bash shell is an interactive shell • The bash shell is also designed to be a powerful scripting language – Bash shell scripts are short programs written using the same syntax used on the command line – Shell scripts allow users to automate often repeated actions by combining a series of commands – Many of the features of the bash shell provide programming logic (such as branches and loops) for writing sophisticated scripts 150609 SELF2015 • 8 An Introduction to Shell Scripts • A bash shell script is just a text file containing a list of ordinal Linux commands • The commands are sent through a specified program, called an interpreter, which runs each command in turn • The interpreter will be the bash shell (/bin/bash or /bin/sh). • Other interpreters allow you to use more powerful programming languages like Perl, Python and Ruby 150609 SELF2015 • 9 An Introduction to Shell Scripts • The first line of your script must specify which interpreter to send instructions to – This is done with a special string called a "shebang" which looks like this: #! – The shebang is followed by the name of the interpreter for this script – For example, to use bash as your interpreter you would use #!/bin/sh or #!/bin/bash 150609 SELF2015 • 10 An Introduction to Shell Scripts • Before you can run a script, you must enable the executable permission on it – otherwise, it's just a text file • A typical command to enable execute permissions on a script is: chmod 775 scriptname 150609 SELF2015 • 11 Default Path • A fixed default set of directories in which Linux looks for commands • These directories are referred to collectively as your PATH • For security reasons, your PATH seldom includes the current directory 150609 SELF2015 • 12 Default Path • To solve this problem you have two choices: – You can explicitly specify the location of the script by typing ~/foo.sh or ./foo.sh ( . always refers to the current directory) – You can place the script in a directory that is part of your PATH – Non-root users will need to create their personal bin: $mkdir ~/bin 150609 SELF2015 • 13 Bash Shell Scripting: Getting Started Variables and Redirecting Data 150609 SELF2015 • 14 Shell Variable Basics • The bash shell allows users to set and reference shell variables • A shell variable is simply a named value that the shell remembers • Shell variables • Can be used in commands • Can be used in shell scripts • Can also be referenced by programs as configuration options 150609 SELF2015 • 15 Shell Variable Basics • There are two types of shell variables: local variables and environmental variables • A local variable exists only within the shell in which it is created • Environmental variables are inherited by child shells such as when a graphical terminal is launched after logging in • Setting local variables is quite simple 150609 SELF2015 • 16 Shell Variable Basics • Make sure that you do not put any spaces on either side of the = sign • The shell will "hang on" to this association for as long as the shell exists or until it is unset 150609 SELF2015 • 17 Environmental Variables • Environmental variables are a part of the process stored in the kernel, like the process id, user id, and current working directory are part of the process • Whenever a process starts another process, environmental variables are inherited by the child process • This allows users to use the bash shell to create or modify an environmental variable, and then all commands started by the shell will inherit that variable 150609 SELF2015 • 18 Environmental Variables • How do we create environmental variables within the bash shell? – First, a shell variable is created, and then the shell variable is "promoted" to an environmental variable using the export command – The variable will then be exported to any future child processes 150609 SELF2015 • 19 Standard In and Standard Out • Command line programs (terminal based programs) read information from one source, and write information to one destination • The source programs read from is referred to as Standard In (stdin), and is usually connected to a terminal's keyboard • The destination programs write to is referred to as Standard Out (stdout), and is usually connected to a terminal's display • When using the bash shell, stdout can be redirected using > or >>, and stdin can be redirected using < 150609 SELF2015 • 20 Redirecting stdout Writing Output to a File • The bash shell uses > to redirect a process's stdout stream to a file • 150609 SELF2015 • 21 Appending Output to a File • To append a command's output to a file, rather than clobbering it, bash uses >> • Often >> is used to create a log file in a script 150609 SELF2015 • 22 Redirecting stdin • Bash uses < to read input from somewhere other than the keyboard • Often used to read in a file as input to script 150609 SELF2015 • 23 Bash Shell Scripting: Getting Started Commands and Tools 150609 SELF2015 • 24 echo • The echo command takes whatever text is typed as part of the command and echoes it to stdout • The echo command, together with redirection, can be used to create text files 150609 SELF2015 • 25 echo • Usage: echo [OPTIONS] [STRING...] • Echoes STRING(s) to standard output 150609 SELF2015 • 26 grep • The name grep stands for general regular expression parser • A regular expression is simply a way of describing a pattern, or template, to match some sequence of characters 150609 SELF2015 • 27 grep 150609 SELF2015 • 28 grep • The pattern argument supplies the template characters for which grep is to search • The pattern is expected to be a single argument – If the pattern contains any spaces, or other characters special to the shell, you must enclose the pattern in quotes to prevent the shell from expanding or word splitting it 150609 SELF2015 • 29 grep • By default, grep shows only the lines matching the search pattern • Sometimes you are interested in the lines that do NOT match the pattern • The -v command line switch inverts grep's operation • 150609 SELF2015 • 30 Pipes • When two commands are joined by a pipe, the stdout stream of the first process is tied directly to the stdin sequence of the second process • In order to create a pipe using bash, the two commands are joined with a vertical bar | • All processes that are joined in a pipe are referred to as a process group • Many commands in Unix are designed to operate as a filter, reading input from stdin and sending output to stdout 150609 SELF2015 • 31 Filtering output using grep • The traditional Unix grep command is commonly used in pipes to reduce data to only useful data; sometimes called a filter • The grep command is used to search for and extract lines which contain a specified string of text 150609 SELF2015 • 32 Filtering output using grep • The first argument to the grep command is the string of text to be searched for • Any remaining arguments are files to be searched for the text • If the grep command is called with only one argument, it looks to Standard In as its source of data on which to operate 150609 SELF2015 • 33 Extracting and Assembling Text: cut and paste • The cut command extracts texts from text files, based on columns specified by bytes, characters, or fields • The paste command merges two text files line by line 150609 SELF2015 • 34 The cut Command - Extracting Text with cut • The cut command extracts columns of text from a text file or data stream • The cut command interprets any command line arguments as names of files on which to operate, or operates on the standard in stream if none are provided • The cut command can extract by bytes, characters, or fields 150609 SELF2015 • 35 The cut Command - Extracting Text with cut • The list arguments are actually a comma-separated list of ranges • Each range can take one of the following forms: 150609 SELF2015 • 36 Extracting text by Character Position with cut -c • With the -c command line switch, the list specifies a character's position in a line of text • The first line character is character number 1 150609 SELF2015 • 37 Extracting Fields of Text with cut -f • The cut command can also be used to extract text that is structured not by character position, but by some delimiter character, such as a TAB or : 150609 SELF2015 • 38 The paste Command
Recommended publications
  • Using the GNU Compiler Collection (GCC)
    Using the GNU Compiler Collection (GCC) Using the GNU Compiler Collection by Richard M. Stallman and the GCC Developer Community Last updated 23 May 2004 for GCC 3.4.6 For GCC Version 3.4.6 Published by: GNU Press Website: www.gnupress.org a division of the General: [email protected] Free Software Foundation Orders: [email protected] 59 Temple Place Suite 330 Tel 617-542-5942 Boston, MA 02111-1307 USA Fax 617-542-2652 Last printed October 2003 for GCC 3.3.1. Printed copies are available for $45 each. Copyright c 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with the Invariant Sections being \GNU General Public License" and \Funding Free Software", the Front-Cover texts being (a) (see below), and with the Back-Cover Texts being (b) (see below). A copy of the license is included in the section entitled \GNU Free Documentation License". (a) The FSF's Front-Cover Text is: A GNU Manual (b) The FSF's Back-Cover Text is: You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development. i Short Contents Introduction ...................................... 1 1 Programming Languages Supported by GCC ............ 3 2 Language Standards Supported by GCC ............... 5 3 GCC Command Options .........................
    [Show full text]
  • Bash Shell Scripts
    Bash Shell Scripts Writing Bash shell scripts Bash shell scripts are text files Text files most efficiently built with programming editors (emacs or vi) File must be executable and in search path chmod 700 my_script PATH environment variable may not include .! An example shell script: #!/bin/bash #My first script echo "Hello World!" Bash Shell Scripts Writing Bash shell scripts Compile a Verilog file with vlog #!/bin/bash if [ ! d work ] ; then echo work does not exist, making it vlib work fi if [ ! s adder.v ] ; then vlog adder.v fi work directory must exist before compilation Get scripts via wget, eg: wget http://web.engr.oregonstate.edu/~traylor/ece474/script --- Bash Shell Scripts Writing Bash shell scripts File attribute checking #!/bin/bash if [ ! s junk_dir ] ; then mkdir junk_dir fi Spaces around brackets are needed! File attribute checking d exists and is a directory e, a file exists f exists and is a regular file s file exists and is not empty --- Bash Shell Scripts Writing Bash shell scripts Compile Verilog then run a simultion #!/bin/bash if [ ! -d "work" ] ; then vlib work fi if [ -s "adder.v" ] ; then vlog adder.v #runs simulation with a do file and no GUI vsim adder -do do.do quiet c else echo verilog file missing fi --- Bash Shell Scripts Writing Bash shell scripts vsim command and arguments vsim entity_name do dofile.do quiet -c -quiet (do not report loading file messages) -c (console mode, no GUI) -do (run vsim from a TCL do file) +nowarnTFMPC (don’t warn about mismatched ports, scary) +nowarnTSCALE (don’t warn about timing mismatches) Try vsim help for command line arguements --- Bash Shell Scripts Writing Bash Shell Scripts (TCL Script) In another text file, we create a TCL script with commands for the simulator.
    [Show full text]
  • Introduction to Linux by Lars Eklund Based on Work by Marcus Lundberg
    Introduction to Linux By Lars Eklund Based on work by Marcus Lundberg ● What is Linux ● Logging in to UPPMAX ● Navigate the file system ● “Basic toolkit” What is Linux ● The Linux Operating system is a UNIX like UNIX compatible Operating system. ● Linux is a Kernel on which many different programs can run. The shell(bash, sh, ksh, csh, tcsh and many more) is one such program ● Linux has a multiuser platform at its base which means permissions and security comes easy. Many Flavours Connect to UPPMAX ● (Download XQuartz or other X11 server for Mac OS ) ● Linux and MacOS: – start Terminal – $ ssh -X [email protected] Connect to UPPMAX for windows users ● Download a X-server such as GWSL or X-ming or VcXsrv or an other of your choosing ● Install WSL and a Distribution such as ubuntu or a ssh program such as MobaXTerm ● Connect to $ ssh -X [email protected] Windows links ● https://sourceforge.net/projects/vcxsrv/ ● https://mobaxterm.mobatek.net/ ● https://opticos.github.io/gwsl/ ● https://sourceforge.net/projects/xming/ ● https://docs.microsoft.com/en-us/windows/wsl/install-wi n10 ● Don’t forget to update to wsl2 X11-forwarding graphics from the command line ● Graphics can be sent through the SSH connection you’re using to connect - Use ssh -Y or ssh -X ● MacOS users will need to install XQuartz. ● When starting a graphical program, a new window will open, but your terminal will be “locked”. - Run using & at the end to run it as a background proccess e.g. “gedit &” - Alternatively, use ctrl-z to put gedit to sleep and
    [Show full text]
  • Conda-Build Documentation Release 3.21.5+15.G174ed200.Dirty
    conda-build Documentation Release 3.21.5+15.g174ed200.dirty Anaconda, Inc. Sep 27, 2021 CONTENTS 1 Installing and updating conda-build3 2 Concepts 5 3 User guide 17 4 Resources 49 5 Release notes 115 Index 127 i ii conda-build Documentation, Release 3.21.5+15.g174ed200.dirty Conda-build contains commands and tools to use conda to build your own packages. It also provides helpful tools to constrain or pin versions in recipes. Building a conda package requires installing conda-build and creating a conda recipe. You then use the conda build command to build the conda package from the conda recipe. You can build conda packages from a variety of source code projects, most notably Python. For help packing a Python project, see the Setuptools documentation. OPTIONAL: If you are planning to upload your packages to Anaconda Cloud, you will need an Anaconda Cloud account and client. CONTENTS 1 conda-build Documentation, Release 3.21.5+15.g174ed200.dirty 2 CONTENTS CHAPTER ONE INSTALLING AND UPDATING CONDA-BUILD To enable building conda packages: • install conda • install conda-build • update conda and conda-build 1.1 Installing conda-build To install conda-build, in your terminal window or an Anaconda Prompt, run: conda install conda-build 1.2 Updating conda and conda-build Keep your versions of conda and conda-build up to date to take advantage of bug fixes and new features. To update conda and conda-build, in your terminal window or an Anaconda Prompt, run: conda update conda conda update conda-build For release notes, see the conda-build GitHub page.
    [Show full text]
  • Metal C Programming Guide and Reference
    z/OS Version 2 Release 3 Metal C Programming Guide and Reference IBM SC14-7313-30 Note Before using this information and the product it supports, read the information in “Notices” on page 159. This edition applies to Version 2 Release 3 of z/OS (5650-ZOS) and to all subsequent releases and modifications until otherwise indicated in new editions. Last updated: 2019-02-15 © Copyright International Business Machines Corporation 1998, 2017. US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents List of Figures...................................................................................................... vii List of Tables........................................................................................................ ix About this document.............................................................................................xi Who should read this document................................................................................................................. xi Where to find more information..................................................................................................................xi z/OS Basic Skills in IBM Knowledge Center.......................................................................................... xi How to read syntax diagrams......................................................................................................................xi How to send your comments to IBM......................................................................xv
    [Show full text]
  • Unix and Computer Science Skills Tutorial Workbook 2: Longer Lasting Fingers and Other Good Things to Know
    Unix and Computer Science Skills Tutorial Workbook 2: Longer Lasting Fingers and other Good Things to Know January 25, 2016 This workbook is a tutorial on some of the things that make it easier to work on the command line and some of the more powerful things you can do on the command line. As with the first workbook I recommend that you take the time to work through the examples to get a feeling for how things work. I have also included some questions and prompts, again, to prompt you to take a look at the tools and ideas. Sometimes the answers to the questions will be fairly direct from the workbook and some will require some extra research on your part. The prompts will encourage you to try things for yourself. This workbooks assumes that you are comfortable doing basic tasks on the command line as complicated as copying, reading and editing files. Workbook 1 in this tutorial series provides an introduction to this material. 1 Better Command lining for Longer Lasting Fingers One of the main things computer scientists and programmers are interested in is finding the most elegant solution possible. In the mathematical and computer science context elegance refers to finding the simplest solution to a problem that still completely solves the problem. When you are designing a program or an algorithm, you'll want to find elegance in your solution. When you are working, though, you should also look for elegance in your process and this section is intended to help you find the more elegant way to get things done.
    [Show full text]
  • Debugging a Program with Dbx
    Debugging a Program With dbx A Sun Microsystems, Inc. Business 901 San Antonio Road Palo Alto, , CA 94303-4900 Part No: 805-4948 Revision A, February 1999 USA 650 960-1300 fax 650 969-9131 Debugging a Program With dbx Part No: 805-4948 Revision A, February 1999 Copyright 1999 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California 94303-4900 U.S.A. All rights reserved. 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. Portions of this product may be derived from the UNIX® system, licensed from Novell, Inc., and from the Berkeley 4.3 BSD system, licensed from the University of California. UNIX is a registered trademark in the United States and in other countries and is exclusively licensed by X/Open Company Ltd. Third-party software, including font technology in this product, is protected by copyright and licensed from Sun’s suppliers. RESTRICTED RIGHTS: Use, duplication, or disclosure by the U.S. Government is subject to restrictions of FAR 52.227-14(g)(2)(6/87) and FAR 52.227-19(6/87), or DFAR 252.227-7015(b)(6/95) and DFAR 227.7202-3(a). Sun, Sun Microsystems, the Sun logo, and Solaris are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and in other countries. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc.
    [Show full text]
  • An Introduction to the C Shell
    -- -- An Introduction to the C shell William Joy (revised for 4.3BSD by Mark Seiden) Computer Science Division Department of Electrical Engineering and Computer Science University of California, Berkeley Berkeley, California 94720 ABSTRACT Csh is a newcommand language interpreter for UNIX†systems. It incorporates good features of other shells and a history mechanism similar to the redo of INTERLISP. While incorporating manyfeatures of other shells which makewriting shell programs (shell scripts) easier,most of the features unique to csh are designed more for the interac- tive UNIX user. UNIX users who have read a general introduction to the system will find a valuable basic explanation of the shell here. Simple terminal interaction with csh is possible after reading just the first section of this document. The second section describes the shell’s capabilities which you can explore after you have begun to become acquainted with the shell. Later sections introduce features which are useful, but not necessary for all users of the shell. Additional information includes an appendix listing special characters of the shell and a glossary of terms and commands introduced in this manual. Introduction A shell is a command language interpreter. Csh is the name of one particular command interpreter on UNIX.The primary purpose of csh is to translate command lines typed at a terminal into system actions, such as invocation of other programs. Csh is a user program just likeany you might write. Hopefully, csh will be a very useful program for you in interacting with the UNIX system. In addition to this document, you will want to refer to a copyofthe UNIX User Reference Manual.
    [Show full text]
  • Systems Supplement for COP 3601
    Systems Guide for COP 3601: Introduction to Systems Software Charles N. Winton Department of Computer and Information Sciences University of North Florida 2010 Chapter 1 A Basic Guide for the Unix Operating System 1. 1 Using the Unix On-line Manual Pages Facility The following are some of the Unix commands for which you may display manual pages: cal cat cd chmod cp cut date echo egrep ex fgrep file find gcc gdb grep kill less ln locate ls mail make man mesg mkdir mv nohup nl passwd pr ps pwd rm rmdir set sleep sort stty tail tee time touch tr tty uname umask unset vim wall wc which who whoami write For example, to display the manual pages for the command "cp" enter man cp Note that you may also enter man man to find out more about the "man" command. In addition there may be commands unique to the local environment with manual pages; e.g., man turnin accesses a manual page for a project submission utility named Aturnin@ that has been added to the system. If you enter a command from the Unix prompt that requires an argument, the system will recognize that the command is incomplete and respond with an error message. For example, if the command "cp" had been entered at the Unix prompt the system would respond with the information such as shown below: cp: missing file arguments Try `cp --help' for more information. The "cp" command is a copy command. If the arguments which tell the command which file to copy and where to locate the copied file are missing, then the usage error message is generated.
    [Show full text]
  • What's a Script?
    Linux shell scripting – “Getting started ”* David Morgan *based on chapter by the same name in Classic Shell Scripting by Robbins and Beebe What ’s a script? text file containing commands executed as a unit “command” means a body of code from somewhere from where? – the alias list, in the shell’s memory – the keywords, embedded in the shell – the functions that are in shell memory – the builtins, embedded in the shell code itself – a “binary” file, outboard to the shell 1 Precedence of selection for execution aliases keywords (a.k.a. reserved words) functions builtins files (binary executable and script) – hash table – PATH Keywords (a.k.a. reserved words) RESERVED WORDS Reserved words are words that have a special meaning to the shell. The following words are recognized as reserved when unquoted and either the first word of a simple command … or the third word of a case or for command: ! case do done elif else esac fi for function if in select then until while { } time [[ ]] bash man page 2 bash builtin executables source continue fc popd test alias declare fg printf times bg typeset getopts pushd trap bind dirs hash pwd type break disown help read ulimit builtin echo history readonly umask cd enable jobs return unalias caller eval kill set unset command exec let shift wait compgen exit local shopt complete export logout suspend * code for a bash builtin resides in file /bin/bash, along with the rest of the builtins plus the shell program as a whole. Code for a utility like ls resides in its own dedicated file /bin/ls, which holds nothing else.
    [Show full text]
  • Using the GNU Compiler Collection
    Using the GNU Compiler Collection Richard M. Stallman Last updated 20 April 2002 for GCC 3.2.3 Copyright c 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. For GCC Version 3.2.3 Published by the Free Software Foundation 59 Temple Place—Suite 330 Boston, MA 02111-1307, USA Last printed April, 1998. Printed copies are available for $50 each. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being “GNU General Public License”, the Front-Cover texts being (a) (see below), and with the Back-Cover Texts being (b) (see below). A copy of the license is included in the section entitled “GNU Free Documentation License”. (a) The FSF’s Front-Cover Text is: A GNU Manual (b) The FSF’s Back-Cover Text is: You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development. i Short Contents Introduction ...................................... 1 1 Compile C, C++, Objective-C, Ada, Fortran, or Java ....... 3 2 Language Standards Supported by GCC ............... 5 3 GCC Command Options .......................... 7 4 C Implementation-defined behavior ................. 153 5 Extensions to the C Language Family ................ 157 6 Extensions to the C++ Language ................... 255 7 GNU Objective-C runtime features.................. 267 8 Binary Compatibility ........................... 273 9 gcov—a Test Coverage Program ................... 277 10 Known Causes of Trouble with GCC ...............
    [Show full text]
  • CLI User's Guide
    AccuRev® CLI User’s Guide Version 7.1 Revised 30-October-2017 Copyright and Trademarks Copyright © Micro Focus 2017. All rights reserved. This product incorporates technology that may be covered by one or more of the following patents: U.S. Patent Numbers: 7,437,722; 7,614,038; 8,341,590; 8,473,893; 8,548,967. AccuRev, AgileCycle, and TimeSafe are registered trademarks of Micro Focus. AccuBridge, AccuReplica, AccuSync, AccuWork, Kando, and StreamBrowser are trademarks of Micro Focus. All other trade names, trademarks, and service marks used in this document are the property of their respective owners. Table of Contents Preface........................................................................................................................vii Using This Book ................................................................................................................................ vii Typographical Conventions ............................................................................................................... vii Contacting Technical Support............................................................................................................ vii 1. Overview of the AccuRev® Command-Line Interface ........................................................................................ 1 Using AccuRev with a Secure AccuRev Server............................................................................ 1 Working with Files in a Workspace ..............................................................................................3
    [Show full text]