Lec6Handouts - CS2044 Advanced Unix Tools Spring 2009...

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
Organization Homework 2 Due Monday March 8th Start Early! Turn In Code That Works! David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
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
Image of page 8
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 week=‘date ’+%W’ | sed ’s/^0//’‘ index=$(($week % $count)) email=$wholist[index] echo $email David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Image of page 9

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

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern