1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.

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.
7 Searching and Regular Expressions (Regex) Mauro Jaskelioff.
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
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.
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.
Shell Script Examples.
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.
System Programming Regular Expressions Regular Expressions
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.
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
An Introduction to Unix Shell Scripting
The if construct The general format of the if command is: Every command has exit status If the exit status is zero, then the commands that follow between.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
CSC 352– Unix Programming, Spring 2015 April 28 A few final commands.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
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.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
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.
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
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.
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 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
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.
Shell Control Structures
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Shell Scripting March 1st, 2004 Class Meeting 7.
Lecture 9 Shell Programming – Command substitution
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
What is Bash Shell Scripting?
CSC 352– Unix Programming, Spring 2016
Unix Talk #2 grep/egrep/fgrep (maybe add more to this one….)
UNIX Reference Sheets CSE 2031 Fall 2010.
Linux Shell Script Programming
Essential Shell Programming
Shell Control Structures
Shell Control Structures
Introduction to Bash Programming, part 3
Review.
Presentation transcript:

1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX

2 Command Substitution a string in back quotes ` … ` does command substitution –This means that the result of the command (the standard output of the command) replaces the back quoted string Examples: count=`wc -w <$1` # the value of count is assigned the number of words in file $1 if [ `wc -l < $2.txt` -lt 1000 ]; #checks if the number of lines in the file is < 1000 cat `grep -l exit *.sh` #print out all *.sh files containing the word exit

3 Exit Command (again)‏ Conventionally, zero normally indicates success. Nonzero values indicate some type of failure. It is thus good practice to ensure that if the shell script terminates properly, it is with an “exit 0” command. If the shell script terminates with some error that would be useful to a calling program, terminate with an “exit 1” or other nonzero condition. most Unix utilities that are written in C will also call “exit( );” upon termination to pass a value back to the shell or utility that called that utility.

4 Exit Example The following shell script exits properly. It also distinguishes the response through the value returned. #!/bin/sh #determines a yes (0) or no (1) answer from user echo “Please answer yes or no”; read answer while : do case $answer in “yes”) exit 0;; “no”) exit 1;; *) echo “Invalid; enter yes or no only” read answer;; esac done

5 Testing the Exit Status Conditions tested in control statements can also be the exit status of commands. Assume that the script “yes.sh” has been invoked. The following segment will test this as part of its script: if yes.sh then echo “enter file name” read file else echo “goodbye”; exit 0 fi

6 Regular Expressions and Wildcards Many Unix utilities use regular expressions A regular expression is a compact representation of a set of strings Note that the shell uses wildcards (*, ?, etc.) for filename matching. The special characters are not necessarily used the same way in regular expressions Thus the pattern “alpha*.c” for filenames is not the same when used in the grep command (for example) to match a regular expression! In a regular expression, “*” means match zero or more of the preceding character

7 Regular expression operators Concatenation –This is implicit and is simply one character followed by another. ab#matches the character “a” followed by “b” alpha#several characters concatenated * operator: –indicates zero or more instances of the preceding character or preceding regular expression if grouping, that is parentheses (, ), are used. ab*c#matches ac, abc, abbc, etc. + operator: –similar to * except matches 1 or more instances of the preceding character

8 Matching a specific class of characters “.” matches any single character except newline a.b#matches a followed by any character, then b # for example adb, a&b, etc. [] is used to indicate one of a set of characters. The ‘-’ is used to define a range. A “^” after “[“ means match anything not in the set. [adkr]#match a, d, k, r [0-9]#match 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 [a-z]#match lower case letters [^aeiou]#match any character except a vowel [^0-9]#match any character except a decimal digit

9 Anchors Anchors ^ and $ can be used to indicate that a pattern will only match when it is at the beginning or end of a line (note that the following use of “^” is different from its use inside a set of characters)‏ ^alpha#match the string “alpha” only when it #is at the beginning of the line [A-Za-z]+$ # a name at the end of the line ^alpha*zeta$ #start with alph, end with zeta and #any number of “a”s in between

10 Alternation and Grouping Use the “|” character to choose between alternatives. Parentheses are for grouping a|b#match a or b a*|b#match any number of a’s or a b. (ab*a)*#any number of ab*a

11 grep and egrep grep searches for strings in files that match a regular expression and prints out the lines that contain these matches to stdout. If no file is specified then grep uses stdin. form (the initial brackets indicate some optional flags): grep [-i] [-w] [-c] [-v] [-E] pattern [files] egrep is extended grep and extends the syntax of regular expressions. Generally grep does not support the parentheses, the + operator, the | operator or the ? operator (zero or one occurrence). The flag –E in grep generally gives egrep behavior.

12 Grep options -i will make the search case insensitive -c will cause the number of lines matched to be printed -w will force the search to look for entire words -v will cause the lines that do not match to be output -l will return only the name of the file when grep finds a match

13 Grep examples grep alpha junk#look for the substring alpha in file junk grep “ii*” junk#look for the substring of one or more i’s grep ^begin junk#look for a line that starts with begin grep recieve *.sh#find a “recieve” in any file ending in.sh grep “[abc].*” junk #find a substring with an a, b, or c, followed #by any number of other characters

14 For statement The shell is assigned each word in the list, where the set of commands is performed each time the word is assigned to the variable. If the “in ” is omitted, then the variable is assigned each of the command line arguments. for [ in ] do one or more commands done

15 For statement examples #!/bin/sh #makes a backup of certain files and echoes arguments for file in `ls *.c` do cp $file $file.bak done for arg do echo $arg done exit

16 Examples cont #!/bin/sh #place command line arguments into a string variable #arguments separated by a blank space s=“” for arg do s=“$s $arg” done #compile each of the files in the list $s for file in $s do gcc –g –c #file done exit 0

17 Expr expr evaluates an arithmetic or relational expression and prints its result to standard output. This is useful when you need to perform calculations in the shell script. It outputs 1 (true) or 0 (false) when evaluating a relational expression. Note that the arguments are operators must be separated by spaces. Example from the tcsh command line (note set)‏ set alpha = 3 expr $alpha + 2#result printed out is 5

18 More expr examples var=`expr $var + 1` #increment var by 1 if [ `expr $s1 \< $s2` = 1 ]#check if the value of s1 #is less than value of s2 beta=`expr $beta \* 2`#multiply value of beta by 2 set beta = 10; expr $beta / 2#using tcsh directly, result is 5 expr “$alpha” = hello#output 1 if variable alpha is #hello