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.

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.
2006-Jan-231 Shell Scripts Jacob Morzinski
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
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).
The Bourne Shell 吳 瑞 麟 National Cheng Kung University, Computer and Network Center.
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.
Basic Elements of C++ Chapter 2.
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.
Scripting CBIS BASH Scripting Step 1 – Create the bash file. Usually a good idea to end it in.sh file1.sh Step 2 – Using CHMOD make the bash file.
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Introduction to UNIX / Linux - 11
An Introduction to Unix Shell Scripting
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Michael Musick CSC 415. A Brief History of bash  Language was named in tribute to Steve Bourne’s shell  Brian Fox wrote the first versions of bash 
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
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.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Chapter Eight Basic Shell Scripting1 System Programming Basic Shell 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.
(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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
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 Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
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.
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.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
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.
CSC 352– Unix Programming, Fall 2012
Shell Scripting March 1st, 2004 Class Meeting 7.
CIRC Summer School 2017 Baowei Liu
Basic Elements of C++.
Lecture 9 Shell Programming – Command substitution
Pepper (Help from Dr. Robert Siegfried)
Basic Elements of C++ Chapter 2.
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.
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
CSE 303 Concepts and Tools for Software Development
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

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 Tech

Lenwood Heath, Virginia Tech, Fall, Shell Script (Program) What is a shell script? Shell commands in a text file that is invoked as its own command Commands include... anything you can type on the command line shell variables control statements ( if, while, for,... )

Lenwood Heath, Virginia Tech, Fall, Script Execution Provide script as an argument to a shell command — bash my_script Or specify shell on the first line of the script — #!/bin/bash Make sure that the script is executable Run my_script directly from the command line No compilation; interpreted by shell

Lenwood Heath, Virginia Tech, Fall, Simple Script #!/bin/bash echo "Hello, World! " system=$(uname -a) echo $system Result: Hello, World! Linux cashew.cslab mdk #1 Fri Aug 13 11:14:25 EDT 2004 i686 unknown unknown GNU/Linux

Lenwood Heath, Virginia Tech, Fall, Shell Variables Numeric Strings Arrays var refers to the name of the variable, $var refers to the value var=100 # sets the value to 100 (( var2=1+$var )) Variable names begin with a letter and can include letters, digits, and underscores

Lenwood Heath, Virginia Tech, Fall, Numeric Variables Integer variables are the only pure numeric variables that can be used in bash Declaration and setting value declare –i var=100 Numeric expressions are enclosed in double parentheses (( var+=1 )) Operators are the same as in C/C++ +, -, *, /, %, &, |,, =, ==, !=, &&, ||

Lenwood Heath, Virginia Tech, Fall, String Variables Unless explicitly declared as another type, variables are strings var=100 makes the var the string "100" However, placing the variable within double parentheses will treat it as an integer (( var2=1+$var ))

Lenwood Heath, Virginia Tech, Fall, String Variables (continued) Using substrings ${string:n} ${string:5} # first five chars ${string:-2} # last two chars ${string:n:m} ${string:0:4} # first to fifth ${string:1:3} # second to fourth ${#string} # length of string Concatenating strings string_var1="$string_var1 $string_var2"

Lenwood Heath, Virginia Tech, Fall, Array Variables Array is a list of values — do not have to declare size Reference a value by ${a[index]} ${a[3]} # value in fourth position $a # same as ${a[0]} Use the declare –a command to declare an array declare –a sports sports=(basketball football soccer) sports[3]=hockey

Lenwood Heath, Virginia Tech, Fall, Array Variables (cont) Array initialization sports=(football basketball) moresports=($sports tennis) or ${array[*]} refers to the entire contents of the array echo ${moresports[*]} Output: football basketball tennis

Lenwood Heath, Virginia Tech, Fall, Command Line Arguments If arguments are passed to a script, they have the values $1, $2, $3, etc. $0 is the name of the script $* is a string of all of the arguments separated by spaces is an array of all of the arguments $# is the number of arguments

Lenwood Heath, Virginia Tech, Fall, Output and Quoting echo a message # print to stdout echo –n "yes/no? " # a prompt Shell interprets $ and `` within double quotes $ — variable substitution ` — command substitution echo "`date +%D`" # 09/27/04 Shell does not interpret special characters within single quotes echo '`date +%D`' # `date +%D`

Lenwood Heath, Virginia Tech, Fall, Redirecting Output to File Redirecting output to a file is the same as on the command line Examples echo "$var" > $OUTFILE # overwrite echo "$var" >> $OUTFILE # append String expansion echo $'\n\n\n' # 3 blank lines echo $'\t' indent # indented text

Lenwood Heath, Virginia Tech, Fall, Control Testing conditions if — Conditional execution for — Loop over the elements of an array while — Loop while a condition is true case — Multiway conditional on variable value Define and invoke functions

Lenwood Heath, Virginia Tech, Fall, Conditions If using integers: (( condition )) If using strings: [[ condition ]] Examples (( a == 10 )) (( b >= 3 )) [[ $1 = -n ]] [[ ($v != fun) && ($v != games) ]] Special conditions for file existence, file permissions, ownership, and file type

Lenwood Heath, Virginia Tech, Fall, Conditions (cont) [[ -e $file ]] – File exists? [[ -f $file ]] – Regular file? [[ -d $file ]] – Directory? [[ -L $file ]] – Symbolic link? [[ -r $file ]] – Read permission? [[ -w $file ]] – Write permission? [[ -x $file ]] – Execute permission?

Lenwood Heath, Virginia Tech, Fall, If Statement Syntax if condition1 then statements elif condition2 then statements else statements fi optional

Lenwood Heath, Virginia Tech, Fall, If Statement — Example file=readings.php if [[ -r $file ]] then echo " $file is readable " elif [[ (-w $file) && (-x $file) ]] then echo "$file is writable and executable " fi Result: readings.php is readable

Lenwood Heath, Virginia Tech, Fall, For Loops Syntax for var [in list] do statements done If list is omitted, is assumed Otherwise ${list[*]}, where list is an array variable

Lenwood Heath, Virginia Tech, Fall, For Loops — Example colors=(yellow red blue green orange) for color in ${colors[*]} do echo $color done Result: yellow red blue green orange

Lenwood Heath, Virginia Tech, Fall, While Loops Syntax while condition do statements done The keywords break, continue, and return have the same meaning as in C/C++

Lenwood Heath, Virginia Tech, Fall, While Loops — Example #!/bin/bash # A shell script that only prints # its arguments while [[ $1 ]] do echo $1 shift done

Lenwood Heath, Virginia Tech, Fall, Case Statements Syntax case expression in pattern1) statements ;; pattern2) statements ;;... *) statements ;; esac

Lenwood Heath, Virginia Tech, Fall, Case Statements — Example case $1 in -a) # statements related to option a ;; -b) # statements related to option b ;; *) # all other options ;; esac

Lenwood Heath, Virginia Tech, Fall, Functions Definition function function_name { statements } Function arguments are $1, $2, $3, etc.

Lenwood Heath, Virginia Tech, Fall, Functions (continued) Example definition function add_two{ (( $sum=$1+$2 )) return $sum } Invoking the function add_two 1 3 echo $? $? value returned by last function call or command Function definition must occur before the function is called in the script