Lecture 4
Lecture 4
Purpose, History
Unix scripting
Unix failings
Other solutions
Scripting is...
‘Easier’
Glue
Weakly typed
Interpreted
Who scripts?
Users
Power users
Administrators
Developers
Testers
Developments
Job Control Language
1960s Unix pipe
1993 Applescript
2005 Automator
2006 Windows PowerShell
Available shells in Linux
bash, sh, tcsh, csh; use cat /etc/shells
Unix Shell Scripting
Unix Philosophy
#!/bin/sh
echo “Hello, World!”
$ chmod +x ./hello
$ ./hello
Hello, World!
The correct UNIX way
$ sh ./hello
Hello, World!
echo -e “no newline\c”
Another example
#!/bin/bash
clear
echo "This is information provided by mysystem.sh. Program starts
now."
echo "Hello, $USER"
echo
echo "Today's date is `date`, this is week `date +"%V"`."
echo
echo "These users are currently connected:"
w | cut -d " " -f 1 | grep -v USER | sort -u
echo
echo "This is `uname -s` running on a `uname -m` processor."
echo
echo "This is the uptime information:"
uptime
echo
echo "That's all folks!"
#! “Sh-Bang”
First line
#!/bin/sh
#!/usr/bin/perl -wnl
#!/usr/bin/env python
Default is /bin/sh
SetUID not honoured
# is also used for comments
Design Patterns
Source: ls
read from file and write to stdout
Filter: sort
read from stdin and write to stdout
Sink: less
read from stdin and write to file
“Cantrip”: rm
do something but return nothing
Compiler: tar
read from file and write to another file
Good scripts
A sensible name
don’t clash with existing commands and
programs
No errors
Perform the intended task
Have a clear logic
Efficient, no unnecessary work
Informative, notifying users about what it is
doing
Reusable
BASH basics
Files read by bash
/etc/profile, .bash_profile, .bashrc
depending on login, interactive, non-interactive, or use
sh directly
Built-in commands like cd and eval, exit, exec, export, …
Three types of commands
built-in, function, executable programs
debugging a script: bash -xv script_file
Some self-study required. Read Bash Beginners Guide
Environment Variables
Environment variable
A variable with name and value used by shells and processes
Use printenv or env to find them
They can be set by
Globally, /etc/profile, /etc/bash.bashrc
Per user, ~/.bash_profile,~/.bashrc, ~/.profile
Non login shell, non interactive shell (shell scripts)
/etc/profile, ~/.bash_profile, ~/.bash_logout
Used by login shells
/etc/bash.bashrc, ~/.bashrc
used by interactive, non-login shells
For details: https://wiki.archlinux.org/index.php/
environment_variables
I/O Channels
$varname Deference
Global and local variables
Seen by subshell/child processes if
export PATH=$HOME/bin:$PATH
Beware white-space!
varname=”foo bar”
Interpolation
‘non-interpolated string’
`command`
foo=`command \`command\``
foo=$(command $(command)) (Bash specific)
html=”$1”; txt=”${html:%.html}.txt”
links -dump “$html” > “$txt”
Conditions—if
see test(1)
if␣[␣$# -lt 2 ]; then
if-less-than-two-arguments
elif␣[␣\(␣“$1”␣=␣‘foo’␣\)␣-a␣\
\(␣-r␣/etc/foorc␣\)␣]; then
if-arg1-is-foo-and-foorc-is-readable
else
if-otherwise
fi
while true
do
infinite loop body
done
Subshells
expr 2 \* 8
16
echo $((2 * 8)) Bash-ism
16
echo 'scale=2; 1/3' | bc
.33
echo 'ibase=10; obase=2; 192' | bc
11000000
Sed and Awk
Read a book!
Regular expressions!
Takes a while to learn
A few recipes are useful
sed—Stream Editor
Delete header on first line
sed -e 1d
Collation
echo -e '1\n2\n3\n4' | awk '
BEGIN{sum=0;max="?"}
max=="?"{max=$1}
{sum+=$1}
$1>max{max=$1}
END{print "Avg:" sum/NR "\nMax:" max}'
A command a day...
“Prayerful parsing”
I/O is expensive
Interface inconsistency
Lack of re-use
Security: TOCTTOU
rm /tmp/*/* (find /tmp -not-accessed-
recently | xargs rm)
Other Systems
Applescript example
Is 10% of disk available?
https://developer.apple.com/library/mac/documentation/applescript/conceptual/
applescriptlangguide/conceptual/ASLR_lexical_conventions.html#//apple_ref/doc/uid/
TP40000983-CH214-SW1
tell application "Finder"
set the percent_free to ¬
(((the free space of the startup disk) / ¬
(the capacity of the startup disk)) * 100) div 1
end tell
if the percent_free is less than 10 then
tell application (path to frontmost application as text)
display dialog "The startup disk has only " & ¬
the percent_free & ¬
" percent of its capacity available." & return & return & ¬
"Should this script continue?" with icon 1
end tell
end if
PowerShell examples