DOC PREVIEW
FSU COP 4342 - Lecture shells 2

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Fall 2006 Shell programming, part 2If example#!/bin/bash# 2006 09 08 - demonstrate if / then / elseif [ "x$1" != "x" ] && [ -f "$1" ]thenecho -n "Remove $1 (n)? "read answerif [ $answer == "y" ] || [ $answer == "Y" ] || [ $answer == "yes" ]thenecho "Would remove"elseecho "Would NOT remove"fielseecho "Please specify a regular file"fiCOP 4342Fall 2006 Shell programming, part 2If example#!/bin/bash# 2006 09 08 - demonstrate if / then / elseif [ "x$1" == "x" ]thenecho "Please specify a regular filename!"exit 1elif [ ! -f "$1" ]thenecho "$1 is not a regular file!"exit 1elseecho -n "Remove $1 (n)? "read answerif [ $answer == "y" ] || [ $answer == "Y" ] || [ $answer == "yes" ]thenCOP 4342Fall 2006 Shell programming, part 2echo "Would remove"elseecho "Would NOT remove"fifiCOP 4342Fall 2006 Shell programming, part 2The case statementcase WORD in PATTERN1 ) COMMANDS ;; PATTERN2 ) COMMANDS;; ... esacThe idea here is that WORD is tested against thevarious PATTERNs listed, in order. The first match thenexecutes the associated COMMANDs.COP 4342Fall 2006 Shell programming, part 2Case example#!/bin/bash# 2006 09 08 - case examplecase $1 in"yes")echo "Thanks!"exit 0;;"no")echo "Okay!"exit 1;;*)echo "Please use either ’yes’ or ’no’ (case-sensitive)";;esac;COP 4342Fall 2006 Shell programming, part 2While/until loopswhile list; do list; done;until list; do list; done;while executes the do list as long as the last commandin t he list returns 0. until executes until the lastcommand in the list returns 0.COP 4342Fall 2006 Shell programming, part 2while example#!/bin/bash# 2006 06 08 -- rdlecho -n "Now ’finish’ ? "read cmdwhile test $cmd != "finish"dorm NONEXISTecho "Status of \$? == $?"echo -n "Now ’finish’ ? "read cmddoneCOP 4342Fall 2006 Shell programming, part 2until example#!/bin/bash# 2006 06 08 -- rdlecho -n "Now ’finish’ ? "read cmduntil test $cmd == "finish"dorm NONEXISTecho "Status of \$? == $?"echo -n "Now ’finish’ ? "read cmddoneCOP 4342Fall 2006 Shell programming, part 2Shifting the argumentsYou can “shift” the argument list, eliminating thecurrent $1 and replacing it with the current $2, andso forth:COP 4342Fall 2006 Shell programming, part 2Shifting the arguments#!/bin/bashwhile [ $# -gt 0 ]doecho "$# --> arguments == ’$@’"shift;doneCOP 4342Fall 2006 Shell programming, part 2Shifting the arguments[langley@sophie 2006-Fall]$ ./Script8.sh a b c d e f g h8 --> arguments == ’a b c d e f g h’7 --> arguments == ’b c d e f g h’6 --> arguments == ’c d e f g h’5 --> arguments == ’d e f g h’4 --> arguments == ’e f g h’3 --> arguments == ’f g h’2 --> arguments == ’g h’1 --> arguments == ’h’[langley@sophie 2006-Fall]$COP 4342Fall 2006 Shell programming, part 2exitWe have already talked about exit, but to reiteratesome points about exit:☞ An exit status of zero should indicate success. It is agood idea to use an explicit exit NUM in scripts.☞ An exit status that is non-zero should indicate failure.☞ C programs use exit(NUM) to return a status.COP 4342Fall 2006 Shell programming, part 2exit example#/bin/bash# 2006 09 08 -- rdl Script9.shif ./Script10.shthenecho -n "Enter filename: "read filenameecho "You entered ’$filename’"elseecho "Okay, no filename needed."fiCOP 4342Fall 2006 Shell programming, part 2exit example#/bin/bash# 2006 09 08 -- rdl Script9.shwhile /bin/truedoecho -n "Should I ask for a filename? "read answercase $answer in"no")exit 1;;"yes")exit 0;;*);;COP 4342Fall 2006 Shell programming, part 2esacdoneCOP 4342Fall 2006 Shell programming, part 2Regular expressionsRegular expres sions are a c onvenient way to describe asequence of characters, and regular expressions are partof such programs as emacs, awk, and perl.COP 4342Fall 2006 Shell programming, part 2Regular expressions: operationsConcatenation: just place items adjacent, such ab, xyz,or somecharsCOP 4342Fall 2006 Shell programming, part 2Regular expressions: operationsRepetition: we use “*” to indicate repetition zero ormore times:a*b == b, ab, aab, aaab, ...COP 4342Fall 2006 Shell programming, part 2Regular expressions: operationsSpecial case of repetition: we can specify one or moretimes with +:a+b == ab, aab, aaab, ...COP 4342Fall 2006 Shell programming, part 2Regular expressions: characters and classesThe dot “.” can indicate any character, such asa.b == a1b, a2b, a3b, ...COP 4342Fall 2006 Shell programming, part 2Regular expressions: characters and classesTo specify a class of characters, you can use the [ ]syntax:[abc] == a, b, c[a-d] == a, b, c, d[^a-z] == NOT a lower case character[0-9] == 0, 1, 2, 3, 4, 5, 6, 7, 8, 9COP 4342Fall 2006 Shell programming, part 2AnchoringYou can “anchor” an expression to either the beginningof a string or its end, or both. Useˆto indicate thebeginning of a line, and $ to indicate the end:^abc$ matches a line that c onsists exactly of abcabc$ matches a line that e nds in abc^abc maches a lines that begins with abcCOP 4342Fall 2006 Shell programming, part 2Alternation and groupingYou can specify a group with round brackets “(“ and“)”.You can specify alternatives with a vertical “”(abc)|(def) matches either abc or defCOP 4342Fall 2006 Shell programming, part 2Note on groupingIt also possible in many instances possible to make areference to whatever matched a group in round brackets.COP 4342Fall 2006 Shell programming, part 2Check chapter 32 for more on regularexpressions32.20 has a good summary of metacharacters fordifferent programs.32.21 has a reference with many useful examplesCOP 4342Fall 2006 Shell programming, part 2Using grep/egrepYou can use the grep program to find strings in files.The “-i” option makes the search case-insensitive. If nofile or fi les are specified, then grep looks to stdin forinput. grep also adds “?” as a special character thatmatches 0 or 1 instance of any character.COP 4342Fall 2006 Shell programming, part 2Examples w ith grep/egrepegrep [Ll]angley * # finds instances of ‘‘langley’’ or# ‘‘Langley’’ in all files in the# current working directoryegrep -i she?p * # finds case-insensitive instances of# shep and she.pegrep -c /bin/bash * # shows filename and# number of matchesCOP 4342Fall 2006 Shell programming, part 2Popular options with grep/egrep☞ -i → case-insensitive☞ -c → display count of matching lines rather allmatching lines☞ -v → invert the matching☞ -H → always show filenames☞ -h → always suppress filenamesCOP 4342Fall


View Full Document

FSU COP 4342 - Lecture shells 2

Download Lecture shells 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture shells 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture shells 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?