DAVE LIDDAMENT INTRODUCTION TO BASH

Similar documents
LING 408/508: Computational Techniques for Linguists. Lecture 5

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

Open up a terminal, make sure you are in your home directory, and run the command.

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Quick Start Guide. by Burak Himmetoglu. Supercomputing Consultant. Enterprise Technology Services & Center for Scientific Computing

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

CSE 15L Winter Midterm :) Review

CSC209. Software Tools and Systems Programming.

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Introduction: What is Unix?

GNU/Linux 101. Casey McLaughlin. Research Computing Center Spring Workshop Series 2018

Practical 02. Bash & shell scripting

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

CS197U: A Hands on Introduction to Unix

Exercise Sheet 2. (Classifications of Operating Systems)

Introduction to Linux

Exploring UNIX: Session 3

COMP 4/6262: Programming UNIX

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

CSCI 2132 Software Development. Lecture 5: File Permissions

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

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

A shell can be used in one of two ways:

Introduction to Linux. Roman Cheplyaka

Quick Start Guide. by Burak Himmetoglu. Supercomputing Consultant. Enterprise Technology Services & Center for Scientific Computing

CSC209. Software Tools and Systems Programming.

Part 1: Basic Commands/U3li3es

I/O and Shell Scripting

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

CSE II-Sem)

Lab 4: Shell Scripting

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

The Linux Command Line & Shell Scripting

9.2 Linux Essentials Exam Objectives

CSE 303 Lecture 4. users/groups; permissions; intro to shell scripting. read Linux Pocket Guide pp , 25-27, 61-65, , 176

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

Introduction to UNIX Command Line

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

Hands-on Keyboard: Cyber Experiments for Strategists and Policy Makers

Lab 4: Bash Scripting

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Useful Unix Commands Cheat Sheet

LOG ON TO LINUX AND LOG OFF

Introduction to Supercomputing

commandname flags arguments

CISC 220 fall 2011, set 1: Linux basics

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Files

Lecture 3. Unix. Question? b. The world s best restaurant. c. Being in the top three happiest countries in the world.

The Unix Shell & Shell Scripts

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

Програмиранев UNIX среда

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Shell Programming (Part 2)

Conditional Control Structures. Dr.T.Logeswari

Bash scripting basics

CS246 Spring14 Programming Paradigm Notes on Linux

Introduction to Linux Workshop 1

Week 2 Lecture 3. Unix

PROGRAMMAZIONE I A.A. 2015/2016

Mills HPC Tutorial Series. Linux Basics I

CS Unix Tools & Scripting

Read the relevant material in Sobell! If you want to follow along with the examples that follow, and you do, open a Linux terminal.

CENG393 Computer Networks Labwork 1

Introduction to the UNIX command line

Computer Systems and Architecture

CSNB113: System Administration - 8 th Topic: Shell Scripts Programming I

SANJAY GHODAWAT POLYTECHNIC

unix intro Documentation

Shell Start-up and Configuration Files

Basic Linux (Bash) Commands

CSE 374 Midterm Exam 2/6/17 Sample Solution. Question 1. (12 points, 4 each) Suppose we have the following files and directories:

Linux Training. for New Users of Cluster. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Exercise 1: Basic Tools

Linux Shell Script. J. K. Mandal

Linux shell programming for Raspberry Pi Users - 2

Assignment 3, Due October 4

Introduction to Linux

Chapter 4. Unix Tutorial. Unix Shell

Perl and R Scripting for Biologists

Computer Systems and Architecture

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Multiple Choice - 42 Questions - 10 of 20%

UNIX shell scripting

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

d. 1 e. test: $a: integer expression expected

Introduction Into Linux Lecture 1 Johannes Werner WS 2017

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

Recap From Last Time:

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

BGGN 213 Working with UNIX Barry Grant

BASH Programming Introduction

Computer Systems and Architecture

Lab Working with Linux Command Line

Mills HPC Tutorial Series. Linux Basics II

