Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

Similar documents
sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Implementation of a simple shell, xssh

Implementation of a simple shell, xssh

Bourne Shell Reference

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

I/O and Shell Scripting

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Linux shell programming for Raspberry Pi Users - 2

GNU Bash. An Introduction to Advanced Usage. James Pannacciulli Systems (mt) Media Temple

Useful Unix Commands Cheat Sheet

Introduction to the Shell

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

Review of Fundamentals

Last Time. on the website

GNU Bash. an introduction to advanced usage. James Pannacciulli Systems Engineer.

Creating a Shell or Command Interperter Program CSCI411 Lab

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

Linux shell scripting Getting started *

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

5/8/2012. Specifying Instructions to the Shell Chapter 8

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Bash command shell language interpreter

Here redirection. Case statement. Advanced Unix Tools Lecture 6 CS214 Spring 2004 Friday March 5, 2004

BASH SHELL SCRIPT 1- Introduction to Shell

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Module 8 Pipes, Redirection and REGEX

INTRODUCTION TO SHELL SCRIPTING ITPART 2

Assignment clarifications

Why Bourne Shell? A Bourne Shell Script. The UNIX Shell. Ken Wong Washington University. The Bourne Shell (CSE 422S)

5/8/2012. Exploring Utilities Chapter 5

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 4 Shell Variables, More Shell Scripts (Thanks to Hal Perkins)

COMP 4/6262: Programming UNIX

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

BASH and command line utilities Variables Conditional Commands Loop Commands BASH scripts

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

UNIX System Programming Lecture 3: BASH Programming

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Shells and Shell Programming

Bash Reference Manual

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Basic Linux (Bash) Commands

CST Algonquin College 2

bash, part 3 Chris GauthierDickey

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

Basics. I think that the later is better.

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Bash Reference Manual Reference Documentation for Bash Edition 2.5b, for Bash Version 2.05b. July 2002

Process Management 1

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

Shell Programming Systems Skills in C and Unix

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Processes and Shells

Advanced bash scripting

5/20/2007. Touring Essential Programs

Process Management! Goals of this Lecture!

RHCE BOOT CAMP. System Administration

CSC UNIX System, Spring 2015

System Programming. Unix Shells

UNIX Shell Programming

Linux Systems Administration Shell Scripting Basics. Mike Jager Network Startup Resource Center

9.2 Linux Essentials Exam Objectives

Project 1: Implementing a Shell

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2018

Shells and Shell Programming

Pod::Usage, pod2usage() - print a usage message from embedded pod documentation

Part III. Shell Config. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

CSCI 2132: Software Development

CSC209 Review. Yeah! We made it!

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

Unix Processes. What is a Process?

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Chapter 1 - Introduction. September 8, 2016

Week Overview. Simple filter commands: head, tail, cut, sort, tr, wc grep utility stdin, stdout, stderr Redirection and piping /dev/null file

The Online Unix Manual

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

Chapter 9. Shell and Kernel

Linux Signals and Daemons

CHAPTER 2 THE UNIX SHELLS

UNIX Kernel. UNIX History

System Programming. Introduction to Unix

Bash Shell Programming Helps

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

85321, Systems Administration Chapter 6: The shell

Shell scripting Scripting and Computer Environment - Lecture 5

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

Week 5 Lesson 5 02/28/18

ACS Unix (Winter Term, ) Page 21

Perl. Many of these conflict with design principles of languages for teaching.

Introduction to Supercomputing

A shell can be used in one of two ways:

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Introduction to shell scripting

Introduction. Let s start with the first set of slides

Topic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes

Transcription:

Bash Scripting Alessandro Barenghi Dipartimento di Elettronica, Informazione e Bioingegneria Politecnico di Milano alessandro.barenghi - at - polimi.it April 28, 2015

Introduction The bash command shell Bash stands for Bourne-Again shell : it is a GNU reimplementation of the Unix r7 shell by S. Bourne Derived from a long line of other shells (sh, Bourne shell, Korn Shell,...) The most common shell and the one you get by default on most Linux systems Other shells differ from bash in terms of syntactic sugar

