Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.

Slides:



Advertisements
Similar presentations
Learning Unix/Linux Bioinformatics Orientation 2008 Eric Bishop.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Guide To UNIX Using Linux Third Edition
Understanding the Basics of Computational Informatics Summer School, Hungary, Szeged Methos L. Müller.
Shell Script Examples.
Using Macs and Unix Nancy Griffeth January 6, 2014 Funding for this workshop was provided by the program “Computational Modeling and Analysis of Complex.
Chapter 15 Introductory Bash Programming
Introduction to Shell Script Programming
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
An Introduction to Unix Shell Scripting
2INC0 Operating Systems Introduction to Linux
Unix Tutorial for FreeSurfer Users. Helpful To Know FreeSurfer Tutorial Wiki:
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
Unix Tutorial for FreeSurfer Users. Helpful To Know FreeSurfer Tutorial Wiki:
Lecture 4  C Shell Scripts(Chapter 10). Shell script/program  Shell script: a series of shell commands placed in an ASCII text file  Commands include.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
Linux Commands C151 Multi-User Operating Systems.
LING 408/508: Programming for Linguists Lecture 5 September 9 th.
A Brief Overview of Unix Brandon Bohrer. Topics What is Unix? – Quick introduction Documentation – Where to get it, how to use it Text Editors – Know.
Dr. Sajib Datta Jan 16,  The website is up.  Course lectures will be uploaded there ◦ Check regularly for assignments and update.
 Prepared by: Eng. Maryam Adel Abdel-Hady
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
CIRC Summer School 2016 Baowei Liu
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
GRID COMPUTING.
Intro to shell scripting
Development Environment Basics
CIRC Winter Boot Camp 2017 Baowei Liu
Department of Computer Engineering
Intro to shell scripting
Prepared by: Eng. Maryam Adel Abdel-Hady
Chapter 9 Shell Programming
CIRC Summer School 2017 Baowei Liu
Andy Wang Object Oriented Programming in C++ COP 3330
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CIRC Winter Boot Camp 2017 Baowei Liu
Part 1: Basic Commands/Utilities
CSE 390a Lecture 5 Intro to shell scripting
Some Linux Commands.
C151 Multi-User Operating Systems
Shell Scripting March 1st, 2004 Class Meeting 7.
CIRC Summer School 2017 Baowei Liu
The Linux Operating System
Shell Script Assignment 1.
Intro to shell scripting
CSE 303 Concepts and Tools for Software Development
Introduction to UNIX.
LING 408/508: Computational Techniques for Linguists
The Linux Command Line Chapter 24
Intro to shell scripting
Persistent shell settings; intro to shell scripting
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
Intro to shell scripting
Intro to shell scripting
CSE 303 Concepts and Tools for Software Development
Lab 4: Introduction to Scripting
Intro to shell scripting
Intro to shell scripting
Intro to shell scripting
Introduction to Bash Programming, part 3
Intro to shell scripting
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu

About the Class  Lectures along with example demonstrations. Project session for hands- on experience. Have a project?  Trying examples and doing projects on Bluehive  Lectures along with example demonstrations. Project session for hands- on experience. Have a project?  Trying examples and doing projects on Bluehive

Bash Script  Unix/Linux commands in a text file – batch mode  An executable program to be run by the shell (Bash)  Unix/Linux commands in a text file – batch mode  An executable program to be run by the shell (Bash)

Model for Modern OS Users Shell Kernel Hardware

Linux and Shells

 Easy to edit: long command lines  Record & reusable: many command lines or options  Flexible and Efficient: Run with different arguments.  Easy to use with job scheduler  Easy to edit: long command lines  Record & reusable: many command lines or options  Flexible and Efficient: Run with different arguments.  Easy to use with job scheduler Bash Script VS. Command Lines

Bash Script VS. Other Script Language  Easy to program: the commands and syntax are exactly the same as those directly entered at the command line. Quick start.  Only need a Linux system to run  Slow run speed comparing with other programming languages  Not easy for some tasks like floating point calculations or math functions  Easy to program: the commands and syntax are exactly the same as those directly entered at the command line. Quick start.  Only need a Linux system to run  Slow run speed comparing with other programming languages  Not easy for some tasks like floating point calculations or math functions

 Manipulate files: handle large number of files, change individual parameters in data files, etc.  Wrap up programs: pipeline different tools with different options/flags  Manipulate files: handle large number of files, change individual parameters in data files, etc.  Wrap up programs: pipeline different tools with different options/flags Scenarios Using Bash Script

To Write a Bash Script  An editor: vi emacs, nano,….  Some Linux commands  Set executable permission  Specify interpreter as bash: #!/bin/bash  Comments: # (single line)  An editor: vi emacs, nano,….  Some Linux commands  Set executable permission  Specify interpreter as bash: #!/bin/bash  Comments: # (single line)