CSE374 Winter 2016, Midterm Examination February 8, 2016 Please do not turn the page until the bell rings.

bash Scripting Introduction COMP2101 Winter 2019

Linux Refresher (1) 310/ Fourth Workshop on Distributed Laboratory Instrumentation Systems (30 October - 24 November 2006)

Transcription:

DAVE LIDDAMENT INTRODUCTION TO BASH @daveliddament

FORMAT Short lectures Practical exercises (help each other) Write scripts

LEARNING OBJECTIVES What is Bash When should you use Bash Basic concepts of Linux shells Running several commands together Writing scripts Home work: Useful commands to learn

WHAT IS BASH?

WHEN SHOULD YOU USE BASH?

HOW EXPERIENCED ARE YOU? Not at all, that s why I m here! [1] A bit, I ve been using Bash and I know the basics. [2] Very, I should be running the workshop! [3]

SECTION 1 - BASICS Structure of a command Getting help

ANATOMY OF A COMMAND command [option(s)] <arguments> [<optional arguments>]

ANATOMY OF A COMMAND command [option(s)] <arguments> [<optional arguments>]

ANATOMY OF A COMMAND command [option(s)] <arguments> [<optional arguments>]

EXAMPLE mkdir app/src

EXAMPLE mkdir app/src app/test target docs

EXAMPLE mkdir -p -m 0755 app/src app/test

OPTIONS THAT ARE FLAGS mkdir -p -m 0755 app/src app/test

OPTIONS THAT TAKE PARAMETERS mkdir -p -m 0755 app/src app/test

SHORT AND LONG OPTIONS -v --verbose -a --archive -D --append -l --links -L --copy-links

GETTING HELP man <command> man rsync <command> -h rsync -h <command> help rsync --help

HOW EXPERIENCED ARE YOU? Not at all, that s why I m here! [1] Please help others: A bit, I ve been using Bash and I know the basics. [2] Very, I should be running the workshop! [3]

PRACTICAL List files in a directory. ls List files in a directory showing file size, largest first. ls Show the date. date Show the date in format RFC 2822. date Count the number of lines in a file. wc

REVIEW 1 - BASICS Structure of a command Getting help

SECTION 2 - PERMISSIONS Why have them How to understand them The root user

WHY HAVE PERMISSIONS?

FILE PERMISSIONS USER, GROUP, OTHER ls -l -rw-r--r-- 1 dave staff 155 17 Jun 2015 readme.md -rwxr-xr-- 1 dave staff 155 17 Jun 2015 build drwxr--r-- 1 dave staff 578 17 Jun 2015 src

ROOT USER

HOW EXPERIENCED ARE YOU? Not at all, that s why I m here! [1] Please help others: A bit, I ve been using Bash and I know the basics. [2] Very, I should be running the workshop! [3]

PRACTICAL What groups are you a member of? whoami id List files in your current directory. Who can view and edit them? List files in /etc/ssh. Who can view and edit the files in here? Find a file that anyone can view but only root can edit. Fine a file that only root can view. What happens when you try and look at it. Use: cat <filename>

REVIEW 2 - PERMISSIONS Why have them How to understand them The root user

SECTION 3 - VARIABLES How to set them How to read them Using variables in commands

SETTING VARIABLES NAME=dave MESSAGE= hello world

READING VARIABLES echo $MESSAGE echo Here is a message from $NAME to you: $MESSAGE

READING VARIABLES 2 # Set up a variable DIRECTORY=/tmp/ # Following line will print nothing. No variable DIRECTORYfoo echo $DIRECTORYfoo # Following line will print /tmp/foo echo ${DIRECTORY}foo

VARIABLES IN COMMANDS dir=/tmp ls $dir

VARIABLES IN COMMANDS # Returns current user whoami # Assign user to variable me me=`whoami` # Print out message echo Your username is $me

VARIABLES IN COMMANDS echo The current directory is `pwd`

