Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
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
Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
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.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
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.
Lab 8 Shell Script Reference:
Shell Control Structures CSE 2031 Fall August 2015.
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.
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.
An Introduction to Unix Shell Scripting
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Linux Operations and Administration
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
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 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.
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.
Writing Scripts Hadi Otrok COEN 346.
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.
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.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
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
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
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.
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.
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
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
CIRC Summer School 2017 Baowei Liu
Lecture 9 Shell Programming – Command substitution
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
Pepper (Help from Dr. Robert Siegfried)
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
CSE 303 Concepts and Tools for Software Development
Presented by, Mr. Satish Pise
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

Introduction to Unix – CS 21 Lecture 11

Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control Flow Statements Conditional statements Looping

So Many Commands… Unix has a lot of commands, but there is no way it has everything What do you do if no command exists that does what you want? Build it yourself! The shell itself is actually a programming language A shell program consists of a sequential list of commands Think of it as a listing of programs to run in order

First Thing: Comments A comment in a shell script is started with the # symbol Example: # This is a comment Comments are not processed and are only there to help people read your program Use them to explain your code

Basic Form Of A Shell Executable

Ways To Run Shell Executables source FILENAME /bin/bash FILENAME Make the file executable and run it as a program First line should specify which shell to use #!/bin/bash #!/usr/bin/perl

Return Values And exit Every program and shell returns an integer value to indicate success or failure Used to inform other programs how things went 0 = Success Any other number = Failure

Variables In order to hold values, variables are supported Any combination of letters, numbers, and _ Must start with a letter or _ Case sensitive D and d are two separate variables

Setting And Using Variables Setting variables is done through the = operation No prior declaration is needed All variables are just strings of characters d=date Reading variables and accessing variables is done with the $ Variable is replaced with its value echo $d echo ${d}

Examples

Reading User Input Into Variables Usage: read var1 var2 … Reads values from standard input and assigns them to each variable If more words are typed in then the excess get assigned to the last variable If more variables are assigned than values given, the excess variables are empty

Example Of User Input

Difference Between Single, Double, and Backwards Quote Single quotes (‘) do not interpret any variables Double quotes (“) interpret variables Backwards quotes (`) interpret variables and treat them as a program to run and return the results of that program

Example Of Quote Difference

Command Line Parameters Just like all other Unix programs, shell scripts can read parameters off of the command line Different from user input Value is known before execution, not typed in during execution Example: testScript.sh testFile

Special Variables For Command Line Parameters Accessing command line parameters requires special variables $0 The name of the running program $1-$9 The first nine arguments to the program $* All of the command line arguments $# The total number of command line arguments

Example Of Command Line Parameters

What If There Are More? Shell programs aren’t limited to only 9 arguments Unfortunately, you can only access 9 at a time “shift” command Shifts the arguments to the left and brings new values in from the right

Visual Representation Of shift abcdefghij bcdefghij $1$2$3$4$6$5$8$7$9 $1$2$3$4$6$5$8$7$9 shift

Example Of Shift

Arithmetic Variables that contain numbers can be treated as numbers They can be added, subtracted and so on

Expr Command : Basic Usage Usage: expr EXPRESSION + - \* / % Examples expr expr 2 \* 3 expr 4 % 3

Example Of Expr

Other expr Options \( \) | Logical or & Logical and Many others… Check the man page

Control Flow And Conditionals Shell scripts can be more powerful than just a list of commands Sometimes you want to perform some commands only if certain conditions are true Example: Only print out a file if it is not a binary file

The test Command Used to check if certain conditions are true Usage: test EXPRESSION

Conditions test Checks Comparisons -eq -ne -lt -le -gt -ge

More Conditions System conditions -d File is a directory -e File exists -f File is a normal file -s File is non-empty

More test Conditions ! Not Negates the next check Example: test ! –x tFile -a And two conditions Example: test $1 –eq $2 –a $2 –gt 5 -o Or two conditions Example: test $1 –eq $2 –o $2 –gt 5

Shortcut For Test Because this is used very often, a shortcut exists [] Example: [ -f tFile ] test –f tFile

When Do You Use This? test is used to control the operation of your script The answer from test should guide the execution of your code one way or another Used in conditional statements

if then fi if [ CONDITION ] ; then STATEMENTS fi

Flowchart Of if Is Condition True? if Statements fi Yes No

Example Of if

If then else fi if [ CONDITION ] ; then STATEMENTS-YES else STATEMENTS-NO fi

Flowchart of if else Is Condition True? if Statements-Yes fi Yes No Statements-No

Example of if else

if then elif else fi if [ CONDITION1 ] ; then STATEMENTS-1 elif [ CONDITION2 ] ; then STATEMENTS-2 else STATEMENTS-3 fi

Flowchart of if elif else Is Condition1 True? if Statements-1 fi Yes No Statements-2 Is Condition2 True? No Yes Statements-3

Example Of if elif else

Case esac case STRING is pattern) STATEMENTS-1 ;; pattern) STATEMENTS-2 ;; … esac

Flowchart Of case case … Statements-1Statements-2Statements-N esac

Example Of case

Looping Sometimes you want to do something many times Loop for a set number of times Loop while a condition is true Loop until a condition is false

for Loop for VAR in LIST do STATEMENTS done

Flowchart Of For Loop Statements List empty? for Yes done No var = next

Example Of For Loop

Special Form Of For Loop Without a list, it will go through all of the command line arguments for var do STATEMENTS done

Example Of Special For Loop

While Loop while CONDITION do STATEMENTS done

Flowchart Of While Loop Is Condition True? while Yes No Statements done

Example Of While Loop

Until Loop until CONDITION do STATEMENTS done

Flowchart of Until Loop Is Condition True? until No Yes Statements done

Example Of Until Loop

In Lab Today Practice getting used to all of the constructs shown today Small programming projects

Next Time More programming practice More tools that allow your programs to do much more