cs2044_Lecture4 - 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 4 David Slater dms236 at cornell.edu March 1, 2010 David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools Organization Homework 1 Due on Friday Any Questions About The Homework? Note: Print Lower Boundary Of Each Bin Not Upper Boundaryt David Slater dms236 at cornell.edu CS2044 - Advanced 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 CS2044 - Advanced Unix Tools Random Bash Tip Of The Day The environment variable $PS1 stores your default prompt. You can modify this variable to spruce up your prompt if you like: Example First echo $PS1 to see its current value \s-\v\$ (default) It consists mostly of backslash-escaped special characters, like \s (name of shell) and \v (version of bash). There are a whole bunch of options, which can be found at http://www.gnu.org/software/bash/manual/bashref.html#Printing-a-Prompt David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools Modifying Your Prompt Once you have a prompt you like, set your $PS1 variable Define your prompt [email protected]: ∼ $ export PS1="New Prompt String" Type this line at the command prompt to temporarily change your prompt (good for testing) Add this line to ~/.bashrc or ~/.bash_profiles to make the change permanent. Note: Parantheses must be used to invoke the characters. Examples PS1="\u \w \t_" ⇒ slater ~ 12:12:12_ PS1="\W \j \d\:" ⇒ ~ 0 Oct 02: David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools 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 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 thumbnails Lets look at another script: #!/bin/bash # This script will create a thumbs/ directory containing thumbnails # of all JPEG files in the current folder. if [ !-e thumbs ] ; then mkdir thumbs if [[ $?-ne 0 ]] ; then echo HELP exit fi fi for i in *.jpg *.jpeg *.JPG *.JPEG do if [[ !-f "$i" ]] then continue fi echo "Resizing $i" jpegtopnm < "$i" | pnmscale -xsize=200 | pnmtojpeg > thumbs/"$i" 2> /dev/null done David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools How the shell works It is time we talked a bit about how the shell works....
View Full Document

This document was uploaded on 03/15/2010.

Page1 / 38

cs2044_Lecture4 - 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