curs 3 - shell scripting....
TRANSCRIPT
Curs 3Shell scripting. Automatizare
Gestiunea Serviciilor de Ret,ea
20 octombrie 2011
GSR Curs 3, Shell scripting. Automatizare 1/44
Interact, iunea cu shell-ul
Shell scripting
Shell programming
Filtre de text
find
Globbing s, i expresii regulate
cron
Keywords
Intrebari
GSR Curs 3, Shell scripting. Automatizare 3/44
Prerequisites
I “Cartea de USO”:http://books.google.com/books?id=_JFGzyRxQGcC
I Capitolul 12 – Shell scripting
GSR Curs 3, Shell scripting. Automatizare 4/44
Suport
I “Unix and Linux System Administration”I Chapter 2 – Scripting and the Shell (pg. 29-53, pg. 73-76)I Chapter 9 – Periodic Processes
GSR Curs 3, Shell scripting. Automatizare 5/44
Outline
Interact, iunea cu shell-ul
Shell scripting
Shell programming
Filtre de text
find
Globbing s, i expresii regulate
cron
Keywords
Intrebari
GSR Curs 3, Shell scripting. Automatizare 6/44
Shell
I interpretor de comenzi
I /bin/bash, /bin/sh, /bin/dash, /bin/tcsh
I operatori shell
I redirectare
I ınlant,uirea comenzilor
I programare
I variabile
I comenzi de tip instruct, iuni (if, for, while)
GSR Curs 3, Shell scripting. Automatizare 7/44
Comenzi interne s, i comenzi externe
I built-in commandsI pwd, cd, exit, set, ulimit, exec, source, ., :I help source
I comenzi externeI ls, chmod, adduser, ifconfig, lsmodI man adduser, info ls
I type
razvan@valhalla:~$ type lsmod
lsmod is /bin/lsmod
razvan@valhalla:~$ type source
source is a shell builtin
GSR Curs 3, Shell scripting. Automatizare 9/44
Tipuri de utilizare a Bash
I interactivI neinteractiv
I bash test.shI bash -c “ls”
I shell de loginI /etc/profile, ∼/.bash profile, ∼/.bash login, ∼/.profile,∼/.bash logout
I shell non-loginI ∼/.bashrc (pentru shell interactiv)
GSR Curs 3, Shell scripting. Automatizare 10/44
Operatori shell
I secvent, iereI &&, ;, ||, &
I redirectareI >, <, 2>, 2>&1, &>, >>, <<
I ınlant,uire comenziI |I <(command)
GSR Curs 3, Shell scripting. Automatizare 11/44
Outline
Interact, iunea cu shell-ul
Shell scripting
Shell programming
Filtre de text
find
Globbing s, i expresii regulate
cron
Keywords
Intrebari
GSR Curs 3, Shell scripting. Automatizare 12/44
Script shell
I comenzi s, i operatori shell
I automatizare
I ıncepe, de obicei, cu #!/bin/bashI rulare
I bash hello.bashI chmod a+x hello.bash && ./hello.bash
GSR Curs 3, Shell scripting. Automatizare 13/44
Script shell (2)
razvan@valhalla:~/curs-05$ cat hello.bash
#!/bin/bash
echo "Hello, World"
exit 0
razvan@valhalla:~/curs-05$ bash hello.bash
Hello, World
razvan@valhalla:~/curs-05$ chmod a+x hello.bash
razvan@valhalla:~/curs-05$ ./hello.bash
Hello, World
GSR Curs 3, Shell scripting. Automatizare 14/44
Cum arata, ın general, un shell script?
I o ınlant,uire de comenzi
I nu apar for, while, until, case – doar pentru cazuri speciale
I variabile, expandare de comenzi, opt, iuni utile
I filtre de text, prelucrare de text
GSR Curs 3, Shell scripting. Automatizare 15/44
Cand/De ce shell scripting?
I automatizare + lene
I doua sau mai multe comenzi frecvente secvent, iale
I hook-uri, conectarea de alte act, iuni
GSR Curs 3, Shell scripting. Automatizare 16/44
Outline
Interact, iunea cu shell-ul
Shell scripting
Shell programming
Filtre de text
find
Globbing s, i expresii regulate
cron
Keywords
Intrebari
GSR Curs 3, Shell scripting. Automatizare 17/44
Shell programming
I variabileI a=5
I fara spat, ii
I echo $a
I Ce este o variabila de mediu?I o variabila mos, tenita de procesele copilI demo
I construct, ii de ciclare s, i decizieI for, while, if, case, select
GSR Curs 3, Shell scripting. Automatizare 18/44
Shell programming (2)
for i in list; do command_list; done
for i in 1 2 3 4 5 6 7 8 9 10; do echo $i; done
for i in $(seq 1 10); do echo $i; done
for ((i = 1; i <=10; i++); do echo $i; done
for i in {1..10}; do echo $i; done (bash-only)
command | while read a b c; do command_list; done
GSR Curs 3, Shell scripting. Automatizare 19/44
Shell programming (3)
if command; then command_list; fi
if test $# -eq 0; then command_list; fi
test -f file
test -d directory
test string1 == string2
test num1 -lt num2
GSR Curs 3, Shell scripting. Automatizare 20/44
Caractere speciale shell
I $ – shell expansion
I ;, |, & – ınlant,uirea comenzilor
I ”, ’, \ – escaping
I *, [, {, ? – globbing
GSR Curs 3, Shell scripting. Automatizare 21/44
Argumente script shell
I $#I numarul total de argumentI argc
I $0I numele scriptului/executabilului
I $1, $2, $3, $4 ...I argumentele scriptului
I $*, $@I lista de argumente a scriptului
I shiftI shiftarea argumentelor unui scriptI poate primi ca argument valorea shift-ului
GSR Curs 3, Shell scripting. Automatizare 22/44
Outline
Interact, iunea cu shell-ul
Shell scripting
Shell programming
Filtre de text
find
Globbing s, i expresii regulate
cron
Keywords
Intrebari
GSR Curs 3, Shell scripting. Automatizare 23/44
Filtre de text
I ın Unix fis, ierele sunt textI de configurareI jurnaleI ies, irile comenzilor
I Do one thing do one thing well!I fiecare comanda prelucreaza textul s, i este transmis altei
comenziI operatorul |
I I’m lovin’ this!
last -30 | head -n -2 | cut -d ’ ’ -f 1 | sort | uniq | wc -l
2
GSR Curs 3, Shell scripting. Automatizare 24/44
head, tail, cat, tac
I catI atent, ie la crearea unui nou procesI cat file.txt | grep pattern → grep pattern <
file.txt
I head -n -10I toate liniile mai put, in ultimele 10
I tail -n +11I toate liniile ıncepand de la linia 11
I tail este util pentru log-uri (tail -f)
GSR Curs 3, Shell scripting. Automatizare 25/44
grep
I nu as,aI cat file.txt | grep patternI grep razvan /etc/passwdI grep /home/razvan /etc/passwd – cand e folosit?I grep -r function /path/to/source/folderI dpkg -l | grep libapache2-mod-php5
I atent, ie la escaping (pipe, star, plus)
GSR Curs 3, Shell scripting. Automatizare 26/44
cut, tr, wc
I cutI doar cand e simpluI cut -d ’:’ -f 1 /etc/passwdI pentru “extra features” – awk
I trI what does this button do?
I tr -d -c ’A-Za-z’ < /dev/urandom | head -c 8
I pentru “extra features” – sed
I wcI word countI -c, -l, -w
GSR Curs 3, Shell scripting. Automatizare 27/44
diff
I diferent,ele ıntre doua fis, iere
I folosit pentru crearea de patch-uri
razvan@valhalla:~$ diff test.sh test2.sh
6c6
< bibi
---
> more is less
I ce trebuie scos (<), ce trebuie introdus (>)
I comanda patch aplica un patch obt, inut cu diff (sau git diff
sau svn diff)
GSR Curs 3, Shell scripting. Automatizare 28/44
Outline
Interact, iunea cu shell-ul
Shell scripting
Shell programming
Filtre de text
find
Globbing s, i expresii regulate
cron
Keywords
Intrebari
GSR Curs 3, Shell scripting. Automatizare 29/44
find
I cautare ın sistemul de fis, iere
I find . -name ’*.c’
I find -type f -name ’*.c’
I find -type f -name ’*.c’ -perm /111
I find -type f -name ’*.c’ -perm /111 -exec chmod
a-x {}I find -mindepth 1 -maxdepth 1
I for file in $(find ...); do ... done
I a=(*); for file in ${a[@]}; do ... doneI NU for i in *I NU for i in $(ls ...)
GSR Curs 3, Shell scripting. Automatizare 30/44
xargs
I trimite liniile de la intrarea standard ca argumente comenziiprimite ca argument
I find . -type f | xargs rm -f
I find . -type f | xargs -I {} cp {} /tmp
GSR Curs 3, Shell scripting. Automatizare 31/44
Outline
Interact, iunea cu shell-ul
Shell scripting
Shell programming
Filtre de text
find
Globbing s, i expresii regulate
cron
Keywords
Intrebari
GSR Curs 3, Shell scripting. Automatizare 32/44
Globbing
I filename expansion
I diferit de setul standard de expresii regulateI *
I orice caracter de oricate ori
I ?I orice caracter
I [abc]I set de caracter
I [ˆabc]I diferit de setul de caractere
I {jpg,png,bmp}I select, ie
GSR Curs 3, Shell scripting. Automatizare 33/44
Expresii regulate
I x*I caracterul x de oricate ori (posibil niciodata)
I x+I caracterul x de oricate ori (cel put, in o data)
I x?I caracterul x cel mult o data
I . (punct)I orice caracter
I ˆI ınceput de rand
I $I sfars, it de rand
I [a-z]I set de caractere
I [ˆa-z]I set complementar de caractere
GSR Curs 3, Shell scripting. Automatizare 34/44
sed
I opt, iuni multipleI de obicei folosit la substitut, ia s, irurilor
/pattern/s/to_be_replaced/to_replace/options
sed ’/\/home\//s/^\([^:]\+\):/\1_rules:/’ < /etc/passwd
GSR Curs 3, Shell scripting. Automatizare 35/44
awk
I Advanced cut
I Permite formarea unui separator pe baza unei expresii regulate
I Limbaj de programare
cut -d ’:’ -f 1
awk -F ’:’ ’{print $1;}’
awk -F ’[ \t]+’ ’/ana/ {print $3;}’
GSR Curs 3, Shell scripting. Automatizare 36/44
Exemple
1 /^$/d
2 s/a/b/g
3 s/^a/b/g
4 s/\<ana\>/bogdan/g
5 s/[ \t]\+\<ana\>$/b/g
6 s/^|type.*//g7 s/\([^ \t,]\+\),\([^ \t,]\+\)/\2 | \1/8 s/[ \t]*<li>\(.*\)<\/li>/* \1/g9 s/<\/pre>/<\/code>/g
10 s/\[\(http[^]]\+\)]/[[\1]]/g11 s/\xc5\x9f/\xc8\x99/g
GSR Curs 3, Shell scripting. Automatizare 37/44
Outline
Interact, iunea cu shell-ul
Shell scripting
Shell programming
Filtre de text
find
Globbing s, i expresii regulate
cron
Keywords
Intrebari
GSR Curs 3, Shell scripting. Automatizare 38/44
cron
I serviciu de planificare a unei sarcini
I planificare la nivel de secunda
I planificare statica sau periodica
I /etc/init.d/cron
GSR Curs 3, Shell scripting. Automatizare 39/44
Configurare cron
I la nivel de sistem
I /etc/crontab
I /etc/cron.allow
I /etc/cron.deny
I la nivelul utilizatorului
I crontab -l (listare)
I crontab -e (editare)
I /var/spool/cron/crontabsI format
I minute, hour, day of month, month, day of week, command0 */8 * * * /home/razvan/cron/rsync_backup_mamba
GSR Curs 3, Shell scripting. Automatizare 40/44
Outline
Interact, iunea cu shell-ul
Shell scripting
Shell programming
Filtre de text
find
Globbing s, i expresii regulate
cron
Keywords
Intrebari
GSR Curs 3, Shell scripting. Automatizare 41/44
Cuvinte cheie
I shell
I comenzi interne, externe
I operatori shell
I shell script
I redirectare
I secvent, iere
I variabile
I if, for, case, while
I shell expansion
I caractere speciale
I escaping
I globbing
I expresii regulate
I filtre de text
I head, tail, cat, tac
I grep, awk, sed
I cut, tr, wc
I cron
I crontab
GSR Curs 3, Shell scripting. Automatizare 42/44
Resurse utile
I http://www.gnu.org/software/bash/manual/bashref.html
I http://www.tldp.org/LDP/abs/
I http://www.shelldorado.com/
I http://mywiki.wooledge.org/BashFAQ
I #bash pe Freenode (IRC)
GSR Curs 3, Shell scripting. Automatizare 43/44