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

Similar documents
General Information. Scripting with Bash and Python. Contents. General Information. Contents (cont.) Contents (cont.) Contents (cont.

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

Useful Unix Commands Cheat Sheet

Review of Fundamentals

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

CS160A EXERCISES-FILTERS2 Boyd

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

5/20/2007. Touring Essential Programs

CS Unix Tools. Fall 2010 Lecture 5. Hussam Abu-Libdeh based on slides by David Slater. September 17, 2010

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

Bash Script. CIRC Summer School 2015 Baowei Liu

Lab 2: Linux/Unix shell

Advanced training. Linux components Command shell. LiLux a.s.b.l.

Linux Text Utilities 101 for S/390 Wizards SHARE Session 9220/5522

CST Algonquin College 2

Module 8 Pipes, Redirection and REGEX

Regex, Sed, Awk. Arindam Fadikar. December 12, 2017

Basic Linux (Bash) Commands

Chapter 9. Shell and Kernel

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

- c list The list specifies character positions.

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

CSC UNIX System, Spring 2015

ITST Searching, Extracting & Archiving Data

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

UNIX Shell Programming

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

Introduction to the shell Part II

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

Introduction: What is Unix?

Basics. I think that the later is better.

Assignment clarifications

UNIX, GNU/Linux and simple tools for data manipulation

Shell Programming Overview

STATS Data Analysis using Python. Lecture 15: Advanced Command Line

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

IB047. Unix Text Tools. Pavel Rychlý Mar 3.

Shell Programming Systems Skills in C and Unix

(a) About Unix. History

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

Introduction To Linux. Rob Thomas - ACRC

Shells and Shell Programming

Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

CS214-AdvancedUNIX. Lecture 2 Basic commands and regular expressions. Ymir Vigfusson. CS214 p.1

EECS2301. Lab 1 Winter 2016

Linux Command Line Interface. December 27, 2017

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

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

Perl and R Scripting for Biologists

Introduction to Linux

Introduction to UNIX. Introduction. Processes. ps command. The File System. Directory Structure. UNIX is an operating system (OS).

Introduction to UNIX. CSE 2031 Fall November 5, 2012

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

2) clear :- It clears the terminal screen. Syntax :- clear

Scripting Languages Course 1. Diana Trandabăț

Unleashing the Shell Hands-On UNIX System Administration DeCal Week 6 28 February 2011

Practical 02. Bash & shell scripting

QUESTION BANK ON UNIX & SHELL PROGRAMMING-502 (CORE PAPER-2)

Shells and Shell Programming

Chapter-3. Introduction to Unix: Fundamental Commands

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

UNIX / LINUX - REGULAR EXPRESSIONS WITH SED

CS Advanced Unix Tools & Scripting

Reading and manipulating files

Advanced Linux Commands & Shell Scripting

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Chapter 4. Unix Tutorial. Unix Shell

Introduction to UNIX Command Line

Appendix A GLOSSARY. SYS-ED/ Computer Education Techniques, Inc.

Topic 2: More Shell Skills

Files and Directories

CS 25200: Systems Programming. Lecture 11: *nix Commands and Shell Internals

Introduction to the Shell

Lecture 3 Tonight we dine in shell. Hands-On Unix System Administration DeCal

CSCI2467: Systems Programming Concepts

Command Line Interface Techniques

5/8/2012. Exploring Utilities Chapter 5

UNIX Kernel. UNIX History

CSCI 2132 Software Development. Lecture 7: Wildcards and Regular Expressions

UNIX and Linux Essentials Student Guide

BASH SHELL SCRIPT 1- Introduction to Shell

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

Introduction to UNIX. Introduction EECS l UNIX is an operating system (OS). l Our goals:

Unix Basics. Benjamin S. Skrainka University College London. July 17, 2010

Std: XI CHAPTER-3 LINUX

DATA 301 Introduction to Data Analytics Command Line. Dr. Ramon Lawrence University of British Columbia Okanagan

