Make
Shell Scripts: working faster
K. Cooper
Typed Commands
Harder to learn Faster Use tab completion, regular expressions, and wildcards to go faster Can be scripted to work even faster Remote access is faster Easier to instruct others how to do things tar -czvf texfiles.tgz *.tex Make Command Line
Tab completion
Hit
Wildcards
The asterisk (*) matches anything Can match one character or many The question mark matches one character only Make Regular Expressions
Lists
Can give a list of matching strings using braces { }. Strings can be any length Strings separated by commas Make Regular Expressions
Character Matches
Can match characters from list using brackets [ ]. Shell tries to match each character from the brackets in succession Character collections can be given using hyphens Make Regular Expressions
Examples
rm -rf [0-9][0-9][0-9]* removes any file or directory that begins with three digits ls [A-Z]*.pdf lists any PDF file that begins with a capital letter ls [AZ]*.tex,pdf lists any PDF or TEX file that starts with A or Z lpr -Phpr3 273_03*.ps [34]*.ps prints only some files Make Regular Expressions
Redirects
Can send output of a command to a file > sends output from command at left to file on right < uses contents of file on right as standard input for command at left echo Hello, world > hi.txt Make Regular Expressions
Pipes
Can send output of a command to standard input of another command | sends output of command at left to input of command at right ps aux|grep root ls -l |grep -v .tex Make Regular Expressions
Backquotes
Can use result of a command as argument for another command rm ‘ls|grep -v .tex‘ Make Regular Expressions
Quotes
Frequently needed to handle spaces and special characters Double quotes allow variables to be evaluated Single quotes make a string fixed - no evaluation Make Regular Expressions
Quotes
Frequently needed to handle spaces and special characters Double quotes allow variables to be evaluated Single quotes make a string fixed - no evaluation Make Regular Expressions
Quotes
Frequently needed to handle spaces and special characters Double quotes allow variables to be evaluated Single quotes make a string fixed - no evaluation Make Regular Expressions
Quotes
Frequently needed to handle spaces and special characters Double quotes allow variables to be evaluated Single quotes make a string fixed - no evaluation echo "My shell is $SHELL" echo ’My shell is $SHELL’ Make Regular Expressions
Variables
Assign using equals sign MYDIR=‘pwd‘ Evaluate using a dollar sign echo $MYDIR Make Scripts
Scripts
Allow you to put together many commands Automate complicated file handling tasks Make decisions Make Scripts
Example
#!/bin/bash find . -mtime +30 > contents for currentfile in ‘cat contents‘; do if [ "wc -c $currentfile" > 100000 ]; then rm $currentfile fi done Make Scripts
for
Creates a variable which takes the value of each entry in a list foreach variable in list; do commands; done The list could be the result of a command, contents of a file, etc. Make Scripts
for
Creates a variable which takes the value of each entry in a list foreach variable in list; do commands; done The list could be the result of a command, contents of a file, etc. Make Scripts
for
Creates a variable which takes the value of each entry in a list foreach variable in list; do commands; done The list could be the result of a command, contents of a file, etc. Make Scripts
for
Creates a variable which takes the value of each entry in a list foreach variable in list; do commands; done The list could be the result of a command, contents of a file, etc. for arg in Calculus "Differential Equations" "Functional Analysis"; do echo $arg; done Make Scripts
if
Test the truth value of an expression if [ expr ]; then commands; fi Make Scripts
if
Test the truth value of an expression if [ expr ]; then commands; fi Make Scripts
if
Test the truth value of an expression if [ expr ]; then commands; fi if [ -e ./math]; then ./math; fi Make Scripts
tests
test has many switches to check file properties -e : does the file exist? -f : is it a regular file? -d : is it a directory? -r : is it readable? -w : is it writable? -x : is it executable?