Bash 101 Intro to Shell Scripting; Lightning

Slides:



Advertisements
Similar presentations
CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
Advertisements

EMT 2390L Lecture 4 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
Linux & Shell Scripting Small Group Lecture 4 How to Learn to Code Workshop group/ Erin.
1 UNIX essentials (hands-on) the directory tree running programs the shell (using the T-shell) → command line processing → special characters → command.
Introduction to UNIX/Linux Exercises Dan Stanzione.
Sydney Opera House. Week Three Agenda Administrative Issues Link of the week Review week two lab assignment This week’s expected outcomes Next lab assignment.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
An Introduction to Unix Shell Scripting
Grades Please hand in your homework Quizzes coming back today Current grade on back with missing assignments Anything missing can be turned in late There.
Linux Shell Programming Tutorial 3 ENGR 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Introduction to Unix – CS 21
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2014 Lecture 3 – I/O Redirection, Shell Scripts.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Lab 7 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Introduction to the bash Shell (Bourne-Again SHell)
Using Linux Kaya Oğuz Room: 310.
Bash vs. dash PLUG West PLUG North JP Vossen bashcookbook.com.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing.
Bash Scripting CIRC Summer School 2016 Baowei Liu 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.
CSE 374 Programming Concepts & Tools
CIRC Winter Boot Camp 2017 Baowei Liu
CSE 374 Programming Concepts & Tools
Department of Computer Engineering
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
CSE 374 Programming Concepts & Tools
Sydney Opera House.
The Linux Operating System
Shell Script Assignment 1.
CSE 303 Concepts and Tools for Software Development
Basic UNIX OLC Training.
Pepper (Help from Dr. Robert Siegfried)
The Linux Command Line Chapter 24
CSE 303 Concepts and Tools for Software Development
CST8177 Scripting 2: What?.
CSC 352– Unix Programming, Fall 2012
Bash Scripting CS 580U - Fall 2018.
The Linux Command Line Chapter 24
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Presentation transcript:

Bash 101 Intro to Shell Scripting; Lightning Updated: 2015-11-16 JP Vossen, CISSP bashcookbook.com http://www.jpsdomain.org/public/bash_101_lightning.pdf http://www.jpsdomain.org/public/bash_101_lightning.odp

What is a “shell script?” Fundamentally just a list of commands to run May use arguments, variables, various control logic and arithmetic to figure out what to run when bash is integer only, other shells may not be Plain text file Used on CLI only Builds on: The “Unix” tool philosophy The “Unix” everything-is-a-file philosophy

Why should I care? You can write new commands! Automation Save time & effort and make life easier E.g., if you always type in four commands to accomplish some task, type them once into an editor, add a “shebang” line and comments, save and set execute permissions. You now have a shell script! Automation cron Consistency & Reliability (Process) Documentation One-liners

How do I get started? Fire up an editor #!/bin/bash - echo 'Hello world, my first shell script!' chmod +r script bash 'help' command! 'help set' vs. 'man set' basic operation is invocation = you run stuff Most of a Linux system is run by shell scripts. They are everywhere, find some and read them. Everything in /etc/init.d/ for i in /bin/*; do file $i | grep -q 'shell script' && echo $i; done # You will be surprised!

Positional Parameters “Main” script: $0 $1 $2 $3 myscript foo bar baz $# = number of parms $* = “$1 $2 $3” # a single string of all parms, separated by first character of $IFS (Internal Field Separator) “$@” = “$1” “$2” .. “$N” # For re-use later Reset inside a function $1 = first arg to function, not script But use $FUNCNAME instead of $0

Quotes The shell re-writes the line White space is a delimiter! Quoting Use ' ' unless you are interpolating $variables, then use " " echo 'foo' echo "$foo" grep 'foo' /path/to/file grep "$regex" /path/to/file Except when it's not. Can make your head hurt.

Variables USE GOOD NAMES!!! No $ or spaces around = when assigning: foo='bar' foo="bar$baz" $ when referencing value: echo "$foo" Append: foo="$foo bar" Needs ${} if variable followed by [a-zA-Z_0-9] foo="foo $bar baz" # OK foo="foo${bar}baz" # $bar needs ${}

debugging DO NOT CALL YOUR TEST SCRIPT 'test'! PS4='+xtrace $LINENO: ' First character is duplicated to show nesting level, that's why I have '+' there $LINENO should be in the default PS4 prompt! bash -n path/to/script # gross syntax check bash -x path/to/script # run-time debugging set -x & set +x # debug on / off set -v & set +v # verbose on / off

URLs, Wrap-up and Q&A URLs: Questions? TONS of resources: http://www.bashcookbook.com/bashinfo/ These slides: http://www.jpsdomain.org/public/bash_101_lightning.pdf or http://www.jpsdomain.org/public/bash_101_lightning.odp Bash vs. Dash: http://www.jpsdomain.org/public/2008-JP_bash_vs_dash.pdf and aptitude install devscripts then use checkbashisms The sample script: http://www.jpsdomain.org/public/cdburn STDIN, STDOUT, STDERR: http://en.wikipedia.org/wiki/Standard_streams Revision Control: http://www.jpsdomain.org/public/Revision_Control_for_the_Rest_of_Us.pdf and (older) http://www.jpsdomain.org/public/PANTUG_2007-06-13_appd=Revision_Control=JP.pdf Windows Shell Scripting (cmd.exe): http://www.jpsdomain.org/windows/winshell.html BASH Prompt HOWTO: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/index.html Cygwin: http://www.cygwin.com/ UnxUtils: http://unxutils.sourceforge.net/ GNU Win32 ports: http://sourceforge.net/projects/gnuwin32/ Win32 Perl http://www.activestate.com/solutions/perl/ Mac; this is awesome http://examples.oreilly.com/0636920032731/Terminal_Crash_Course.pdf Questions? I'm on the PLUG list... jp@jpsdomain.org