cs2042_Lecture10 - CS2042 - Unix Tools Fall 2009 Lecture 10...

Info iconThis preview shows pages 1–11. 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: CS2042 - Unix Tools Fall 2009 Lecture 10 David Slater dms236 at cornell.edu October 21, 2009 David Slater dms236 at cornell.edu CS2042 - Unix Tools Organization Homework 3 Due on Friday Class on Friday will be in Upson 361 (Lab) We will be doing an in class programming contest David Slater dms236 at cornell.edu CS2042 - Unix Tools 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 CS2042 - Unix Tools Quick If Back to && Recall that we can run one command after another by typing cmd1 && cmd2 Furthermore, remember that cmd2 only runs if cmd1 exits successfully. We can thus use test to do quick if statements. Example: test -f file1 && cat file1 prints file1 only if it is a file. Good for quick little tests David Slater dms236 at cornell.edu CS2042 - Unix Tools Retrieving Files From a Server You may already have encountered a case where you need to retrieve a file from a server. We can do this on the command line using a secure ftp connection sftp sftp user@host connects to host as user Most of the basic commands work fine on the remote machine (cd, ls, chmod, mkdir etc) Some of the basic commands can be run when connected by typing l before the command (lls, lcd, lpwd, etc) Use put to move a file to the remote server Use get to grab a file from the remote server David Slater dms236 at cornell.edu CS2042 - Unix Tools Functions! Just as in every other language, breaking code out into functions is nice and can simplify code etc etc... Functions must be written before they are first called and have the following syntax: function my_func { code;} or function_name() { cmd1; cmd2; ... cmdn; } Call a function just like you would call any other program. Functions cannot be empty! David Slater dms236 at cornell.edu CS2042 - Unix Tools Trivial Example #! /bin/bash # Hello World with functions! function quit { exit } function hello { echo Hello! } hello quit echo foo Prints Hello! but not foo. David Slater dms236 at cornell.edu CS2042 - Unix Tools More Functions You can also write functions straight from the command line, but these disappear when the session ends just like variables. If you want to use a function that you define in a script outside run it with source #! /bin/bash # Hello.sh hello() { echo "Hello!" } hello $ source ./Hello.sh Hello! $ hello Hello! David Slater dms236 at cornell.edu CS2042 - Unix Tools Local Variables You can define variables to be local to a function. To define a variable as local just do local loc_var=value David Slater dms236 at cornell.edu CS2042 - Unix Tools Trivial Local Var Example #!/bin/bash func () { local loc_var=23 echo "loc_var in function = $loc_var" global_var=999 echo "global_var in function = $global_var"" } echo "Before function call our vars are $loc_var and $global_var func echo "loc_var outside function = $loc_var...
View Full Document

This note was uploaded on 03/15/2010 for the course CS 2024 taught by Professor Dinapoli during the Fall '07 term at Cornell University (Engineering School).

Page1 / 33

cs2042_Lecture10 - CS2042 - Unix Tools Fall 2009 Lecture 10...

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