Lec5Handouts

Lec5Handouts - CS2044 Advanced Unix Tools Spring 2009...

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

View Full Document Right Arrow Icon
CS2044 - Advanced Unix Tools Spring 2009 Lecture 5 David Slater dms236 at cornell.edu February 28, 2009 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 DocumentRight Arrow Icon
Organization Homework 1 Due on Friday Class on Friday in Upson 361 David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 2
Some Review If Syntax if [ condition ]; then cmds; fi for Syntax for i in some group; do cmds; done "$*" expands to ”$1 $2 . .. $n” "$@" expands to ”$1” ”$2” . .. ”$n” Almost always want $@. 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 DocumentRight Arrow Icon
Dealing with improper input Last time we looked at a script lcount.sh: #! /bin/bash # lcountgood.sh i="0" for f in "$@" do j=‘wc -l < $f‘ i=$(($i+$j)) done echo $i Which works great, unless we pass it bad input. Say we pass it Asf* , and there is no file that matches that. It returns the error message ./lcount.sh: line 5: Asf*: No such file or directory. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 4
Improper input The easiest way to fix this is to simply check if the file exists each time through the loop. #! /bin/bash # lcountgood.sh i="0" for f in "$@" do if [ -f $f ] then j=‘wc -l < $f‘ i=$(($i+$j)) fi done echo $i 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 DocumentRight Arrow Icon
How the shell works It is time we talked a bit about how the shell works. Processes in UNIX are organized in a tree. Every process has a parent process that started it or is responsible for it Every process has its own context memory: The environment. The enviornment stores data that is useful to us, namely environment variables. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 6
Environment vs Shell Variables When we set a variable using MYVAR=value , we are setting a shell variable. This means it is only visible to the current shell. So if we do: $ MYVAR=value $ echo $MYVAR value $ bash $ echo $MYVAR In the child bash shell the variable is unknown! 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 DocumentRight Arrow Icon
Environment vs Shell Variables Environment variables are passed to child processes. We set an environmental variable by using the export command: $ export MYVAR=value $ bash $ echo $MYVAR value $ MYVAR=different $ exit $ echo $MYVAR
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 05/21/2009.

Page1 / 26

Lec5Handouts - CS2044 Advanced Unix Tools Spring 2009...

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

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