CIS 240 Introduction to UNIX Instructor: Sue Sampson.

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`
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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
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 Script Examples.
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.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
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.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Chapter 6: Shell Programming
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 Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where.
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.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Module 9 Basic Scripting.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
©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.
Chapter Six Introduction to Shell Script Programming.
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,
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
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.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
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]
Shell Scripting What is Shell Programming? Putting UNIX commands in a file Seldom used where speed is important Often used to manipulate files To create.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
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.
 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.
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
Agenda Korn Shell Functions Korn Shell Variables
CIRC Summer School 2017 Baowei Liu
Lecture 9 Shell Programming – Command substitution
Writing Shell Scripts ─ part 3
Agenda Control Flow Statements Purpose test statement
CSCI The UNIX System Shell Startup and Variables
UNIX Reference Sheets CSE 2031 Fall 2010.
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
Introduction to Bash Programming, part 3
Review.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

CIS 240 Introduction to UNIX Instructor: Sue Sampson

CIS240 – UNIX Shell Programming Variables Control Structures Functions Commands Arithmetic expressions Getting back to a known state

CIS240 – UNIX Shell Programming Variables Places in memory used to hold values Can be initialized and altered Are represented by variable names Do not need to be declared before they are used (declaring means identifying the name and variable type)

CIS240 – UNIX Shell Programming Variable Names Follow the same convention as C names First character is upper or lower case a- z Next characters are upper or lower case letters, numbers, or underscores The maximum length depends on your version of UNIX

CIS240 – UNIX Shell Programming Variable Types Default is string May be declared as follows  -u Upper Case  -lLower Case  -iInteger  -rRead only  -LLeft justified  -RRight justified

CIS240 – UNIX Shell Programming Variable Example Execute these two commands. typeset –i x=5 echo $x echo x == returns x echo $x == returns the contents of variable x

CIS240 – UNIX Shell Programming Environment Variables Special variables that pertain to your environment Upper case letters HOMEContains the user’s home directory IFSInternal Field Separator PATHContains the full path used for commands PS1Primary prompt PS2Secondary prompt SHELLContains the pathname of the current shell

CIS240 – UNIX Shell Programming Decision (if) Format: if then else fi

CIS240 – UNIX Shell Programming File myscript if test -f a_test_file then echo “file exists” else echo “file does not exist” fi # sh myscript file does not exist

CIS240 – UNIX Shell Programming Results of TEST If true, the “then” code is executed If false, the “else” code is executed A zero value is true (reverse of C/C++) False is any value other than zero

CIS240 – UNIX Shell Programming Operators Used to formulate a test File Operators (tests characteristics of files) Arithmetic Operators (allows comparisons) Group Operators (groups operators together)

CIS240 – UNIX Shell Programming Selection (Case) Format: case in pattern1) statement-1;; pattern2) statement-2;; esac

CIS240 – UNIX Shell Programming File secondscript echo –n “Enter a word: “ read guessword case “$guessword” in fred) echo “guessword is fred” ;; bill | bert) echo “guessword is either bill or bert” ;; [S,s]*) echo “guessword starts with S or s” ;; xyzzy) echo “You have been playing adventure!” ;; *) echo “guessword was not matched” ;; Esac Sh secondscript Enter a word: Sue guess word starts with S or s

CIS240 – UNIX Shell Programming Looping (while) Format: while do done

CIS240 – UNIX Shell Programming File thirdscript while test –s test_file do echo “File test_file not of zero length” sleep 10 done Sh thirdscript File test_file not of zero length File test_file not of zero length…

CIS240 – UNIX Shell Programming Looping (for) Format: For in do done

CIS240 – UNIX Shell Programming File fifthscript for item in one two three do echo “Argument value is: $item” done sh fifthscript Argument value is: one Argument value is: two Argument value is: three

CIS240 – UNIX Shell Programming Two Methods for Saving a Shell Script Using cat (cat > script1.sh)  Stores command line scripts as they are entered  Does not allow for editing of scripts Using vi  Allows scripts to be edited  Scripts cannot be run from vi

CIS240 – UNIX Shell Programming Getting Back to a Known State Use d to exit out of a shell script d or exit will get you out of most problems Type bash to get you back into the bourne-again shell Log out and log back in (use logout if exit will not log you out)