Shell Programming. An example u tr abcdefghijklmnopqrstuvwxyz \ thequickbrownfxjmpsvalzydg file2 –encrypts file1 into file2 u record this command with.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
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
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.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
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.
Linux+ Guide to Linux Certification, Third Edition
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
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.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
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.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
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. 2 What is a Shell Script? A Text File With Instructions Executable  Why shell script? Simply and quickly initiate a complex series of tasks or a.
(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.
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.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Linux+ Guide to Linux Certification, Second Edition
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
#!/bin/sh 2002/07/26 Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell.
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.
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
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.
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
CSC 352– Unix Programming, Fall 2012
What is Bash Shell Scripting?
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Linux Shell Script Programming
Shell Control Structures
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Chapter 5 Bourne Shells Scripts
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Shell Programming

An example u tr abcdefghijklmnopqrstuvwxyz \ thequickbrownfxjmpsvalzydg file2 –encrypts file1 into file2 u record this command with a script file –myencrypt #!/bin/sh tr abcdefghijklmnopqrstuvwxyz \ thequickbrownfxjmpsvalzydg –mydecrypt #!/bin/sh tr thequickbrownfxjmpsvalzydg \ abcdefghijklmnopqrstuvwxyz

An example u chmod the files to be executable % chmod u+x myencrypt mydecrypt u run them as normal commands %./myencrypt file2 %./mydecrypt file3 % diff file1 file3

Shell Scripts (1) u A Shell Script is simply a text file with UNIX commands in it u Shell Scripts usually begin with a #! and a shell name –For example:#!/bin/sh –If they do not, the user's current shell will be used u Any UNIX command can go in a Shell Script –these commands are executed in order or in the flow determined by the control statements –much like a program in a language like Pascal, C or Java u Different shells have different control structures –the #! line is very important –we will generally write shell scripts with the Bourne (sh) shell

Shell Scripts (2) u Why Write Shell Scripts? –to avoid repetition v if you do a sequence of steps with standard UNIX commands over and over, why not just type one command? –to automate difficult tasks v many commands have subtle and difficult options which you don't want to figure out every time

Command Line Arguments (1) u Shell Scripts would not be much useful if we could not pass arguments to them on the command line u Shell Script arguments are "numbered" from left to right –Inside the script refer to each argument by this number with a $ in front v $1 - first argument after command v $2 - second argument after command v... up to $9 –These are called Positional Parameters and can be used much like shell variables

Command Line Arguments (1) u Example 1: get a particular line of a file –Write a command with the format: getlineno linenumber filename #!/bin/sh head -$1 $2 | tail –1 u Other variables related to arguments v $0 name of the command running v $* All the arguments (even if there are more than 9) v $# the number of arguments

Command Line Arguments (2) u Example 2: print the oldest files in a directory #! /bin/sh # oldest -- examine the oldest parts of a directory HA=$1 shift ls -lt $* | tail +2 | tail $HA u The shift command shifts all the arguments to the left –$1 = $2, $2 =$3, $3 = $4,... –$1 is lost (but we have saved it in $HA) –useful when there are more than 9 arguments u The "tail +2" command removes the first line (why do this?)

Bourne Shell Variables (1) u There are three basic types of shell variables –Positional variables v $1, $2, $3,..., $9 -- we've seen these –Keyword variables v like PATH, HA, and anything else we may define –Special variables v $*, $#-- all the arguments, the number of the arguments v $$-- the process id of the current shell v $?-- return value of last foreground process to finish -- more on this one later v and others you can find out about with man sh

Bourne Shell Variables (2) u Bourne shell variables are much like variables in a standard programming language –Examples: PATH=$PATH:$HOME/bin HA=$1 PHRASE="House on the hill" v if the value you wish to assign has spaces, RHS (right hand side) must be quoted v if you want sub-shells and commands to be able to look at these variables, you must export them export PATH HA PHRASE Note: no space around =

Bourne Shell Variables (3) u It is also sometimes useful to do numerical computations with shell variables –like everything else in UNIX this is done with a command: expr –expr evaluates very simple expressions of constants –For example: obelix[1]% expr obelix[2]% expr \( \) \* 5 % 6 5 –Results go to standard output v how do you use the results? note: all operands and operators are separated by spaces note: all special chars must be quoted or escaped

Bourne Shell Variables (4) u shell variables are expanded first, then the operands are given to the shell obelix[3]% a=5 obelix[4]% expr $a u Use results of expr with backquotes: `...` obelix[5]% a=`expr \( $a + 1 \)` obelix[6]% echo $a 6 u expr supports the following operators: –arithmetic operators: +,-,*,/,% –comparison operators: =, > –boolean/logical operators: &, | –parentheses: (, ) –precedence is the same as C, Java

Control Statements (1) u Control statements control the flow of execution in an imperative programming language –this is how Java, C, C++,.... and Shells work –non-imperative languages like lisp, ML, Prolog, Miranda,... may work differently u Without control statements execution within a shell scripts executes one statement after the other –each statement may be quite complicated and may invoke other UNIX commands (or even the same command...) u The three most common types of control statements: –conditionals: if/then/else, switch, case,... –loop statements: while, for, repeat, do,... –branch statements: subroutine calls (good), goto (bad)

Control Statements (2) u Both loops and conditions generally "test" something –essentially comes down to a binary decision u In a Bourne shell script, the only thing you can test is whether or not a command is "successful" –every well behaved command returns back a return code v 0 if it was successful v non-zero if it was unsuccessful (actually ) u examine the return code of the previous UNIX command executed in a shell with $? obelix[7]% grep “bma" < /dev/null obelix[8]% echo $? 1 obelix[9]% echo “bma" | grep “bma" obelix[10]% echo $? 0

Conditional (1) The if statement u Simple form: if decision_command_1 then command_set_1 fi u Example: if grep “bma“ $1 >/dev/null then echo -n "It\'s" echo "there" fi grep returns 0 if it finds something returns non-zero otherwise redirect to /dev/null so that "intermediate" results do not get printed

Conditional (2) u Else form: if decision_command_1 then command_set_1 else command_set_2 fi u Example: if grep "UNIX" myfile >/dev/null then echo "UNIX occurs in file" else echo "UNIX does not occur in file" fi The indentation used in these examples is "optional" (as in most programming languages) Good indentation is very highly recommended Required for cs211 assignments

Conditional (3) u Elif form: if decision_command_1 then command_set_1 elif decision_command_2 command_set_2... fi u Example: if grep "UNIX" myfile >/dev/null then echo "UNIX occurs in file" elif grep "DOS" myfile >/dev/null echo “Unix does not occur, but DOS does" else echo “nobody is there" fi

Conditional (4) u Alternatively, statements can be separated by a semicolon (;) –For example: if grep "UNIX" myfile; then echo "Got it"; fi –This actually works anywhere in the shell. v E.g. obelix[11]% cwd=`pwd`; cd $HOME; ls; cd $cwd

Conditional (5) u Sometimes it is useful to have a command which does "nothing" u The : (colon) command in UNIX does nothing besides having its parameters expanded (and ignored). –it returns a return code of 0 u Example: #!/bin/sh # pgrep: a polite grep if grep $* then : else echo "Sorry, but '$1' was not there..." fi

The test Program (1) u File tests: test –f file v does file exist and is not a directory? test –x file v does file exist and is executable? test –s file v does file exist and is longer than 0 bytes? test –z string v is string of length 0 (i.e., the null string)? v See man test(1) for more options u For example you can write: #!/bin/sh # saferm if test –f $1 then mv $1 $1.`date '+%Y-%M-%d:%H:%M.%S'` fi gives the date as :9:30.23

The test Program (2) u String tests –test can compare strings test string1 = string2v does string1 equal string2? test string1 != string2v does string1 not equal string2? –For example, you might do something different depending upon your TERM variable: if test $TERM = vt100; then stty erase '^?' resize elif test –z $TERM; then stty erase '^H' fi stty configures various keys on the keyboard (you can look up details)

The test Program (3) u Integers can also be compared –use –eq, -ne, -lt, -le, -gt, -ge u For example: #!/bin/sh # smallarg: print the smallest line args if test $# -eq 1; then echo $1 elif test $# -gt 1; then A=$1 shift B=`smallarg $*` if test $A -le $B; then echo $A else echo $B fi Recursion: Cooooool

The test Program (4) u The test program has an alias as [ –add in a matching ] at the end –this is supposed to be a bit easier to read #!/bin/sh # smallarg: print the smallest line args if [ $# -eq 1 ]; then echo $1 elif [ $# -gt 1 ]; then A=$1; shift B=`smallarg $*` if test $A -le $B; then echo $A else echo $B fi

The test Program (1) u File tests: test –f file v does file exist and is not a directory? test –x file v does file exist and is executable? test –s file v does file exist and is longer than 0 bytes? test –z string v is string of length 0 (i.e., the null string)? v See man test(1) for more options u For example you can write: #!/bin/sh # saferm if test –f $1 then mv $1 $1.`date '+%Y-%M-%d:%H:%M.%S'` fi gives the date as :9:30.23

The test Program (2) u String tests –test can compare strings test string1 = string2v does string1 equal string2? test string1 != string2v does string1 not equal string2? –For example, you might do something different depending upon your TERM variable: if test $TERM = vt100; then stty erase '^?' resize elif test –z $TERM; then stty erase '^H' fi stty configures various keys on the keyboard (you can look up details)

The test Program (3) u Integers can also be compared –use –eq, -ne, -lt, -le, -gt, -ge u For example: #!/bin/sh # smallarg: print the smallest line args if test $# -eq 1; then echo $1 elif test $# -gt 1; then A=$1 shift B=`smallarg $*` if test $A -le $B; then echo $A else echo $B fi Recursion: Cooooool

The test Program (4) u The test program has an alias as [ –add in a matching ] at the end –this is supposed to be a bit easier to read #!/bin/sh # smallarg: print the smallest line args if [ $# -eq 1 ]; then echo $1 elif [ $# -gt 1 ]; then A=$1; shift B=`smallarg $*` if test $A -le $B; then echo $A else echo $B fi

Command Line Input u The read command reads one line of input from the terminal and assigns it to variables give as arguments u Syntax read var1 var2 var3... v action: reads a line of input from standard input v assign var1 to first word, var2 to second word,... v the last variable gets any excess variables on the line u Example: obelix[11]% read X Y Z Here are some words as input obelix[12]% echo $X Here obelix[13]% echo $Y are obelix[14]% echo $Z some words as input

The While Loop (1) u While loops repeat statements while the return code of some UNIX command is 0 u The syntax is: while decision command do command_set done u Example 1: #!/bin/sh # print numbers from 1 to $1 COUNT=1 while [ $COUNT –le $1 ]; do echo –n $COUNT " " COUNT=`expr $COUNT + 1` done echo

The While Loop (2) u Example 2: –a more conventional approach to finding the smallest argument #!/bin/sh # smallarg: print the smallest line args smallest=$1 shift while [ $# -gt 0 ]; do if [ $1 –lt $smallest ]; then smallest=$1 fi shift done echo $smallest

For Loops (1) u For loops allow the repetition of a command for a specific set of values –E.g.: each argument on a line, each file in a set of files u Unlike many programming languages, for loops in shell script are not numeric or counted –we can use a while loop for this u Syntax: for var in value1 value2... do command_set done –command_set is executed with each value of var (value1, value2,...) in sequence

For Loops (2) u For Loop Example 1: #!/bin/sh # timestable – print out a multiplication table echo –n "Which times table do you want?" read Table for i in do Value=`expr $i \* $Table` echo $i x $Table = $Value done

For Loops (3) u For Loop Example 2: #!/bin/sh # file-poke – tell us stuff about files for file in $*; do echo –n "$file " if [ -s "$file" ]; then echo is full. else echo is empty. fi done –command goes through all files on the command line v these could be from filename expansion – we might type obelix[16]% file-poke * v Inside file-poke, $* will consist of a list of all files in the current directory

For Loops (4) u For Loop Example 3: #!/bin/sh # findcmd – show where command $1 is found in $PATH for prog in $*; do for dir in `echo $PATH | tr ':' ' '`; do if [ -x $dir/$prog ]; then echo $dir/$prog fi done PATH is : separated convert to space separated Note: doubly nested loop

Piping Input to a Loop u The standard input can be piped into a while or for loop –take a look at the output from the who command: obelix[15]% who watt ttyp3 Aug 27 11:30 (aquarium.scl.csd.uwo.ca) watt ttyp4 Sep 30 21:19 (diamond.admin.csd.uwo.ca) mwg ttyp2 Oct 6 16:57 (heffalump.csd.uwo.ca) –Now write a command to make this output "friendly": #!/bin/sh who | while read user tty month date time therest do echo –n $user is on $tty and has been since $month $date at $time. if [ ! –z "$therest" ]; then echo " $user is connected from $therest" else echo fi done Pipe output of who into the while loop read reads one line at a time from the standard input returns non-zero user code at end-of-file

Piping Output from a loop u The output from a loop can also be piped into the next command u For example: #!/bin/sh # findcmd2 – show where command $1 is found in $PATH # – and print extended information about it for prog in $*; do for dir in `echo $PATH | tr ':' ' '`; do if [ -x $dir/$prog ]; then echo $dir/$prog fi done done | while read cmd; do ls –l $cmd done; Output of for loop is piped into while loop, where it is read, one line at a time

Piping In and Out of a Loop u A more complex example: #!/bin/sh # lookfor – watch who output for specific users who | while read user tty mon dt year therest; do for watch in $*; do if [ $watch = $user ]; then echo $watch is on fi done done | sort | uniq –c | sed –e 's/ *\([0-9][0-9]*\) \(.*\)/\2 \1 times/' make lines unique but include count grab count into \1 re-order for printout