220 likes | 329 Vues
Shell scripts are powerful tools that streamline file management through useful functions like mass file modifications, batch renaming, and format conversions (e.g., Bitmap to JPEG). They allow users to execute commands on multiple files simultaneously, back up critical data, and generate composite files from different data sources. This discussion delves into various operations with shell scripts, including conditionals, loops, and text manipulation techniques, demonstrating their flexibility in automating repetitive tasks in a Unix-like environment.
E N D
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 • 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
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 • [ $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 • An example: for i in `ls` do if [ -d $i ] then echo $i fi done
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 • 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 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 • 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 • 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 • 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 • 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 - 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 • If-statements if test-cmds then commands else commands fi • Else portion is optional
Shell Scripts - Conditionals • Multiple If-statements in a row if test-cmds then commands elif test-cmds then commands else commands fi
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 • While-loops while test-cmds do commands done
Shell Scripts - Loops • For-loops for x in list do commands done
Shell Scripts - Loops • Example: for x in 1 2 3 4 5 do echo “$x” done • Outputs: 1 2 3 4 5
Shell Scripts - Loops • Example: x=1 while [ $x -le 3 ] do echo “$x” x=`expr $x + 1` done • Outputs: 1 2 3
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 • Print out the greatest of the variables $1, $2 and $3 (These represent the first 3 command-line arguments passed to the script)