Why learn the Command Line? The command line is the text interface to the computer. DATA 301 Introduction to Data Analytics Command Line

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

Linux Essentials. Programming and Data Structures Lab M Tech CS First Year, First Semester

Introduction to UNIX command-line II

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

Introduction to Linux

Part 1: Basic Commands/U3li3es

Introduction to Linux

Assume that username is cse. The user s home directory will be /home/cse. You may remember what the relative pathname for users home directory is: ~

Getting to grips with Unix and the Linux family

Software I: Utilities and Internals. What isbash?

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

Introduction to Linux Organizing Files

Transcription:

Part III Shell Config Compact Course @ Max-Planck, February 16-26, 2015 33

Special Directories. current directory.. parent directory ~ own home directory ~user home directory of user ~- previous directory (works also w/o ) cd / usr / local / bin pwd cd ~ pwd cd ~- pwd cd.. pwd Compact Course @ Max-Planck, February 16-26, 2015 34

Aliases and Variables alias - short form for any command alias alias ll='ls -l ' environment variables Variables are not only within scripts, but also in the shell By setting a variable, it is present in the current script/shell By exporting it (export VARIABLE=value), it is also present at all child processes (NOT at the parent) Many environment variables are already set by default Compact Course @ Max-Planck, February 16-26, 2015 35

Aliases and Variables alias - short form for any command alias alias ll='ls -l ' environment variables Variables are not only within scripts, but also in the shell By setting a variable, it is present in the current script/shell By exporting it (export VARIABLE=value), it is also present at all child processes (NOT at the parent) Many environment variables are already set by default PATH =/ home / neckel / bin /: $PATH ; echo $PATH env local variables set shows all local and global variables and functions unset deletes a variable Compact Course @ Max-Planck, February 16-26, 2015 35

Bash config profile and rc especially aliases are used in every session should not be defined each time You might even run arbitrary code on login or logout e.g. clean up (kill jobs) on logout Profile is used for complete session bashrc is used for a single terminal Files /etc/profile /etc/bash.bashrc $HOME/.bash profile $HOME/.bashrc $HOME/.bash logout Compact Course @ Max-Planck, February 16-26, 2015 36

Pipes stdin/stdout: Standard input/output for programs Most Linux programs read from stdin and write to stdout Pipes are used to redirect input and output cmd1 cmd2 connect the output of cmd1 with the input of cmd2 cmd > file redirect the output of cmd to file Compact Course @ Max-Planck, February 16-26, 2015 37

Pipes stdin/stdout: Standard input/output for programs Most Linux programs read from stdin and write to stdout Pipes are used to redirect input and output cmd1 cmd2 connect the output of cmd1 with the input of cmd2 cmd > file redirect the output of cmd to file cmd 2> file redirect stderr of cmd to file cmd > file 2>&1 redirect stdout to file and stderr to stdout cmd &> file redirect all output to file cmd >> file redirect the output of cmd and append it to file cmd < file use the content of file as stdin for cmd cmd <<Endmark Read from stdin until Endmark is inserted in a seperate line ; command separator Compact Course @ Max-Planck, February 16-26, 2015 37