Some Linux Commands  ls: list directory contents  cd: change directory  man: manual  echo  ls: list directory contents  cd: change directory  man: manual  echo

Linux Command echo  Display a line of text  Example: echo hello world  “…” or ‘…’  Display a line of text  Example: echo hello world  “…” or ‘…’

File permissions  Three scopes and permissions  Bash script has to have execute permission to allow the operating system to run it.  Check permissions: ls –l  Change permissions: chmod +x filename  Three scopes and permissions  Bash script has to have execute permission to allow the operating system to run it.  Check permissions: ls –l  Change permissions: chmod +x filename

Day 1 Examples  ssh to bluehive:  Create a directory for this class: mkdir folderName  cp /public/bliu17/Teaching/Bash/shareFiles.sh. ./shareFiles.sh Day1  ssh to bluehive:  Create a directory for this class: mkdir folderName  cp /public/bliu17/Teaching/Bash/shareFiles.sh. ./shareFiles.sh Day1

Bash Variables  Create a variable: name=value  No data type  No need to declare but can be declared with “declare command”  No white space allowed before and after =  Use $ to refer to the value: $name  Name cannot start with a digit.  Create a variable: name=value  No data type  No need to declare but can be declared with “declare command”  No white space allowed before and after =  Use $ to refer to the value: $name  Name cannot start with a digit.

Environment Variables  env  $SHELL  $PATH  $LD_LIBRARY_PATH  $RANDOM  $USER  $PWD  env  $SHELL  $PATH  $LD_LIBRARY_PATH  $RANDOM  $USER  $PWD

Variable Value  Assign value: a=2  Pass value: b=$a  Display value: echo $a  Strong quoting & weak quoting  Assign value: a=2  Pass value: b=$a  Display value: echo $a  Strong quoting & weak quoting

Assign Variable Value  Parameter expansion ${}  Command Substitution: $(), or `  Arithmetic expansion: $(( … ))  Parameter expansion ${}  Command Substitution: $(), or `  Arithmetic expansion: $(( … ))

Parameter and Parameter Expansion  Parameter – an entity store value ( e.g. variable)  ${parameter}  Indirection ${!parameter}  Parameter – an entity store value ( e.g. variable)  ${parameter}  Indirection ${!parameter}

Arithmetic Expression  Arithmetic operators: + - * /  Integer only  Arithmetic Expansion ((…))  White spaces are not important  Arithmetic operators: + - * /  Integer only  Arithmetic Expansion ((…))  White spaces are not important

Basic calculator: bc  An arbitrary precision calculator language  Simple usage: echo $a+$b | bc  Can use math library: echo “s(0.4)” | bc -l  bc –l <<< “s(0.4)”  An arbitrary precision calculator language  Simple usage: echo $a+$b | bc  Can use math library: echo “s(0.4)” | bc -l  bc –l <<< “s(0.4)”

Command Substitution  $() is nestable echo $(echo $(ls)) echo “$(echo “$(ls)”)”  cannot be $(( command ))  `` cannot be directly nesting echo `echo \`ls\``  $() is nestable echo $(echo $(ls)) echo “$(echo “$(ls)”)”  cannot be $(( command ))  `` cannot be directly nesting echo `echo \`ls\``

Shell Expansions Review  Parameter Expansion: $variable, ${variable}  Arithmetic Expansion: $(( expression ))  Command Substitution: $() or ``  Parameter Expansion: $variable, ${variable}  Arithmetic Expansion: $(( expression ))  Command Substitution: $() or ``

Loop Constructs: for loop  Basic Syntax for arg in [list] do ….. done  [list]: 1. Command Substitution: `ls` 2. Arithmetic Expansion 3. Brace Expansion (string or integer): {1..5}  Basic Syntax for arg in [list] do ….. done  [list]: 1. Command Substitution: `ls` 2. Arithmetic Expansion 3. Brace Expansion (string or integer): {1..5}

for loop –Arithmetic Expansion  Basic Syntax for (( expr1; expr2; expr3 )) do … done  Examples:  White space are not important for Arithmetic Expansion  Basic Syntax for (( expr1; expr2; expr3 )) do … done  Examples:  White space are not important for Arithmetic Expansion

Project  Copy the Project folder to your own directory:./shareFiles.sh Project  Write a Bash script to count how many files there are in files/  Key tech: editor, chmod, command substitution, for loop, arithmetic expansion, echo  Copy the Project folder to your own directory:./shareFiles.sh Project  Write a Bash script to count how many files there are in files/  Key tech: editor, chmod, command substitution, for loop, arithmetic expansion, echo