If example#!/bin/bash# 2008 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"fiUnix Tools: ShellsIf 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 1Unix Tools: ShellsIf exampleelseecho -n "Remove $1 (n)? "read answerif [ $answer == "y" ] || [ $answer == "Y" ] || [ $answer == "yes" ]thenecho "Would remove"elseecho "Would NOT remove"fifiUnix Tools: ShellsThe case statementcase WORD inPATTERN1 )COMMANDS;;PATTERN2 )COMMANDS;;...esacThe idea here is that WORD is tested against the various PATTERNslisted, in order. The first match then executes the associatedCOMMANDs.Unix Tools: ShellsCase 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;Unix Tools: ShellsWhile/until loopswhile list; do list; done;until list; do list; done;while executes the do list as long as the last command in the listreturns 0. until executes until the last command in the listreturns 0.Unix Tools: Shellswhile example#!/bin/bash# 2006 06 08 - rdlecho -n "Now ’finish’ ? "read cmdwhile test $cmd != "finish"dorm NONEXISTecho "Status of \$? == $?"echo -n "Now ’finish’ ? "read cmddoneUnix Tools: Shellsuntil example#!/bin/bash# 2006 06 08 - rdlecho -n "Now ’finish’ ? "read cmduntil test $cmd == "finish"dorm NONEXISTecho "Status of \$? == $?"echo -n "Now ’finish’ ? "read cmddoneUnix Tools: ShellsShifting the argumentsYou can “shift” the argument list, eliminating the current $1 andreplacing it with the current $2, and so forth:Unix Tools: ShellsShifting the arguments#!/bin/bashwhile [ $# -gt 0 ]doecho "$# -> arguments == ’$@’"shift;doneUnix Tools: ShellsShifting 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]$Unix Tools: ShellsexitWe have already talked about exit, but to reiterate some pointsabout exit:An exit status of zero should indicate success. It is a good ideato 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.Unix Tools: Shellsexit example#/bin/bash# 2006 09 08 - rdl Script9.shif ./Script10.shthenecho -n "Enter filename: "read filenameecho "You entered ’$filename’"elseecho "Okay, no filename needed."fiUnix Tools: Shellsexit 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;;*);;esacdoneUnix Tools: ShellsRegular expressionsRegular expressions are a convenient way to describe a sequence ofcharacters, and regular expressions are part of such programs asemacs, awk, and perl.Unix Tools: ShellsRegular expressions: operationsConcatenation: just place items adjacent, such ab, xyz, orsomecharsUnix Tools: ShellsRegular expressions: operationsRepetition: we use “*” to indicate repetition zero or more times:a*b == b, ab, aab, aaab, ...Unix Tools: ShellsRegular expressions: operationsSpecial case of repetition: we can specify one or more times with +:a+b == ab, aab, aaab, ...Unix Tools: ShellsRegular expressions: characters and classesThe dot “.” can indicate any character, such asa.b == a1b, a2b, a3b, ...Unix Tools: ShellsRegular expressions: characters and classesTo specify a class of characters, you can use the [ ] syntax:[abc] == a, b, c[a-d] == a, b, c, d[â-z] == NOT a lower case character[0-9] == 0, 1, 2, 3, 4, 5, 6, 7, 8, 9Unix Tools: ShellsAnchoringYou can “anchor” an expression to either the beginning of a string orits end, or both. Useˆto indicate the beginning of a line, and $ toindicate the end:âbc$ matches a line that consists exactly of abcabc$ matches a line that ends in abcâbc matches a line that begins with abcUnix Tools: ShellsAlternation and groupingYou can specify a group with round brackets “(“ and “)”.You can specify alternatives with a vertical “”(abc)|(def) matches either abc or defUnix Tools: ShellsNote on groupingIt also possible in many instances possible to make a reference towhatever matched a group in round brackets.Unix Tools: ShellsCheck chapter 32 for more on regular expressions32.20 has a good summary of metacharacters for different programs.32.21 has a reference with many useful examplesUnix Tools: ShellsUsing grep/egrepYou can use the grep program to find strings in files. The “-i” optionmakes the search case-insensitive. If no file or files are specified, thengrep looks to stdin for input. grep also adds “?” as a specialcharacter that matches 0 or 1 instance of any character.Unix Tools: ShellsExamples with 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 matchesUnix Tools: ShellsPopular options with grep/egrep-i → case-insensitive-c → display count of matching lines rather all matching lines-v → invert the matching-H → always show filenames-h → always suppress filenames-l → just show the filenames that have one or more matchesUnix Tools: ShellswcYou can use the wc program to count characters, words, and lines:wc -l*# count the number of lines in all fileswc -w*# count the number of words in all fileswc -c*# count the number of characters in all fileswc -lw*# count the number of words and lines in all fileswc*# count words, characters, and lines in all filesUnix Tools:
View Full Document