PRACTICAL Create variables to hold your first name and surname. Create a variable to hold the current time (use the date function) Print to screen Hello <first name> <last name>, the time is <time>

REVIEW 3 - VARIABLES How to set them How to read them Using variables in commands

SECTION 4 - CHAINING COMMANDS Introduction to piping Writing to files

PIPES # List all files in a directory ls # Count how many files in a directory ls wc -l # Give messages echo There are `ls wc -l` files in the directory `pwd`

REDIRECTING TO FILES # Write Hello to the file echo Hello > message.txt # Append Goodbye to the file greetings.txt echo Goodbye >> message.txt

PRACTICAL Look at the following commands. If there are 4 files in the directory what will the output be? ls > files.txt echo Number of files `cat files.txt wc -l` ls >> files.txt echo Number of files `cat files.txt wc -l` ls > files.txt echo Number of files `cat files.txt wc -l`

REVIEW 4 - CHAINING COMMANDS Introduction to piping Writing to files

SECTION 5 - CHANGING FLOW For loops If statements

FOR LOOPS # Assume we have a file months.txt of the year on each line: jan feb march # Run a for loop like this: for month in `cat months.txt` do echo $month done

IF STATEMENTS # Set up some variables: name1=dave # If statements like this: if [ $name == dave ] then echo Hello Dave fi

IF STATEMENTS # Set up some variables: age=21 # If statements like this: if [ $age -lt 37 ] then echo You look much older else echo I believe that fi

IF STATEMENTS [ -a FILE ] True if file exists [ A -eq B ] True if A == B [ A -ne B ] True if A!= B Lots more: http://tldp.org/ldp/bash-beginners-guide/ html/sect_07_01.html

PRACTICAL Experiment with for command Create file with days of week on each line Loop through each line and echo it out Play with if command Create simple if statement using string comparison Create simple if statement using integer comparison Create simple if statement to check if file exists

REVIEW 5 - CHANGING FLOW For loops If statements

SECTION 6 - WRITING A SCRIPT Hello World example Capturing arguments Write your own deployment script

FIRST SCRIPT #!/bin/bash echo Hello world # Run the script chmod a+x hello./hello

PASSING ARGUMENTS TO A SCRIPT #!/bin/bash echo You passed $# arguments to this script echo Argument 1: $1 echo Argument 2: $2 # Run the script./hello./hello foo./hello foo bar

PRACTICAL 1 Write a script that takes 1 argument (which is name) and echoes that back to the user Checks 1 argument has been passed to it. If it hasn t then print an error message and exit (use exit) If name is Apple then echo a message saying Thanks for hosting us Run scripts with different names and missing / too many arguments.

PRACTICAL 2 - DEPLOY SCRIPT Create a new directory. Within this directory create the following: directory called log (use mkdir) directory called deploy (use mkdir) directory called code (contains a clone of of https:// github.com/daveliddament/phptraining-phpunit- RomanNumerals) git clone https://github.com/daveliddament/phptraining- PHPUnit-RomanNumerals code

PRACTICAL 2 - DEPLOY SCRIPT Write a script that takes 1 argument which is the name of the tag that needs deploying. Checks 1 argument has been passed to it. If it hasn t then print an error message and exit (use exit) In the code directory checkout tag Copy code from code to deploy Append to log/deploy.log file an entry that includes time, user who ran the script and the tag that was deployed. Add a check that makes sure that the git tag exists (use grep). If it doesn t then report an error.

REVIEW 6 - WRITING A SCRIPT Hello World example Capturing arguments Write your own deployment script

HOMEWORK 1 - USEFUL COMMANDS tar grep sed find rsync

HOMEWORK 2 - SCRIPTS Write a script that takes a dump or your database. Include in the name the time the database was dumped in the format dbname-yyyymmdd-hhmmss.dump Write a script that generates a release note. It takes 2 git commits SHAs and generates a doc that contains only the commits between the 2 SHAs with messages that start Add. Generate various release notes for the RomanNumerals project.