<<

Make

Shell Scripts: working faster

K. Cooper

2012 Line

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 to get automatic word completion Works for both commands and filenames If you hit twice, gives list of options The up- and down-arrow keys let you choose a command from those you have typed recently You can use left and right arrows and backspace to edit commands Make Command Line

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 using braces { }. Strings can be any length Strings separated by commas Make Regular Expressions

Character Matches

Can match characters from list using brackets [ ]. 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 that begins with three digits [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*. [34]*.ps prints only some files Make Regular Expressions

Redirects

Can send output of a command to a file > sends output from command left to file on right < uses contents of file on right as standard input for command at left 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| root ls -l |grep - .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=‘‘ 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 . -mtime +30 > contents for currentfile in ‘ contents‘; do if [ " -c $currentfile" > 100000 ]; then rm $currentfile fi done Make Scripts

for

Creates a variable 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 [ ]; 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? - : is it writable? -x : is it executable?