Cursul #6
Automatizarea sarcinilor. Shell Scripting
1
Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
Jamie Zawinski (JWZ)
2
Suport de curs
Capitolul 13 - Automatizarea sarcinilor
3
Ce este un Script Shell
4
Perspective pentru shell scripting
5
Rulare script
6
Exemplu Shell Script
7
#!/bin/bash
�sudo mdutil -a -i off
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist
sudo mdutil -a -i on
sudo mdutil -E /
Utilizarea Shell Scripting
8
One liner
9
Dezvoltarea de scripturi shell
10
Comenzi existente
11
Înlănțuire de comenzi
12
Variabile
13
Controlul fluxului
14
Construcția if
15
if comanda1; then
comanda2
else
comanda3
fi
if test –f ”$file”; then
echo ”File $file exists.”
fi
if test ”$#” –ne 1; then
echo ”Usage: $0 argument”
exit 1
fi
Construcția for
asemănătoare cu o
listă
16
for i in list; do
command1
command2 ...
done
for user in $(cut –d ‘:’ –f 1); do
nlogins=$(last ”$user” | grep ”^$user” | wc -l)
echo ”User $user logged in $nlogins times recently.”
done
Construcția while read
cut, mai slab decât
awk
17
while read field1 field2 ...; do
command1
command2 ...
done < file�
IFS=”:”; while read user x y z t home v; do echo ”$user:$home”;
done < /etc/passwd
For vs while
18
Funcții
19
Exemplu funcții
20
#!/bin/bash
OFFLINEIMAP=/opt/local/bin/offlineimap
LOGFILE=~/.log/offlineimap-cron.log
function check_alive()
{
ps -ef | grep "$OFFLINEIMAP" | grep -v grep > /dev/null 2>&1
}
�check_alive
if test $? -ne 0; then
nohup nice -n 19 "$OFFLINEIMAP" >> "$LOGFILE" 2>&1 &
fi
�exit 0
Expresii regulate - Utilizare
21
Când să nu folosești expresii regulate
22
Metacaractere în expresii regulate
23
Exemple de expresii regulate
24
Regex vs globbing
25
Suport expresii regulate
26
Sfaturi Shell Scripting
27
Regular Expressions Cookbook
28
Larry Wall
29
Lua
30
Cuvinte cheie
31