Lec6Handouts

Lec6Handouts - CS2044 Advanced Unix Tools Spring 2009...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS2044 - Advanced Unix Tools Spring 2009 Lecture 6 David Slater dms236 at cornell.edu March 2, 2009 David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools Organization Homework 2 Due Monday March 8th Start Early! Turn In Code That Works! David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools A word on testing Thus far we have looked at testing in terms of if statements and case statements. Bash also has a built in command called test that we can use. The syntax is test [ expr ] or just test expr . The most useful way to use this is with and and or statements. For instance test -d $1 && echo Y || echo N Is a possible solution to the first problem from the scripting contest. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools more testing you can use all the same file tests with test cmd1 && cmd1 evaluate cmd2 if cmd1 is true cmd1 || cmd2 evaluates cmd2 only if cmd1 is false cmd1 && cmd2 || cmd3 runs cmd2 if cmd1 is true, cmd3 if cmd1 is false David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools Bash array variables An array variable in bash is declared indirectly by myarray[indexnr]=value indexnr is treated as an arithmatic express and must evaluate to a positive number there is no maximum size of an array no requirement that member variables be indexed or assigned contiguously David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools more bash arrays arrays are zero-based (first element is indexed with number 0) you can declare a variable to be an array by typing declare -a myarray . You an also create arrays by typing myarray=(val1 val2 val3 ... valn) To add to an array just type myarray[indexnumber]=vaue You can use read to read in an array by using the-a option. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools Referencing variables in an array To refer to the content of an array you use curly braces: $ myarray=(one two three) $ echo $ { myarray[*] } one two three $ echo $myarray[*] one[*] $ echo $ { myarray[2] } two Referring to the array without an index is the same as referring to the content of the first element (the one referenced with index number zero). David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools A few more array notes You remove values from an array using unset , just like any other shell variable $ myarray=(one two three four) $ unset myarray[1] echo $ { myarray[*] } one three four $ unset myarray $ echo $ { myarray[*] } <- no output -> David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools An array example #! /bin/bash # Created on 3/1/2009 by David Slater # Purpose of script: Determines whose week it is # Usage: ./finduser.sh wholist=( ’Bob Smith <[email protected]>’ ’Jane L. Williams <[email protected]>’ ’Eric S. Raymond <[email protected]>’ ’Larry Wall <[email protected]>’ ’Linus Torvalds <[email protected]>’ ) count=0 while [ -n "$ { wholist[count] } " ] do count=$(($count+1)) done...
View Full Document

This document was uploaded on 05/21/2009.

Page1 / 30

Lec6Handouts - CS2044 Advanced Unix Tools Spring 2009...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online