1 / 22

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. Mass file modification Run a program on a set of files Rename a set of files Convert image files from Bitmap to JPEG

milt
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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

  4. 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

  5. Test Example • An example: for i in `ls` do if [ -d $i ] then echo $i fi done

  6. 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

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

  8. 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.

  9. 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…

  10. 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)

  11. 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

  12. 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”

  13. 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

  14. Shell Scripts - Conditionals • If-statements if test-cmds then commands else commands fi • Else portion is optional

  15. Shell Scripts - Conditionals • Multiple If-statements in a row if test-cmds then commands elif test-cmds then commands else commands fi

  16. Shell Scripts - Conditionals • Examples if [ $var -gt 0 ] then echo ‘$var is greater than zero’ else echo ‘$var is not greater than zero’ fi

  17. Shell Scripts - Loops • While-loops while test-cmds do commands done

  18. Shell Scripts - Loops • For-loops for x in list do commands done

  19. Shell Scripts - Loops • Example: for x in 1 2 3 4 5 do echo “$x” done • Outputs: 1 2 3 4 5

  20. Shell Scripts - Loops • Example: x=1 while [ $x -le 3 ] do echo “$x” x=`expr $x + 1` done • Outputs: 1 2 3

  21. 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

  22. 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)

More Related