Basic shell scripting CS 2204 Class meeting 7

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 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
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
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Introduction to Linux and Shell Scripting Jacob Chan.
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.
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.
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
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
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Lecture 4  C Shell Scripts(Chapter 10). Shell script/program  Shell script: a series of shell commands placed in an ASCII text file  Commands include.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
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.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
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 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.
(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.
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 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
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 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
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Bash Scripting CIRC Summer School 2016 Baowei Liu 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.
Bash Shell Scripting 10 Second Guide.
Intro to shell scripting
CIRC Winter Boot Camp 2017 Baowei Liu
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
CSC 352– Unix Programming, Fall 2012
Part 1: Basic Commands/Utilities
CAP652 Lecture - Shell Programming
CSE 390a Lecture 5 Intro to shell scripting
Shell Scripting March 1st, 2004 Class Meeting 7.
CIRC Summer School 2017 Baowei Liu
Lecture 9 Shell Programming – Command substitution
CSE 303 Concepts and Tools for Software Development
CHAPTER 8 (skip , ) CHAPTER 10
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Intro to shell scripting
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
Intro to shell scripting
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

Basic shell scripting CS 2204 Class meeting 7 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright 2001-2003.

Shell script/program A series of shell commands placed in an ASCII text file Commands include Anything you can type on the command line Shell variables Control statements (if, while, for) (C) Doug Bowman, Virginia Tech, 2001

Resources Review UIAN chapter 4 Online Advanced bash-scripting guide http://www.tldp.org/LDP/abs/html/index.html Bash Reference Manual http://www.gnu.org/manual/bash-2.05a/bashref.html ksh Reference Manual http://www.bolthole.com/solaris/ksh.html (C) Doug Bowman, Virginia Tech, 2001

Script execution Provide script as an argument to the shell program (e.g. bash my_script) Or specify which shell to use within the script First line of script is #!/bin/bash Make the script executable using chmod Make sure the PATH includes the current directory Run directly from the command line No compilation is necessary! (C) Doug Bowman, Virginia Tech, 2001

Simple example script #!/bin/bash echo “Hello world!” cd ~ pwd Output: /home/grads/sgifford (C) Doug Bowman, Virginia Tech, 2001

Quoting Quoting is necessary to use special characters in a variable’s value or string ”” - shell only interprets $ and ‘‘ and \ $ - variable substitution ` - Command substitution \” - Literal double quote \ is used to escape characters echo ‘`date +%D`‘ will print: 10/06/03 ’’ - shell doesn’t interpret special characters echo ‘`date +%D`‘ will print: `date +%D` (C) Doug Bowman, Virginia Tech, 2001

Shell variables Numeric Strings Arrays Command line arguments Functions Read only var refers to the name, $var to the value t = 100 #Sets var t to value 100 echo ”\$t = $t” #will print: $t = 100 Remove a variable with unset var Names begin with alpha characters and include alpha, numeric, or underscore (C) Doug Bowman, Virginia Tech, 2001

Numeric variables Integer variables are the only pure numeric variables that can be used in bash Declaration and setting value: declare -i var=100 Expressions in the style of C: (( expression )) e.g. (( var+=1 )) +, -, *, /, %, &, |, ~, <, >, <=, >=, ==, !=, &&, || (C) Doug Bowman, Virginia Tech, 2001

String variables If you do not use the declare keyword with option –i when using a variable for the first time, it will be a string var=100 makes var the string ‘100’. However, (( var=100 )) will treat var as an integer even though it is a string (C) Doug Bowman, Virginia Tech, 2001

Array variables Array is a list of values Don’t have to declare size Reference a value by ${name[index]} ${a[3]} $a (same as ${a[0]}) Use the declare -a command to declare an array declare –a sports sports=(ball frisbee puck) sports[3]=bat (C) Doug Bowman, Virginia Tech, 2001

Arrays Array initialization sports=(football basketball) moresports=(${sports[*]} tennis) ${array[@]} or ${array[*]} refers to the entire array contents echo ${moresports[*]} produces football basketball tennis (C) Doug Bowman, Virginia Tech, 2001

Command line arguments If arguments are passed to a script, they can be referenced by $1, $2, $3, … $0 refers to the name of the script $@ - array filled with arguments excluding $0 $# - number of arguments (C) Doug Bowman, Virginia Tech, 2001

Exporting variables The export command, when used with a variable name, allows child processes of the shell to access the variable (C) Doug Bowman, Virginia Tech, 2001

Output We have already seen echo More common in other shells including ksh is print (does not exist in bash) echo –n does not print newline after output (C) Doug Bowman, Virginia Tech, 2001

Return values Scripts can return an integer value Use exit N The variable $? will contain the return value of the last command run Can be used to test conditions (C) Doug Bowman, Virginia Tech, 2001

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, file type, etc. (C) Doug Bowman, Virginia Tech, 2001

Conditions (continued…) [[ -e $file]] – File exists? [[ -f $file]] – Regular file? [[ -d $file]] – Directory? [[ -L $file]] – Symbolic link? [[ -r $file]] – File has read permission? [[ -w $file]] – File has write permission? [[ -x $file]] – File has execute perm? [[ -p $file]] – File is a pipe? (C) Doug Bowman, Virginia Tech, 2001

If statements Syntax: if condition then statements elif condition else fi optional (C) Doug Bowman, Virginia Tech, 2001

If statement Example if [[ -r $fname ]] then echo ’$fname is readable’ elif [[ -w $fname && -x $fname ]] echo ’$fname is writeable and executable’ fi (C) Doug Bowman, Virginia Tech, 2001

For loops Syntax: If list is omitted, $@ is assumed for var [in list] do statements done If list is omitted, $@ is assumed Otherwise ${list[*]} where list is an array variable. (C) Doug Bowman, Virginia Tech, 2001

For loop example for colors in Red Blue Green Yellow Orange Black Gray White do echo $colors done echo (C) Doug Bowman, Virginia Tech, 2001

While loops Syntax: while condition do statements done The keywords break, continue, and return have the same meaning as in C/C++ (C) Doug Bowman, Virginia Tech, 2001

Case statements Syntax: case expression in pattern1) statements ;; … *) esac (C) Doug Bowman, Virginia Tech, 2001

Case statement example case $1 in -a) statements related to option a ;; -b) statements related to option b ;; *) all other options ;; esac (C) Doug Bowman, Virginia Tech, 2001

Command substitution Use the output of a command in a variable, condition, etc. by: `command` $(command) (C) Doug Bowman, Virginia Tech, 2001

Examples Arguments printed in a for loop Reformatting the wc command Performing a depth-first search (C) Doug Bowman, Virginia Tech, 2001