Cmpsc 60 week 6 discussion
This presentation is the property of its rightful owner.
Sponsored Links
1 / 22

CMPSC 60: Week 6 Discussion PowerPoint PPT Presentation


  • 48 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

CMPSC 60: Week 6 Discussion

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


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


Shell scripts usefulness

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


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


More test

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


Test example

Test Example

  • An example:

    for i in `ls`

    do

    if [ -d $i ]

    then

    echo $i

    fi

    done


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


Head tail

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


Expr evaluates math expressions

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.


Shell scripts how they start

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…


Shell scripts variables

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)


Shell scripts variable manipulation

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


Shell scripts quotations

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”


Shell scripts quotations1

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


Shell scripts conditionals

Shell Scripts - Conditionals

  • If-statements

    if test-cmds

    then

    commands

    else

    commands

    fi

  • Else portion is optional


Shell scripts conditionals1

Shell Scripts - Conditionals

  • Multiple If-statements in a row

    if test-cmds

    then

    commands

    elif test-cmds

    then

    commands

    else

    commands

    fi


Shell scripts conditionals2

Shell Scripts - Conditionals

  • Examples

    if [ $var -gt 0 ]

    then

    echo ‘$var is greater than zero’

    else

    echo ‘$var is not greater than zero’

    fi


Shell scripts loops

Shell Scripts - Loops

  • While-loops

    while

    test-cmds

    do

    commands

    done


Shell scripts loops1

Shell Scripts - Loops

  • For-loops

    for x in list

    do

    commands

    done


Shell scripts loops2

Shell Scripts - Loops

  • Example:

    for x in 1 2 3 4 5

    do

    echo “$x”

    done

  • Outputs:

    1

    2

    3

    4

    5


Shell scripts loops3

Shell Scripts - Loops

  • Example:

    x=1

    while [ $x -le 3 ]

    do

    echo “$x”

    x=`expr $x + 1`

    done

  • Outputs:

    1

    2

    3


Shell scripts loops4

Shell Scripts - Loops

  • Example:

    for x in `ls`

    do

    if [ -d $x ]

    then

    chmod 750 $x

    else

    chmod 640 $x

    fi

    done

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


Shell scripts exercise

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)


  • Login