Wildcards Wildcards are expanded by the shell * zero or more characters? exactly one character [abcd] one of the characters a-d [a-d] same [!a-d] any other character {first,second} either first or second Similar pattern exist in other contexts as well (compare regular expressions), but always a bit different... :-( Compact Course @ Max-Planck, February 16-26, 2015 38

Part IV Bash Advanced: Regular Expressions and More Compact Course @ Max-Planck, February 16-26, 2015 39

Regular Expressions??? Compact Course @ Max-Planck, February 16-26, 2015 40

Regular Expressions??? The set of regular languages over an alphabet Σ and the corresponding regular expressions are defined recursively as follows: The empty language Ø is a regular language, and the corresponding regular expression is Ø. The empty string { } is a regular language, and the corresponding regular expression is. For each a in Σ, the singleton language { a } is a regular language, and the corresponding regular expression is a. If A and B are regular languages, and r1 and r2 are the corresponding regular expressions, Then A U B (union) is a regular language, and the corresponding regular expression is (r1+r2) AB (concatenation) is a regular language, and the corresponding regular expression is (r1r2) A* (Kleene star) is a regular language, and the corresponding regular expression is (r1*) Compact Course @ Max-Planck, February 16-26, 2015 40

Regular Expressions!!! What does all this mean to you, as a user? Absolutely nothing. As a user, you don t care if it s regular, nonregular, unregular, irregular, or incontinent. So long as you know what you can expect from it, you know all you need to care about. Jeffrey Friedl, author of Mastering Regular Expressions Compact Course @ Max-Planck, February 16-26, 2015 41

Regular Expressions!!! What does all this mean to you, as a user? Absolutely nothing. As a user, you don t care if it s regular, nonregular, unregular, irregular, or incontinent. So long as you know what you can expect from it, you know all you need to care about. Jeffrey Friedl, author of Mastering Regular Expressions Did you ever...... search for a character or string in a text file?... use tab for auto-completion?... use the * in a terminal for selecting a group of files?... Then you ve already somehow used regular expressions. Compact Course @ Max-Planck, February 16-26, 2015 41

Regular Expressions When searching for a string, exactly the given character sequence is searched regular expressions get powerful as a tool to find patterns Additionally to ordinary characters, which stand for themselves special characters are used which are interpreted in a special way. The exact syntax for special characters differs between different implementations Compact Course @ Max-Planck, February 16-26, 2015 42

Regular Expressions When searching for a string, exactly the given character sequence is searched regular expressions get powerful as a tool to find patterns Additionally to ordinary characters, which stand for themselves special characters are used which are interpreted in a special way. The exact syntax for special characters differs between different implementations Example to identify an email address: [^@]\+@.\+\.[^.]\+ regular expressions usually look very cryptic! But imagine you would have to write a normal program to identify an email address! Regular expressions can be used to find/replace groups of strings which can be described by a pattern Compact Course @ Max-Planck, February 16-26, 2015 42

Special Characters for Regular Expressions char A character maching itself * matches zero or more occurences (greedy!) of the previous expression \+ matches one or more occurences (GNU extension) \? matches zero or one occurence (GNU extension) \{i\} matches i occurences \{i,\} matches i or more occurences \{i,j\} matches i to j occurences. matches an arbitrary character Compact Course @ Max-Planck, February 16-26, 2015 43

Special Characters for Regular Expressions char A character maching itself * matches zero or more occurences (greedy!) of the previous expression \+ matches one or more occurences (GNU extension) \? matches zero or one occurence (GNU extension) \{i\} matches i occurences \{i,\} matches i or more occurences \{i,j\} matches i to j occurences. matches an arbitrary character ˆ matches the beginning of a string $ matches the end of a string [list] matches a single character from the list ([ˆlist] any character not in the list) \n matches newline \(\) defines a group, reuse via \1 (1st group), \2 (2nd group) \ allows for a logical OR Compact Course @ Max-Planck, February 16-26, 2015 43

Character Classes [:digit:] 0 to 9 (alternative: [0-9]). [:alnum:] alphanumeric character 0-9 or A-Z or a-z. [:alpha:] character A-Z or a-z. [:xdigit:] Hexadecimal notation 0-9, A-F, a-f. [:punct:] Punctuation symbols, e.g..,?! ; : # $ % & ( ) [:print:] Any printable character. [:space:] whitespace (space, tab,...). [:upper:] uppercase character A-Z (alternative: [A-Z]). [:lower:] lowercase character a-z (alternative: [a-z]). Compact Course @ Max-Planck, February 16-26, 2015 44

Character Classes [:digit:] 0 to 9 (alternative: [0-9]). [:alnum:] alphanumeric character 0-9 or A-Z or a-z. [:alpha:] character A-Z or a-z. [:xdigit:] Hexadecimal notation 0-9, A-F, a-f. [:punct:] Punctuation symbols, e.g..,?! ; : # $ % & ( ) [:print:] Any printable character. [:space:] whitespace (space, tab,...). [:upper:] uppercase character A-Z (alternative: [A-Z]). [:lower:] lowercase character a-z (alternative: [a-z]). Attention: Additional brackets are necesssary! [[:digit:]] [0 9] Compact Course @ Max-Planck, February 16-26, 2015 44

programs using regular expressions grep print lines matching a pattern grep -i case insensitive grep -v invert match grep -n additionally print line number tr translate: echo "lower/upper" tr "A-Z""a-z" sed stream editor awk pattern scanning and processing language find search for files in a directory hierarchy... most editors can handle regular expressions Compact Course @ Max-Planck, February 16-26, 2015 45

sed - Stream Editor works (as most bash programs) on a stream of data data is processed linewise no way of going back a line ( efficient) apply some action on selected lines (using addresses to select) Compact Course @ Max-Planck, February 16-26, 2015 46

sed - Stream Editor works (as most bash programs) on a stream of data data is processed linewise no way of going back a line ( efficient) apply some action on selected lines (using addresses to select) Addresses address: line number or regular expression zero, one or two (comma-separated) addresses can be used zero addresses: all lines are processed one address: all lines matching the address are processed two addresses: match from first to second address Compact Course @ Max-Planck, February 16-26, 2015 46

sed - Stream Editor works (as most bash programs) on a stream of data data is processed linewise no way of going back a line ( efficient) apply some action on selected lines (using addresses to select) Addresses address: line number or regular expression zero, one or two (comma-separated) addresses can be used zero addresses: all lines are processed one address: all lines matching the address are processed two addresses: match from first to second address n match only line number n n~step starting from line n, match every stepth line /pattern/ lines matching the given regular expression i,j all lines from i to j (including both) $ match the last line Compact Course @ Max-Planck, February 16-26, 2015 46

sed - Editing Commands parameters -n supress output (per default, all lines are printed) -e editing command follows in command line -f script to be read from file commands p print line = print line number i\text and a\text insert text before/after matched line c\text replace matched line by text s replace pattern, e.g. sed -e 's/search/replace/g' (note: g at end optional, but replaces globally!) (note: use single quotes to avoid problems with white space and variable expansion for $) w write line into given file, sed -n '/patt/ w out.txt' demo.txt Compact Course @ Max-Planck, February 16-26, 2015 47

Operating on Files less is more than more more displays the content of text files pagewise only downward-scrolling is possible less is an extended and more flexible more Compact Course @ Max-Planck, February 16-26, 2015 48

Operating on Files less is more than more more displays the content of text files pagewise only downward-scrolling is possible less is an extended and more flexible more outputting files cat outputs the content of a file to stdout (try tac) cat concatenates several files head outputs the first part of files tail outputs the last part of files for i in ` seq 30 `; do echo $i >> temp. txt ; sleep 1; \ done & tail -f temp. txt Compact Course @ Max-Planck, February 16-26, 2015 48

Operating on Files (2) file find out more about file type cmp Compare files byte by byte diff Compare files line by line (graphical: kdiff3) patch apply patch (created by diff) to a file tar pack and compress files (try tar -xzf and tar -czf) sort sort lines of text files (and write to stdout) uniq report or omit repeated lines wc print newline, word, and byte counts for each file cut remove sections from each line of files echo " 1;2;3;4 " cut -b 4-5 echo " 1;2;3;4 " cut -d ";" -f 3 cut -d ' ' -f1,2 / etc / mtab Compact Course @ Max-Planck, February 16-26, 2015 49

Operating on Files (3) paste merge lines of files join basic database functionality (join tables) file1. txt line1 line2 line3 line4 file2. txt a b c d file3. txt 1 2 3 4 paste -d.- file?. txt paste file [12]. txt > j1. txt paste file [13]. txt > j2. txt join j?. txt Compact Course @ Max-Planck, February 16-26, 2015 50