Introduction Shell scripts A shell script is simply a ASCII text file, which is fed to a command interpreter The script should begin with a shebang a symbol ( #! ) followed by the pathname of the interpreter Any system binary can be used as an interpreter The script pathname is simply passed to it as the first parameter In case no interpreter is specified, the default interpreter (i.e., typically bash ) is used a also called a sha-bang, hashbang, pound-bang, hash-exclam, or hash-pling

Shell Variables typesystem By default, the variables have global visibility in the script The basic variable type in bash is the textual string Any C-like variable name is fine, but the common convention is to have ALL UPPERCASE names The $ operator substitutes a variable with its content

Shell Variables Commandline parameters Commandline parameters of a script can be obtained via implicit variables as: $0, $1, $2 and so on, to obtain one of them (as per C s argv ) $* yields a single variable with the concatenated parameters $@ yields a list of the parameters, as tokenized by bash $# Yields the number of parameters In case you need it $ recalls the value of the last variable expansion

Shell Variables Environment Variables The bash interpreter, as all the binaries, runs with environment defined variables Environment variables can be accessed by running programs (e.g. through C s getenv function) You can add a variable for a single command execution prefixing its declaration to the command The export command allows you to export the variable in the current bash environment To remove a variable from the environment, use the command unset

Non-builtins Some helpful commands All the environment variables can be output invoking Your current working directory is obtained invoking seq is a simple program generating sequence of strings, with a variable integer value Common syntax: seq <beginning> [step] <end> the -f=format allows you to use a format string to print out the values -w pads the numbers to a constant lengths with zeros pwd -s=separator changes the different line ending from \n env

Control Flow Basics Straight line code The statements in a bash script are executed in program order by default ; can be used as a statement separator between statements on a single line: you can turn any script in a one liner (not advised) A # not followed by a! is the beginning of a single line comment The exec command explicitly executes a fork-exec call pair to run the following commands The echo command prints its arguments on the screen The exit <number> command exits the script with return value number

Control Flow Basics if construct The if statement syntax is: if <condition>; then <statements> else <statements> fi The output of a command can be used as a condition (successful execution is true, error is false) true and false commands are available for base cases The test command allows you to test common stuff test -e <filename> returns true if the file exists test! -e <filename> returns true if the file doesn t exist test <string1> = <string2> returns true if the strings match Enclosing the condition in [ ] is equivalent to invoking test

Control Flow Basics for construct The for statement syntax is: for <variable> in <list>; do <statements> done for splits <list> according to the value of the IFS a variable and runs the loop body assigning each element to variable Typically the <list> is obtained as the output of executing a command via command substitution: Enclose something in, bash will substitute it with the output of its execution Common command substitutions include either ls or seq a internal field separator

Control Flow Basics case construct The case statement syntax is: case <variable> in <list>; <pattern>) <statements> ;; esac The case statements performs in a similar fashion to the switch-case in C, i.e., match a variable against alternatives Main difference: since the variables are string, you can specify a POSIX regular expression a to be matched The * metacharacter indicates any (zero or more) characters [ ] can enclose character classes, e.g., [0-9] Concatenation is simply concatenating patterns a Caveat: POSIX regular expressions do not match the expressive power of common regular expression, e.g., they can count

Arithmetic interpretation Learning to do math All the variables are by default strings in bash, i.e. no arith operations are available e.g., trying to print the variable SUM=1+1 will result in 1+1 being printed It is possible to ask for an arithmetic interpretation of a string via double braces (( )) echo $((6*7)) will print 42 It is not possible to enclose multiple statements in (( )) with the only following exception: for (( i=0 ; $i<10 ; i=$i+1 )) do... ; done

Functions...or somehow that looks like them Bash provides minimal support for code refactoring in the form of simplified functions The syntax for a function declaration is: <function name> () { <function body> } All the functions are assumed to have variable arguments, accessible via $@ Function invocation is simply done as function name arg1 arg2 arg3... Warning: the function body is not parsed until the function is invoked: latent syntax errors may hide there

Pipes and File Descriptors Data flow among commands Proper bash plumbing and flow redirection is a basic skill to redirect stuff around the system Practically, bash provides the user with constructs to Concatenate the standard output files ( stdout,stderr ) of a command into the standard input of another one Feed any of them with/to a file on the disk Duplicate data flows or merge them Fits the UNIX paradigm: every command does one precise thing, combine them to obtain complex effects

Pipes and File Descriptors Files and file descriptors Bash allows the redirection of information into a command s stdin or from a command s stdout, stderr Many commands under Linux assume something is piped into their stdin to work properly bash allows explicit referencing to stdin,stdout, stderr employing the 0,1,2 token respectively More filedescriptor (fds from now on) can be opened, and referenced with integers > 2

Pipes and File Descriptors Redirection operators fd > file and fd < file redirects data from a fd, into a file, and viceversa. Redirect a command output, redirects stderr fd >> file appends to the file instead of overwriting fd1 >& fd2 merges the data from fd1 into fd2 command1 command2 redirects the stdout of command1 into the stdin of command2 (this is not bidirectional!) The operators and commands can be concatenated at will

Pipes and File Descriptors Useful redirection targets the stdin of less : less is a text pager, which allows you to read long texts the stdin of the stdin of sort : sorts by line the text passed as input uniq : returns unique occurrences of the lines /dev/null : a fake device discarding anything written into it tee <filename> : copies whatever it is redirected into its stdin both to its stdout and into the <filename>

Pipes and flows Managing Filedescriptors Executing filedescriptor<> filename opens your file for reading and writing (e.g. exec 6<> ) To close a fd, redirect it to - (e.g. exec 6>&- closes 6) read -n number of chars variable name reads, by default from stdin, n characters into the variable read -n number of chars variable name <&3 characters into the variable reads from fd 3, n A simple echo foo may be used to write into an open fd through redirection (e.g. echo foo >3 )

Pipes and flows Pipes Employing a pipe character (i.e. ) chains the standard output of the leading command to the trailing of the last one The stdbuf command allows to specify how many bytes should the input ( -i ), output ( -o ), and error( -e ) buffers be long (changing stderr buffer is not advised!) Comes in terribly handy when employing nc or whenever you want a stream to be pushing characters down the pipe instantly Simply prepend a stdbuf -i0 -o0 -e0 and enjoy! Removing the buffering is critical if you re juggling with socat,nc and the likes to send something via network

Eye of the beholder Keeping an eye on ongoing things Watching over things is always important Sometimes it d be useful to have a self refreshing command out of any command watch does exactly the tricks -n <seconds> specifies how often to refreshing -d highlights the changes from the last time (useful for spotting tiny changes)