cs2044_Lecture3

cs2044_Lecture3 - CS2044 Advanced Unix Tools Spring 2009...

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

View Full Document Right Arrow Icon
CS2044 - Advanced Unix Tools Spring 2009 Lecture 3 David Slater dms236 at cornell.edu February 26, 2010 David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background 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 Hopefully you all completed the questionnaire Homework handed out at the end of class due next Friday at 6 pm. Lab Accounts? David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 2
Some Review What does this do? sed ’s/^[ \t]*//g’ myfile David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 3

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

View Full Document Right Arrow Icon
Some Review What does this do? sed ’s/^[ \t]*//g’ myfile Removes all leading whitespace sed ’/^#/d’ /etc/services | more sed ’/^[^#]/d’ /etc/services | more David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 4
Some Review What does this do? sed ’s/^[ \t]*//g’ myfile Removes all leading whitespace sed ’/^#/d’ /etc/services | more sed ’/^[^#]/d’ /etc/services | more The first displays the file without commends, the second displays only the comments David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 5

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

View Full Document Right Arrow Icon
Random Bash Thing Of The Day Arithmatic Bash will do math when it is placed within double parenthesis (( )). A math expression returns 0 if the value is nonzero and 1 if the value is zero (wonderful isn’t it?) if we want to echo the math we need to do $((math )) Inside (( )) variable substitution is done automatically (don’t need $) Examples: (( 1 + 2 )); echo $? 0 (( 0 )); echo $? 1 (( 1 > 2 )); echo $? 1 David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 6
Passing arguments to scripts When we pass arguments to a bash script, we can access them in a very simple way: $1, $2, . .. ${10}, ${11} - are the values of the first, second etc arguments $0 - The name of the script $# - The number of arguments $* - All the arguments, ”$*” expands to ”$1 $2 . .. $n”, [email protected] - All the arguments, ”[email protected]” expands to ”$1” ”$2” . .. ”$n” You almost always want to use [email protected] (see why later) $? - Exit code of the last program executed $$ - current process id. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 7

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

View Full Document Right Arrow Icon
Simple Examples #! /bin/bash/ echo $(( $1 * $2 )) To do arithmetic in bash use $(( math )) #1 /bin/bash/ echo "Hello $1, my name is $HOSTNAME, i hear that you like $2, that is pretty nice, but my favorite is $SHELL. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 8
Structure Of Bash Scripts All our favorite (basic) constructs exist in bash scripts. Recall that every command has an exit status, stored in the variable $?. For tests, we are checking if this exit status is 0 (success) David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 9

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

View Full Document Right Arrow Icon
Finally if! If statements are structured just as you would expect: if cmd1 then cmd2 cmd3 elif cmd4 then cmd5 else cmd6 fi if cmd1 then cmd2; cmd3 elif cmd4 then cmd5 else cmd6 fi Each conditional statement evaluates as true if the cmd executes successfully (returns an exit code of 0) Can use a ; instead of hitting enter for a newline David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 45

cs2044_Lecture3 - CS2044 Advanced Unix Tools Spring 2009...

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

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