cmpsc 60: week 6 discussion

22
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara

Upload: milt

Post on 23-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

CMPSC 60: Week 6 Discussion. Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon. University of California Santa Barbara. Shell Scripts - Usefulness. Mass file modification Run a program on a set of files Rename a set of files Convert image files from Bitmap to JPEG - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CMPSC 60: Week 6 Discussion

CMPSC 60: Week 6 Discussion

Originally Created By: Jason Wither

Updated and Modified By: Ryan Dixon

University of California Santa Barbara

Page 2: CMPSC 60: Week 6 Discussion

Shell Scripts - Usefulness

Mass file modification– Run a program on a set of files

Rename a set of files Convert image files from Bitmap to JPEG Backup critical files

Tailor command input and output– Create custom filters to dynamically alter text– Generate a composite file combining data from a set of files

Simplify sequences of commands

Page 3: CMPSC 60: Week 6 Discussion

Test

Evaluate an expression When the expression is true, exits with 0 When false, exits with a non-zero value “test -e filename” == “[ -e filename ]”

– if filename exists, exits with 0

“test $var1 = $var2”, “[ $var1 != $var2 ]”– string equality, inequality test

Page 4: CMPSC 60: Week 6 Discussion

More Test

[ $i -eq 2 ], [ $i -ne 1 ]– integer equality, inequality test

[ $i -lt 20 ], [ $i -le 4 ]– integer Less Than, Less than or Equal test

[ $i -gt 0 ], [ $i -ge 5 ]– integer Greater Than, Greater than or Equal test

[ ! -d $file ]– NOT a directory = true

Page 5: CMPSC 60: Week 6 Discussion

Test Example

An example:

for i in `ls`

do

if [ -d $i ]

then

echo $i

fi

done

Page 6: CMPSC 60: Week 6 Discussion

Cut

Cut out selected bytes, columns, or fields cut -b10-15 <file>

– prints bytes 10-15 of each line in <file> cut -c1-10,20-22 <file>

– prints characters 1-10 and 20-22 of each line in <file> cut -f2 <file>

– prints the second field of each line in <file>– by default, fields are separated by tabs– the delimiter can be changed from tabs using the -d option

Page 7: CMPSC 60: Week 6 Discussion

Head / Tail

Prints the first / last lines of a file head <file>

– Prints the first 10 lines of <file>

tail -15 <file>– Prints the last 15 lines of <file>

tail -2 <file> | head -1– Prints out only the second to last line of file

Page 8: CMPSC 60: Week 6 Discussion

expr – evaluates math expressions

expr 2 + 2– Outputs 4– White space around “+” matters

Other operations:– + - * / % and more– Note that some operators may need to be

escaped to be called from the command line. expr 2 * 2 # Results in an error since “*” is a wild card. expr 2 \* 2 # Correct expression, asterisk is escaped.

Page 9: CMPSC 60: Week 6 Discussion

Shell Scripts - How they start

Begins with #! (Sha-Bang) #! is followed by the interpreter to use

– #!/bin/sh (what we’ll be doing)– #!/usr/bin/perl– etc…

BTW, white-space matters…

Page 10: CMPSC 60: Week 6 Discussion

Shell Scripts - Variables

Case-sensitive Created as needed, simply assign to a name

– var=3 or var=“Your favorite band”

Access by pre-pending with a $– echo $var (prints out the value of var)

Page 11: CMPSC 60: Week 6 Discussion

Shell Scripts - Variable Manipulation

Working with numbers– var=$t + 2– var=`expr $t+2`– var = `expr $t + 2`

– var=`expr $t + 2`

(WRONG!!!!!!)(WRONG!!!!!!)(WRONG!!!!!!)

Correct Solution

Page 12: CMPSC 60: Week 6 Discussion

Shell Scripts - Quotations

Double-quotes– Variables within are resolved

If var=“This is cool” echo “$var stuff” Outputs “This is cool stuff”

Single-quotes– String is treated literally

Echo ‘$var stuff’ Outputs “$var stuff”

Page 13: CMPSC 60: Week 6 Discussion

Shell Scripts - Quotations

Back-quotes– Executes quoted command

echo “Today’s date is `date`” Prints “Today’s date is Wed May 19…”

– var=`ls -l` var now contains the output of the ls -l command

Page 14: CMPSC 60: Week 6 Discussion

Shell Scripts - Conditionals

If-statements

if test-cmds

then

commands

else

commands

fi Else portion is optional

Page 15: CMPSC 60: Week 6 Discussion

Shell Scripts - Conditionals

Multiple If-statements in a rowif test-cmdsthen

commandselif test-cmdsthen

commandselse

commandsfi

Page 16: CMPSC 60: Week 6 Discussion

Shell Scripts - Conditionals

Examples

if [ $var -gt 0 ]

then

echo ‘$var is greater than zero’

else

echo ‘$var is not greater than zero’

fi

Page 17: CMPSC 60: Week 6 Discussion

Shell Scripts - Loops

While-loops

while

test-cmds

do

commands

done

Page 18: CMPSC 60: Week 6 Discussion

Shell Scripts - Loops

For-loops

for x in list

do

commands

done

Page 19: CMPSC 60: Week 6 Discussion

Shell Scripts - Loops

Example:for x in 1 2 3 4 5do

echo “$x”done

Outputs:12345

Page 20: CMPSC 60: Week 6 Discussion

Shell Scripts - Loops

Example:x=1while [ $x -le 3 ]do

echo “$x”x=`expr $x + 1`

done Outputs:

123

Page 21: CMPSC 60: Week 6 Discussion

Shell Scripts - Loops

Example:for x in `ls`do

if [ -d $x ]then

chmod 750 $xelse

chmod 640 $xfi

done Sets all directories with rwxr-x--- and all other files with rw-r-----

permissions

Page 22: CMPSC 60: Week 6 Discussion

Shell Scripts - Exercise

Print out the greatest of the variables $1, $2 and $3 (These represent the first 3 command-line arguments passed to the script)