Shell Programming

Transcription

Shell Programming
Shell Programming
Different shells (e.g. bash, ksh, tcsh, ash, sh) => different commands/scripts
Why a shell script?
• simple way to string together a bunch of UNIX-commands
• scripts are usually fast to get going
• portable across the whole UNIX world
Nevertheless: scripts are controversial.
• syntax is often ambiguous, wrong documented
• interpretation sometimes leads to surprising results
Helpful webpages:
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://tldp.org/LDP/abs/html/
http://www.google.de
23.10.2008 | Introduction to Mathematical Software | 18
Organisation
google
23.10.2008 | Introduction to Mathematical Software | 19
Organisation
google
23.10.2008 | Introduction to Mathematical Software | 20
Google (part 1): Reading your mind
see http://www.ams.org/featurecolumn/archive/pagerank.html
The problem:
given is
• a library with 25 billion documents
• no centralized organisation
• no librarians
• anyone can add documents
You are interested in information. You only know some keywords.
further complication:
Google claims more then 25 billion indexed pages. 95% of the text in
the Web is composed of only some 1,000 words. How can we
distinguish the important pages from the unimportant ones?
Impossible?
23.10.2008 | Introduction to Mathematical Software | 21
Google (part 1): Reading your mind
The heart of the google software is the PageRank algorithm.
hjdsjfsfsjfs sfsdfdsfdsfsd dsfsdfdsfdsfds
sdfsdfds fds fsdf s df fds f
sdfsd f dsf sd f ds fds fsd f ds fds fd f
fdsfdsfdsfdsfdsfds fdsfdsfd
wefwefweefewerr wewrer wer wer wwer rewrrewr
ewr wrerewrewwerr wer werewr n
fdmnsfmnfmsdfds,m,ms dm,vf,msdf,ndsnfads nfn
asnm,dmc sdm,nsd sfsd,fn,sdnfm,sdf s
f,sdn,mfns d
hjdsjfsfsjfs sfsdfdsfdsfsd dsfsdfdsfdsfds
sdfsdfds fds fsdf s df fds f
sdfsd f dsf sd f ds fds fsd f ds fds fd f
fdsfdsfdsfdsfdsfds fdsfdsfd
wefwefweefewerr wewrer wer wer wwer rewrrewr
ewr wrerewrewwerr wer werewr n
fdmnsfmnfmsdfds,m,ms dm,vf,msdf,ndsnfads nfn
asnm,dmc sdm,nsd sfsd,fn,sdnfm,sdf s
f,sdn,mfns d
(1)
(2)
hjdsjfsfsjfs sfsdfdsfdsfsd dsfsdfdsfdsfds
sdfsdfds fds fsdf s df fds f
sdfsd f dsf sd f ds fds fsd f ds fds fd f
fdsfdsfdsfdsfdsfds fdsfdsfd
wefwefweefewerr wewrer wer wer wwer rewrrewr
ewr wrerewrewwerr wer werewr n
fdmnsfmnfmsdfds,m,ms dm,vf,msdf,ndsnfads nfn
asnm,dmc sdm,nsd sfsd,fn,sdnfm,sdf s
f,sdn,mfns d
dsbfsbdfm,dsnm,fs,mdnfm,sdnm,fvm,s dnmvfsd
m,vfm,ds m,vsdm,
fm,sdm,vfsdm,vfmsdnmf,ds mnf,mdsnfk,grahds
ekltgfl kewsfnjöklewjfrl kewnhkfnweklcfj el kwfl kw
englkreuigtwzuifhl kwflieq wfl kjensdklfel kwghfl kw
enfl kewzl kfjews
dsbfsbdfm,dsnm,fs,mdnfm,sdnm,fvm,s dnmvfsd
m,vfm,ds m,vsdm,
fm,sdm,vfsdm,vfmsdnmf,ds mnf,mdsnfk,grahds
ekltgfl kewsfnjöklewjfrl kewnhkfnweklcfj el kwfl kw
englkreuigtwzuifhl kwflieq wfl kjensdklfel kwghfl kw
enfl kewzl kfjews
wfej khfiouewhfklewfpioq wöfjewöofjowejfoöewj ö
oäfj weofjöwekjföwejfpoiewjföowejföjewofewl ksö
fmnöl- eäwsfw
e
wfklj weifeki wfewhfiewnhl kfqnjöoiqüäöj mdöläqj w
edföl kqwej fdöqwj e
e
fekl emnl kfnewklfnel kwnfl kewhtl kwefrdöqewjdfq
öwj döqwjr dföl kwqjrlokeq wklfel kwjfnklewfrl kewjf
rlkewfnk.,ewnfj kl ewjfws enfl kewrölqjröl3qiro3poi
r54urjtewnjhas.kfcnds k.a,frl wei uri2l krl krfeqhwkj
rfhiouqwehfiqrki3erfpiewqrfewhurp1queoriu3r 4e
po32qjhwr f.kwföl kq wujrej k
wfej khfiouewhfklewfpioq wöfjewöofjowejfoöewj ö
oäfj weofjöwekjföwejfpoiewjföowejföjewofewl ksö
fmnöl- eäwsfw
e
wfklj weifeki wfewhfiewnhl kfqnjöoiqüäöj mdöläqj w
edföl kqwej fdöqwj e
e
fekl emnl kfnewklfnel kwnfl kewhtl kwefrdöqewjdfq
öwj döqwjr dföl kwqjrlokeq wklfel kwjfnklewfrl kewjf
rlkewfnk.,ewnfj kl ewjfws enfl kewrölqjröl3qiro3poi
r54urjtewnjhas.kfcnds k.a,frl wei uri2l krl krfeqhwkj
rfhiouqwehfiqrki3erfpiewqrfewhurp1queoriu3r 4e
po32qjhwr f.kwföl kq wujrej k
dsbfsbdfm,dsnm,fs,mdnfm,sdnm,fvm,s dnmvfsd
m,vfm,ds m,vsdm,
fm,sdm,vfsdm,vfmsdnmf,ds mnf,mdsnfk,grahds
ekltgfl kewsfnjöklewjfrl kewnhkfnweklcfj el kwfl kw
englkreuigtwzuifhl kwflieq wfl kjensdklfel kwghfl kw
enfl kewzl kfjews
wfej khfiouewhfklewfpioq wöfjewöofjowejfoöewj ö
oäfj weofjöwekjföwejfpoiewjföowejföjewofewl ksö
fmnöl- eäwsfw
e
wfklj weifeki wfewhfiewnhl kfqnjöoiqüäöj mdöläqj w
edföl kqwej fdöqwj e
e
fekl emnl kfnewklfnel kwnfl kewhtl kwefrdöqewjdfq
öwj döqwjr dföl kwqjrlokeq wklfel kwjfnklewfrl kewjf
rlkewfnk.,ewnfj kl ewjfws enfl kewrölqjröl3qiro3poi
r54urjtewnjhas.kfcnds k.a,frl wei uri2l krl krfeqhwkj
rfhiouqwehfiqrki3erfpiewqrfewhurp1queoriu3r 4e
po32qjhwr f.kwföl kq wujrej k
(6)
2whrökwlqjrfpoiq wzhrl kq wejrtfewiofugioewthj nl
kwerufiow
qghefjqwhjfiuqwehrfehq wköfj
Let P be a web page.
We call I(P) the importance of P.
2whrökwlqjrfpoiq wzhrl kq wejrtfewiofugioewthj nl
kwerufiow
qghefjqwhjfiuqwehrfehq wköfj
hjdsjfsfsjfs sfsdfdsfdsfsd dsfsdfdsfdsfds
sdfsdfds fds fsdf s df fds f
sdfsd f dsf sd f ds fds fsd f ds fds fd f
fdsfdsfdsfdsfdsfds fdsfdsfd
wefwefweefewerr wewrer wer wer wwer rewrrewr
ewr wrerewrewwerr wer werewr n
fdmnsfmnfmsdfds,m,ms dm,vf,msdf,ndsnfads nfn
asnm,dmc sdm,nsd sfsd,fn,sdnfm,sdf s
f,sdn,mfns d
2whrökwlqjrfpoiq wzhrl kq wejrtfewiofugioewthj nl
kwerufiow
qghefjqwhjfiuqwehrfehq wköfj
dsbfsbdfm,dsnm,fs,mdnfm,sdnm,fvm,s dnmvfsd
m,vfm,ds m,vsdm,
fm,sdm,vfsdm,vfmsdnmf,ds mnf,mdsnfk,grahds
ekltgfl kewsfnjöklewjfrl kewnhkfnweklcfj el kwfl kw
englkreuigtwzuifhl kwflieq wfl kjensdklfel kwghfl kw
enfl kewzl kfjews
wfej khfiouewhfklewfpioq wöfjewöofjowejfoöewj ö
oäfj weofjöwekjföwejfpoiewjföowejföjewofewl ksö
fmnöl- eäwsfw
e
wfklj weifeki wfewhfiewnhl kfqnjöoiqüäöj mdöläqj w
edföl kqwej fdöqwj e
e
fekl emnl kfnewklfnel kwnfl kewhtl kwefrdöqewjdfq
öwj döqwjr dföl kwqjrlokeq wklfel kwjfnklewfrl kewjf
rlkewfnk.,ewnfj kl ewjfws enfl kewrölqjröl3qiro3poi
r54urjtewnjhas.kfcnds k.a,frl wei uri2l krl krfeqhwkj
rfhiouqwehfiqrki3erfpiewqrfewhurp1queoriu3r 4e
po32qjhwr f.kwföl kq wujrej k
2whrökwlqjrfpoiq wzhrl kq wejrtfewiofugioewthj nl
kwerufiow
qghefjqwhjfiuqwehrfehq wköfj
Let Pj have lj many outgoing
links.
If Pi is such a page, Pj will pass
1/lj „importance“ to Pi.
(5)
hjdsjfsfsjfs sfsdfdsfdsfsd dsfsdfdsfdsfds
sdfsdfds fds fsdf s df fds f
sdfsd f dsf sd f ds fds fsd f ds fds fd f
fdsfdsfdsfdsfdsfds fdsfdsfd
wefwefweefewerr wewrer wer wer wwer rewrrewr
ewr wrerewrewwerr wer werewr n
fdmnsfmnfmsdfds,m,ms dm,vf,msdf,ndsnfads nfn
asnm,dmc sdm,nsd sfsd,fn,sdnfm,sdf s
f,sdn,mfns d
hjdsjfsfsjfs sfsdfdsfdsfsd dsfsdfdsfdsfds
sdfsdfds fds fsdf s df fds f
sdfsd f dsf sd f ds fds fsd f ds fds fd f
fdsfdsfdsfdsfdsfds fdsfdsfd
wefwefweefewerr wewrer wer wer wwer rewrrewr
ewr wrerewrewwerr wer werewr n
fdmnsfmnfmsdfds,m,ms dm,vf,msdf,ndsnfads nfn
asnm,dmc sdm,nsd sfsd,fn,sdnfm,sdf s
f,sdn,mfns d
dsbfsbdfm,dsnm,fs,mdnfm,sdnm,fvm,s dnmvfsd
m,vfm,ds m,vsdm,
fm,sdm,vfsdm,vfmsdnmf,ds mnf,mdsnfk,grahds
ekltgfl kewsfnjöklewjfrl kewnhkfnweklcfj el kwfl kw
englkreuigtwzuifhl kwflieq wfl kjensdklfel kwghfl kw
enfl kewzl kfjews
dsbfsbdfm,dsnm,fs,mdnfm,sdnm,fvm,s dnmvfsd
m,vfm,ds m,vsdm,
fm,sdm,vfsdm,vfmsdnmf,ds mnf,mdsnfk,grahds
ekltgfl kewsfnjöklewjfrl kewnhkfnweklcfj el kwfl kw
englkreuigtwzuifhl kwflieq wfl kjensdklfel kwghfl kw
enfl kewzl kfjews
wfej khfiouewhfklewfpioq wöfjewöofjowejfoöewj ö
oäfj weofjöwekjföwejfpoiewjföowejföjewofewl ksö
fmnöl- eäwsfw
e
wfklj weifeki wfewhfiewnhl kfqnjöoiqüäöj mdöläqj w
edföl kqwej fdöqwj e
e
fekl emnl kfnewklfnel kwnfl kewhtl kwefrdöqewjdfq
öwj döqwjr dföl kwqjrlokeq wklfel kwjfnklewfrl kewjf
rlkewfnk.,ewnfj kl ewjfws enfl kewrölqjröl3qiro3poi
r54urjtewnjhas.kfcnds k.a,frl wei uri2l krl krfeqhwkj
rfhiouqwehfiqrki3erfpiewqrfewhurp1queoriu3r 4e
po32qjhwr f.kwföl kq wujrej k
wfej khfiouewhfklewfpioq wöfjewöofjowejfoöewj ö
oäfj weofjöwekjföwejfpoiewjföowejföjewofewl ksö
fmnöl- eäwsfw
e
wfklj weifeki wfewhfiewnhl kfqnjöoiqüäöj mdöläqj w
edföl kqwej fdöqwj e
e
fekl emnl kfnewklfnel kwnfl kewhtl kwefrdöqewjdfq
öwj döqwjr dföl kwqjrlokeq wklfel kwjfnklewfrl kewjf
rlkewfnk.,ewnfj kl ewjfws enfl kewrölqjröl3qiro3poi
r54urjtewnjhas.kfcnds k.a,frl wei uri2l krl krfeqhwkj
rfhiouqwehfiqrki3erfpiewqrfewhurp1queoriu3r 4e
po32qjhwr f.kwföl kq wujrej k
hjdsjfsfsjfs sfsdfdsfdsfsd dsfsdfdsfdsfds
sdfsdfds fds fsdf s df fds f
sdfsd f dsf sd f ds fds fsd f ds fds fd f
fdsfdsfdsfdsfdsfds fdsfdsfd
wefwefweefewerr wewrer wer wer wwer rewrrewr
ewr wrerewrewwerr wer werewr n
fdmnsfmnfmsdfds,m,ms dm,vf,msdf,ndsnfads nfn
asnm,dmc sdm,nsd sfsd,fn,sdnfm,sdf s
f,sdn,mfns d
dsbfsbdfm,dsnm,fs,mdnfm,sdnm,fvm,s dnmvfsd
m,vfm,ds m,vsdm,
fm,sdm,vfsdm,vfmsdnmf,ds mnf,mdsnfk,grahds
ekltgfl kewsfnjöklewjfrl kewnhkfnweklcfj el kwfl kw
englkreuigtwzuifhl kwflieq wfl kjensdklfel kwghfl kw
enfl kewzl kfjews
2whrökwlqjrfpoiq wzhrl kq wejrtfewiofugioewthj nl
kwerufiow
qghefjqwhjfiuqwehrfehq wköfj
(3)
wfej khfiouewhfklewfpioq wöfjewöofjowejfoöewj ö
oäfj weofjöwekjföwejfpoiewjföowejföjewofewl ksö
fmnöl- eäwsfw
e
wfklj weifeki wfewhfiewnhl kfqnjöoiqüäöj mdöläqj w
edföl kqwej fdöqwj e
e
fekl emnl kfnewklfnel kwnfl kewhtl kwefrdöqewjdfq
öwj döqwjr dföl kwqjrlokeq wklfel kwjfnklewfrl kewjf
rlkewfnk.,ewnfj kl ewjfws enfl kewrölqjröl3qiro3poi
r54urjtewnjhas.kfcnds k.a,frl wei uri2l krl krfeqhwkj
rfhiouqwehfiqrki3erfpiewqrfewhurp1queoriu3r 4e
po32qjhwr f.kwföl kq wujrej k
(4)
2whrökwlqjrfpoiq wzhrl kq wejrtfewiofugioewthj nl
kwerufiow
qghefjqwhjfiuqwehrfehq wköfj
(7)
Let Bi be the set of pages linking
to Pi. Then the importance
relation between a page and its
neighbours is as follows:
2whrökwlqjrfpoiq wzhrl kq wejrtfewiofugioewthj nl
kwerufiow
qghefjqwhjfiuqwehrfehq wköfj
I(Pi) := ∑ I(Pj)/lj
Pj ∈ B i
23.10.2008 | Introduction to Mathematical Software | 22
Google (part 1): Reading your mind
(1)
hjdsjfsfsjf
s
sfsdfdsfds
fsd
dsfsdfdsfd
sfds
sdfsdfds
fds fsdf
sdf fds f
sdfsd f dsf
sd f ds fds
fsd f ds
fds fd f
fdsfdsfdsf
dsfdsfdsfd
sfdsfd
hjdsjfsfsjf
s
sfsdfdsfds
fsd
dsfsdfdsfd
sfds
sdfsdfds
fds fsdf
sdf fds f
sdfsd f dsf
sd f ds fds
fsd f ds
fds fd f
fdsfdsfdsf
dsfdsfdsfd
sfdsfd
rwer were
wr n
(2)
hjdsjfsfsjf
s
sfsdfdsfds
fsd
dsfsdfdsfd
sfds
sdfsdfds
fds fsdf
sdf fds f
sdfsd f dsf
sd f ds fds
fsd f ds
fds fd f
fdsfdsfdsf
dsfdsfdsfd
sfdsfd
qghefjqwh
jfiuqwehrf
ehqwköfj
hjdsjfsfsjf
s
sfsdfdsfds
fsd
dsfsdfdsfd
sfds
sdfsdfds
fds fsdf
sdf fds f
sdfsd f dsf
sd f ds fds
fsd f ds
fds fd f
fdsfdsfdsf
dsfdsfdsfd
sfdsfd
qghefjqwh
jfiuqwehrf
ehqwköfj
hjdsjfsfsjf
s
sfsdfdsfds
fsd
dsfsdfdsfd
sfds
sdfsdfds
fds fsdf
sdf fds f
sdfsd f dsf
sd f ds fds
fsd f ds
fds fd f
fdsfdsfdsf
dsfdsfdsfd
sfdsfd
hjdsjfsfsjf
s
sfsdfdsfds
fsd
dsfsdfdsfd
sfds
sdfsdfds
fds fsdf
sdf fds f
sdfsd f dsf
sd f ds fds
fsd f ds
fds fd f
fdsfdsfdsf
dsfdsfdsfd
sfdsfd
qghefjqwh
jfiuqwehrf
ehqwköfj
(3)
qghefjqwh
jfiuqwehrf
ehqwköfj
I(Pi) := ∑ I(Pj)/lj
(6)
(5)
hjdsjfsfsjf
s
sfsdfdsfds
fsd
dsfsdfdsfd
sfds
sdfsdfds
fds fsdf
sdf fds f
sdfsd f dsf
sd f ds fds
fsd f ds
fds fd f
fdsfdsfdsf
dsfdsfdsfd
sfdsfd
qghefjqwh
jfiuqwehrf
ehqwköfj
(4)
(7)
Define a matrix H = (hij) with
hij :=
H=
0
½
0
0
½
0
0
0
0
0
0
1
0
0
½
0
0
½
0
0
0
?? chicken vs. egg problem
Pj ∈ B i
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
½
0
½
0
0
0
0
1
0
0
1/lj
0
and a vector I of PageRanks: I :=
Then I = H*I.
0
1
0
0
1
0
0
In other words:
I is eigenvector of the matrix H with eigenvalue 1.
to be continued ... soon
23.10.2008 | Introduction to Mathematical Software | 23
if Pj ∈ Bi
otherwise
Organisation
How to
get a
shell
23.10.2008 | Introduction to Mathematical Software | 24
The bash
A first example
#!/bin/bash -vx
echo "My first script" > fst_exa.txt
cat fst_exa.txt
cat fst_exa.txt | wc
rm fst_exa.txt
The script has got the name fst_exa.bash.The ‚correct‘ way to
start is /bin/bash fst_exa.bash. Shorter: ./fst_exa.bash
Another simple example, using parameters def_params.bash
#!/bin/bash
echo "Script name is
echo "First Parameter is
echo "Second Parameter is
echo "Process ID is
echo "Parameter Count is
echo "All Parameters
echo "The FLAGS are
23.10.2008 | Introduction to Mathematical Software | 25
[$0]"
[$1]"
[$2]"
[$$]"
[$#]"
[$@]"
[$-]"
The bash
Useful example-commands for scripting are
sort
grep
basename
dirname
cut
wc
tr 'a' 'b'
expr
eval
echo
date
head | tail
tar
sorts lines in various orders
searches for expressions in strings or files
strips the path from a path string to leave just the filename
removes the file from a path string to leave just the pathname
prints selected parts of lines from a file to stdout cf.
unix_for_while.bash
count the characters, words, or lines
transform characters unix_tr.bash
simple arithmetic processor example in unix_expr.bash
evaluate variables example in unix_eval.bash
output strings
shows a date string
access head- or tail-lines in files
packing or unpacking an archive (a collection of several files in one file)
23.10.2008 | Introduction to Mathematical Software | 26
The bash
Accents and quotations unix_accents_quotes.bash
(be careful with spaces!)
#!/bin/sh
# This is a comment!
echo "Hello World"
# This is a comment, too!
echo "Hello World"
echo "Hello * World"
echo Hello * World
echo Hello World
echo "Hello" World
echo Hello “
" World
echo "Hello \"*\" World"
echo `hello` world
echo 'hello' world
23.10.2008 | Introduction to Mathematical Software | 27
The bash
Complex Commands group simple commands into control sets.
• loop structures:
• for name [in ...] do list-of-commands done
alphabet="a b c d e"
#
count=0
#
for letter in $alphabet
#
do
#
count=`expr $count + 1`
#
echo "Letter $count is [$letter]" #
done
#
23.10.2008 | Introduction to Mathematical Software | 28
Initialise a string
Initialise a counter
Set up a loop control
Begin the loop
Increment the counter
Display the result
End of loop
The bash
Complex Commands group simple commands into control sets.
• loop structures:
• while condition do list-of-commands done
unix_for_while.bash
alphabet="a b c d e"
# Initialise a string
count=0
# Initialise a counter
while [ $count -lt 5 ]
# Set up a loop control
do
# Begin the loop
count=`expr $count + 1`
# Increment the counter
position=`bc $count + $count - 1`
# Position of next letter
letter=`echo "$alphabet" | cut -c$position-$position`
# Get next letter
echo "Letter $count is [$letter]"
# Display the result
done
23.10.2008 | Introduction to Mathematical Software | 29
The bash
Complex Commands group simple commands into control sets.
• the if structure
• if condition1 then list-of-commands1
[elif condition2 then list-of-commands2] ...
[else list-of-commandsn] fi
Example: unix_test_if.bash
#!/bin/bash -vx
if test -w $1
then
echo "File $1 existiert" #-w file exists and write-rights
fi
#in the following: [...] implicitely calls "test"
if [ -f $1/$2 ] #-f file exists and is simple file
then
echo "This filename [$2] exists"
elif [ -d $1 ] #-d directory exists
then
echo "This dirname [$1] exists"
else
echo "Neither [$1] nor [$2] exist"
fi
23.10.2008 | Introduction to Mathematical Software | 30

Similar documents