Shell Scripting March 1st, 2004 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.
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
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
The Bourne Shell 吳 瑞 麟 National Cheng Kung University, Computer and Network Center.
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.
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 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.
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
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
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.
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.
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.
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 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
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.
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.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
CIRC Winter Boot Camp 2017 Baowei Liu
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
Writing Shell Scripts ─ part 3
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
Pepper (Help from Dr. Robert Siegfried)
Topics Introduction to File Input and Output
An Introduction to Java – Part I, language basics
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.
Linux Shell Script Programming
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Introduction to Bash Programming, part 3
Topics Introduction to File Input and Output
Bash Scripting CS 580U - Fall 2018.
Basic shell scripting CS 2204 Class meeting 7
Review.
Presentation transcript:

Shell Scripting March 1st, 2004 Class Meeting 7

Shell Script (Program) What is a shell script? 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, . . . )

Script Execution Provide script as an argument to the shell program (i.e. bash my_script) Or specify which shell to use within the script First line of the script: #!/bin/bash Make sure that the script is executable Run directly from the command line No compilation is necessary

Simple Script #!/bin/bash echo “Hello, World!” cd ~ pwd Output: /home/grads/callgood

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 an alphabetic character and can include alpha, numeric, and the underscore

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++ +, -, *, /, %, &, |, <, >, <=, >=, ==, !=, &&, ||

String Variables Unless variables are explicitly declared as another type, they are considered to be 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 ))

String Variables (cont) 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”

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

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

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 $@ refers to all of the arguments – not an array – space separated group of strings $# refers to the number of arguments

Output and Quoting echo command is used to output information to standard out echo –n does not print a newline Shell interprets $ and `` within double quotes $ - variable substitution ` - command substitution echo “`date +%D`” # 03/01/04 Shell does not interpret special charactes within single quotes echo ‘`date +%D`” # `date +%D`

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’ echo $’\t’

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, . . .

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?

If Statement Syntax if condition then statements elif else fi optional

If Statement (cont) Example if [[ -r $file ]] then echo “$file is readable” elif [[ (-w $file) && (-x $file) ]] echo “$file is writeable and exectuable” fi

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

For Loops (cont) Example colors=(yellow red blue green orange) for color in ${colors[*]} do echo $color done

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

Case Statements Syntax case expression in pattern1) statements ;; ... *) esac

Case Statements (cont) Example case $1 in -a) # statements related to option a ;; -b) # statements related to option b *) # all other options esac

Command Substitution Use the command of a command in a variable, condition, . . . `command` $(command) Example files=(`ls`) for file in ${files[*]} do echo $file done

Functions Syntax function function_name { statements . . . } Functions can take arguments, in the same way that the script takes arguments (i.e. $1 represents first argument, $2 the second, . . . )

Functions (cont) Example function add_two{ (( $sum=$1+$2 )) return $sum } . . . # calling a function add_two 1 3 echo $? $? represents the return value of the last function call or command Function definition needs to occur before the function is called in the script

Assignment Suggestions One approach to the script: Handle arguments, setting flags for each i.e., if recursive option is given, set a variable that indicates that it was set Write part of script that generates the makefile in a function; call this function recursively, if necessary (i.e. if recursive option was set)

Assignment Suggestions (cont) Within function, store the output of ls command in an array; loop through this array and test filename of each file in the current directory for valid filenames and/or extensions files=(`ls`) for file in ${files[*]} do # no need to check directories if [[ -f $file ]] then # test for valid filename and/or extensions fi done

Assignment Suggestions (cont) Remember that you need to know all of the valid filenames before you can begin writing any of the makefile; you may need to store parts of the output in temporary files, and then concatenate these files into your makefile; be sure that you remove and temporary files that you create