This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group 2013. Module 9 Basic Scripting.

Slides:



Advertisements
Similar presentations
Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
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.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison.
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Guide To UNIX Using Linux Third Edition
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Linux & Shell Scripting Small Group Lecture 4 How to Learn to Code Workshop group/ Erin.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Lab 8 Shell Script Reference:
Shell Control Structures CSE 2031 Fall August 2015.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Introduction to Shell Script Programming
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.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
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 ?
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Chapter Three The UNIX Editors.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
Sed. Class Issues vSphere Issues – root only until lab 3.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Lab 8 Overview Apache Web Server. SCRIPTS Linux Tricks.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
If condition1 then statements elif condition2 more statements […] else even more statements fi.
Group, group, group One after the other: cmd1 ; cmd2 One or both: cmd1 && cmd2 Only one of them: cmd1 || cmd2 Cuddling (there):( cmd1 ; cmd2 ) Cuddling.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
Linux Administration Working with the BASH Shell.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Chapter 3: Mastering Editors Chapter 3 Mastering Editors (Emacs)
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
Department of Computer Engineering
Chapter 9 Shell Programming
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Guide To UNIX Using Linux Third Edition
Shell Programming (ch 10)
Writing Shell Scripts ─ part 3
Agenda Control Flow Statements Purpose test statement
The Linux Command Line Chapter 24
Introduction to Bash Programming, part 3
The Linux Command Line Chapter 24
Presentation transcript:

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Module 9 Basic Scripting

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Exam Objective 3.3 Turning Commands into a Script Objective Summary – Basic Text Editing – Basic Shell Scripting

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Text Editors

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group A script is a sequence of commands If you can type it on the command line, it can be scripted You can branch based on tests and loop over collections This is all available from the command line, too Scripts are repeatable and consistent and free you to do more interesting work

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Editors Scripts are text files LibreOffice will add formatting, making it unusable nano and vi / vim are popular editors Of the two, nano is far easier to use

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group nano Type as normal, use arrow keys to move around Most commands are Control + another character Context sensitive commands are at the bottom of the screen, e.g. ^X to exit, ^W to find ^G gets you help anywhere

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group shebang

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group #! #! is the shebang (hash + bang) Tells the kernel what shell interpreter to use to run the script Must be on the first line #!/bin/sh #!/usr/bin/ruby Lets you run the script with./script no matter which shell the user has

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Working with Variables

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Variables Temporary storage of data in memory Assigning (no $): – FOO=“hello” – CWD=`pwd` – BAR=“Hello $NAME” Using (need a $): – $FOO – echo “Hello $NAME”

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Special Variables $1..$9 are the arguments to the script./test.sh hello there # $1=“hello” $2=“there” $? is the exit code of the last command to be run use “exit 1” to exit your own script with error code 1

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Working with Conditionals

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Conditionals if something; then do this fi if something; then do this elif something else; then do that else try this fi # something returns an exit code. If it is 0, then “do this” will be executed up until fi # alternative syntax, allows for multiple tests and a default option if you want

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group tests test –f /tmp/foo # test if file exists test ! –f /tmp/foo # test if file doesn’t exist test $A –eq 1 # is $A = 1 (numeric) test “$B” = “Hello” # string comparison test $A –lt 10 # $A < 10? alternatively, [ is the same as test: if test –f /tmp/foo; then # it works if [ -f /tmp/foo ]; then # looks much nicer!

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Case case ”$GREETING" in hello|hi) echo "hello yourself" ;; goodbye) echo "nice to have met you" echo "I hope to see you again" ;; *) echo "I didn't understand that" esac

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Loops

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group The for loop Operates over a fixed set of items or a glob NAMES=“Alice Bob Charlie” for N in $NAMES; do echo “Hello $N” done for FILE in *; do ls –l $FILE done

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group While loops Tests the loop condition each time while [ ! –f /tmp/foo ]; do try_to_make_foo # makes /tmp/foo on success sleep 1 # wait 1 sec done Used when the number of loops is unknown or changes each loop