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 L ess T han, L ess than or E qual test
[ $i -gt 0 ], [ $i -ge 5 ]
– integer G reater T han, G reater than or E qual 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, whitespace 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`
(WRONG!!!!!!)
(WRONG!!!!!!)
– var = `expr $t + 2` (WRONG!!!!!!)
– var=`expr $t + 2` 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:
3
4
1
2
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 commandline arguments passed to the script)