2006-Jan-231 Shell Scripts Jacob Morzinski

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.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Scripting Languages and C-Shell. What is a scripting language ? Script is a sequence of commands written as plain text and run by an interpreter (shell).
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
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.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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.
An Introduction to Unix Shell Scripting
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 8 Shell.
Shell Features CSCI N321 – System and Network Administration Copyright © 2000, 2005 by Scott Orr and the Trustees of Indiana University.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
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
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
CS465 - UNIX The Bourne Shell.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
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.
CSC 352– Unix Programming, Spring 2015 April 28 A few final commands.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Linux+ Guide to Linux Certification, Second Edition
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
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.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
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.
Shell Control Structures
Shell Features CSCI N321 – System and Network Administration
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
CIRC Summer School 2017 Baowei Liu
CIRC Winter Boot Camp 2017 Baowei Liu
Part 1: Basic Commands/Utilities
Lecture 9 Shell Programming – Command substitution
CSE 303 Concepts and Tools for Software Development
Pepper (Help from Dr. Robert Siegfried)
CSC 352– Unix Programming, Spring 2016
CSE 303 Concepts and Tools for Software Development
Shell Control Structures
Shell Control Structures
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

2006-Jan-231 Shell Scripts Jacob Morzinski

2006-Jan-232 How did you hear about the class? 1. Anne Hunter's list posting 2. the slideshow in the Infinite 3. the goldenrod paper guide

2006-Jan-233 Why use shell scripts? They simplify your life. Store complicated commands in a script, to save effort and reduce typing errors. athrun ops rdesktop -ujmorzins -f –N \ -a16 vash.mit.edu:4884 ldapsearch -u -LLL -h ldap.mit.edu \ -b dc=mit,dc=edu uid=jmorzins 2>&1 | \ egrep -v '^(SASL[ /]|objectClass:)'

2006-Jan-234 Basic: setting up a script 1. Edit the file.  Emacs, vi, jedit, gedit, whatever you prefer.  Run with: bash /path/to/file 2. (Make Unix happy.)  Make sure the file begins with #!/bin/bash  chmod a+rx file  Make sure file is in your $PATH (or $path ). 3. Run the script.  file (or /path/to/file )

2006-Jan-235 Basic: making Unix happy The #! line tells Unix what program to run the file through. We want to use bash. The chmod command tells Unix to make the file readable and executable. Your $PATH is a list of directories that Unix looks through, when trying to find a program whose name matches the word you just typed at the prompt.

2006-Jan-236 Basic: setting the PATH If your shell is sh-based (like bash): PATH="$PATH":/mit/jmorzins/bin export PATH Sometimes you can combine these: export PATH="$PATH":/mit/jmorzins/bin If your shell is csh-based (like tcsh): set path = ( $path:q /mit/jmorzins/bin )

2006-Jan-237 Basic: fixed-text example $ cat user1 #!/bin/sh vos exa user.jmorzins kvno jmorzins $./user1 user.jmorzins APHRODITE.MIT.EDU /vicepb Creation Wed Aug 30 09:50: kvno = 20

2006-Jan-238 Basic: variables allow flexebility $ cat user2 #!/bin/sh vos exa user."$1" kvno "$1" $./user2 boojum user.boojum COCYTUS.MIT.EDU /vicepb Creation Sun Aug 10 00:08: kvno = 78

2006-Jan-239 Review of basic knowledge At this point, you know:  How to set up simple shell scripts  How to pass one word into the script, to have the script run commands using that word ( "$1" ).

2006-Jan-2310 What the shell really does You: type a command The shell does:  Variable substitution (interpolation)*  File name substitution (globbing)*  Parses the line into arguments*  Finally, runs the command  * Can be influenced by quoting.

2006-Jan-2311 Runs the command Searches through $PATH to find the executable file, if not an absolute path. You can run a sequence of commands if you separate them with semicolons ( ; ) Commands can return an exit status.  Status = 0 means success  Status != 0 means some sort of failure You can use “ && ” and “ || ” to chain commands in an “and” or “or” fashion.

2006-Jan-2312 Parses the line into arguments The shell splits the command line on white space. The first word is the command name The rest of the words are “arguments,” and are passed to the command.

2006-Jan-2313 File globs If files match your pattern, the file names are filled into the command line. PatternMeaning *Match zero or more characters ?Matches just one character [abc] [x-z]Match just one character, from a range [!0-9]Match one character, that is not in the range

2006-Jan-2314 Variable substitution A variable can be a word, a number, or a special symbol:  Some symbols: $$, $?, $*,  Numbers 0, and 1-9: $0, $1, …, $9  Words: $PATH, $var, $longer_name Note that variable substitution happens before file substitution and before the commandline is split into words.

2006-Jan-2315 Quoting Single quote: '...'  The shell ignores all enclosed characters Double quote: "..."  The shell ignores most enclosed charactecters  The shell does interpret $, `, and \ Backtick: `...`  Run a command, insert the output Backslash: \  Special treatment for the following character.

2006-Jan-2316 Input/Output redirection Commands usually print to standard output, sometimes print to standard error. Commands read from standard input. Stdin is 0 Stdout is 1 Stderr is 2 Higher numbers are possible

2006-Jan-2317 Table of I/O redirection cmd1 | cmd2 pipe output from cmd1 into cmd2 commad > file put text into file, truncate file command >> file append text to file command < file read from file command 2>&1 send stderr to where stdout is command 1>&2 send stdout to where stderr is cmd1 `cmd2` capture output of cmd2 for command-line of cmd1

2006-Jan-2318 Setting Variables Setting variables  var=value  ENVVAR=value  export ENVVAR Note:  variables don’t affect parent shells  only environmental variables affect child shells

2006-Jan-2319 Using variables $* - list of all command-line parameters. Bad. – Good list of all command-line parameters. “$1”, “$2”, …, “$9” – individual command line parameters shift – remove “$1”, shift all other variables down one set – set new “$1”, “$2”, etc variables

2006-Jan-2320 Utilities – test test – runs a test, returns true or false  Good for if statements, or while loops  also called “[”, but in that case it must be used with a matching “]” -fis a file -dis a directory -zis zero length -nis non-zero length =, !=compare strings -a, -oand, or !negate next part -gt, -lt -ge, -le compare numbers

2006-Jan-2321 Utilities – grep Pattern searching  Uses regular expressions  Powerful, but hard to learn a letter “a”. any one char * zero or more… [abc] [^abc] char in a range, or inverted ^ start of line $ end of line \(z\) \1 save a match, re-use a match

2006-Jan-2322 Shell Loops - if if expr ; then cmd1 ; else cmd2 ; fi cmd1 and cmd2 can be complex the else can be omitted if [ x“$var” = x“yes” ] ; then echo good ; fi

2006-Jan-2323 Shell Loops – while, for while expr ; do cmd ; done for var in a b c d e ; do cmd ; done

2006-Jan-2324 Shell functions name () { cmd1 ; cmd2 ; } Act like mini-shell scripts. Can set variables in the current shell.