al comando con la command line
DESCRIPTION
LinuxTRANSCRIPT
![Page 1: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/1.jpg)
State al comando con la command lineGiuseppe Maxia
The Data Charmer
QA Director, Continuent, Inc
1
$Friday, October 26, 12
![Page 2: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/2.jpg)
Un po' di informazioni personaliGiuseppe Maxia‣ Noto anche come "The Data Charmer"‣ Direttore del controllo di qualità a Continuent, Inc‣ Sardo cosmopolita‣ Database hacker di lunga data‣ Blogger (http://datacharmer.blogspot.com)‣ Curatore di MySQL Sandbox‣ Utente Linux dal 1993
2Friday, October 26, 12
![Page 3: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/3.jpg)
Questa sessione parla della linea di comandoDi cosa parliamo
‣ Strumenti e suggerimenti per gli utenti della linea di comando
‣ Imparare qualche trucco per scrivere meno e produrre di più
‣ Far diventare gli utenti di interfacce grafiche verdi di invidia ...
3
bar
foo
baz
Friday, October 26, 12
![Page 4: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/4.jpg)
Quando serve usare la linea di comando ...Definiamo il problema
‣ Supponiamo che abbiate un documento • con una data nel formato "mm-gg-aaaa"
• volete trasformarla in "aaaa.mm.gg" (o "gg.mm.aaaa")
‣ Utente GUI• Aprire il documento con Word (Open/LibreOffice)
• Modificare la data
‣ Utente Linea di comando• Probabilmente fa lo stesso
4Friday, October 26, 12
![Page 5: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/5.jpg)
Quando serve usare la linea di comando ...Aumentiamo il problema
‣ Supponiamo che abbiate un documento • con molte date nel formato "mm-gg-aaaa"
• volete trasformarle in "aaaa.mm.gg" (o "gg.mm.aaaa")
‣ Utente GUI• Aprire il documento con ... uh, qualcosa
• Sperare che supporti espressioni regolari
‣ Utente Linea di comando• Usa vi o emacs e risolve con un comando
5Friday, October 26, 12
![Page 6: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/6.jpg)
Questo e' uno dei modi ...Modificare una data con vi
Il vostro testo contiene 12-25-2012 e anche 08-31-1978, e perfino 01-04-2004.
Alcune righe dopo, troviamo anche 05-12-1995.
:%s/\(\d\d\)-\(\d\d\)-\(\d\d\d\d\)/\3.\2.\1/g6
Friday, October 26, 12
![Page 7: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/7.jpg)
Questo e' uno dei modi ...Modificare una data con vi
Il vostro testo contiene 2012.25.12 e anche 1978.31.08, e perfino 2004.04.01.
Alcune righe dopo, troviamo anche 1995.12.05.
7Friday, October 26, 12
![Page 8: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/8.jpg)
In effetti, potete anche usare OpenOffice
‣ Se sapete usare espressioni regolari, anche con OpenOffice o LibreOffice potete arrivare alla stessa soluzione.
‣ Siamo ancora (quasi) in parità ‣ ma ...
8Friday, October 26, 12
![Page 9: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/9.jpg)
Quando veramente serve usare la linea di comando ...Ma il problema può farsi più interessante
‣ Supponiamo che abbiate molti documenti • con molte date nel formato "mm-gg-aaaa"
• volete trasformarle in "aaaa.mm.gg" (o "gg.mm.aaaa")
‣ Utente GUI• ???
‣ Utente Linea di comando• Usa sed o perl dalla linea di comando
9Friday, October 26, 12
![Page 10: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/10.jpg)
Qui nessuno batte la command lineMolte date da cambiare in + documentiperl -i -pe 's/(\d\d)-(\d\d)-(\d{4})/$3.$2.$1/g' *.txt
Oppure, se volete la soluzione più difficile:
sed -i -E 's/\([0-9][0-9]\)-\([0-9][0-9]\)-\([0-9][0-9][0-9][0-9]\)/\3.\2.\1/g' *.txt
10Friday, October 26, 12
![Page 11: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/11.jpg)
Che diavolo sono quei segni orribili sullo schermo?Intermezzo
‣ Espressioni regolari (regular expressions)‣ Un linguaggio di ricerca testuale‣ Usato in diversi ambienti (inclusi molti editor grafici!)‣ Diversi dialetti (come in Sardegna)‣ Utile come l'inglese‣ Difficile come il sardo (per gli italiani)‣ Facile come il sardo (per i sardi: basta capirli!)
11Friday, October 26, 12
![Page 12: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/12.jpg)
Cercare un carattere in un testo e' molto facileEspressioni regolari in due schermate$ (echo "Ciao"; echo "mondo"; echo "2012")
Ciao
mondo
2012
$ (echo "Ciao"; echo "mondo"; echo "2012") | grep "a"Ciao
$ (echo "Ciao"; echo "mondo"; echo "2012") | grep "o"Ciao
mondo
12Friday, October 26, 12
![Page 13: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/13.jpg)
Certi caratteri hanno un senso particolareespressioni regolari in due schermate$ (echo "Ciao"; echo "mondo"; echo "2012") | grep "\w"Ciao
mondo
2012
$ (echo "Ciao";echo "mondo"; echo "2012") | grep "[0-9]"2012
$ (echo "Ciao";echo "mondo";echo "2012")|grep "[0-9a-z]"Ciao
mondo
2012
13Friday, October 26, 12
![Page 14: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/14.jpg)
Certi caratteri hanno un senso ancora piu' particolareHo barato. C'e' una terza schermata$ (echo "Ciao"; echo 'sei nel';echo "2012") | grep "\s"
sei nel
$ (echo '!Ciao'; echo '2012!') | grep "\W"
!Ciao
2012!
$ (echo '!Ciao'; echo '2012!') | grep "^\W"!Ciao
$ (echo '!Ciao'; echo '2012!') | grep "\W$"2012!
14Friday, October 26, 12
![Page 15: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/15.jpg)
Ci sono tante cose da imparare!In realtà ci sarebbero altre schermate ...
‣ Ma la lasciamo per un'altra sessione
‣ nel frattempo, se avete perso il film, vi consiglio il libro!
15
http://regex.info/
Friday, October 26, 12
![Page 16: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/16.jpg)
‣Perché usare la linea di comando?
16
?
Friday, October 26, 12
![Page 17: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/17.jpg)
Quello che ho imparato con Microsoft diventa inutile dopo pochi anni. Quello che ho imparato con Unix e' ancora valido
Una storia di apprendimento
17
1985-901991199319941995-971998-992000-20012001-20062007-20092010-20112012-
• MS-DOS• Windows 3.0• Windows 3.1• Windows NT• Windows 95• Windows 98• Windows 2000• Windows XP• Windows Vista• Windows 7• Windows 8
• Unix shell
• Linux
• Mac OSX
Friday, October 26, 12
![Page 18: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/18.jpg)
Per principiantiIntroduciamo la linea di comando
‣ Prima di tutto: niente panico!
18
||||
Friday, October 26, 12
![Page 19: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/19.jpg)
Quando un principiante incontra la linea di comandoChe cosa e' il panico da command line?
‣ E ora che faccio?
19
$_Friday, October 26, 12
![Page 20: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/20.jpg)
Un po' di conoscenza e passa la pauraLa linea di comando
‣ Principi
20Friday, October 26, 12
![Page 21: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/21.jpg)
Il personaggio principaleLa shell
‣ E' un interprete di comandi‣ Ce sono vari dialetti
• bash
• csh
• ksh
• zsh
‣ Noi ci occupiamo solo di "bash"21
Friday, October 26, 12
![Page 22: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/22.jpg)
la shell può fare molte cose senza aiuto di altrii comandi interni della shell
‣ echo‣ export‣ alias‣ for‣ while‣ test‣ pwd‣ ...
22Friday, October 26, 12
![Page 23: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/23.jpg)
E poi ci sono programmi che si trovano in tutte le macchine Unix
i comandi esterni
‣ man, ls, cd, find, grep‣ date, mv, cp‣ rm, mkdir, rmdir‣ cat, sed‣ strings, wc, head, tail‣ more/less‣ sort
23Friday, October 26, 12
![Page 24: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/24.jpg)
Per completare il quadroE altri strumenti
‣ Shell scripts‣ Aliases‣ Perl‣ awk‣ sort‣ …. E un sacco di altri piccoli aiuti da Unix
24
Che può sostituire tutto il resto, se volete
Friday, October 26, 12
![Page 25: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/25.jpg)
E' il gestore del database MySQLUno strumento che useremo
‣ Si chiama "mysql"‣ Riceve comandi SQL‣ Li invia al server di database‣ Riceve risultati
‣ Ma può fare molte altre cose ...
25Friday, October 26, 12
![Page 26: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/26.jpg)
Meglio specificare ...Se non fosse chiaro finora ..
‣ Le azioni alla linea di comando sono:• comandi (il nome dell'applicazione da lanciare)
• parametri (argomenti opzionali per il comando)
• E il tasto ENTER (detto anche INVIO o RETURN)
26
Enter
Friday, October 26, 12
![Page 27: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/27.jpg)
Che non e' uno stile di vita, ma un modo efficiente di lavorareLa filosofia di Unix (I)
‣ Uno strumento per ogni funzionalità • Crea uno strumento che fa bene una cosa
• Per fare molte cose, crea molti strumenti
• Falli lavorare insieme (vedi la prossima slide)
27Friday, October 26, 12
![Page 28: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/28.jpg)
E' un po' come "Usa la forza!" ma funziona!La filosofia di Unix (II)
‣ Per far cooperare diversi strumenti:• Attacca l'uscita di uno strumento all'ingresso di un altro.
• Per avere più azioni, attacca più strumenti.
$ sort < filename | uniq | nl
28Friday, October 26, 12
![Page 29: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/29.jpg)
L'ingresso e l'uscita di un'applicazione possono essere diretti a un'altra
Unix pipes (flussi applicativi)
29
outputapplication >
inputapplication <
outputapplication >input<
application | application
input output
Friday, October 26, 12
![Page 30: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/30.jpg)
Non si deve aspettare che la prima applicazione abbia finito per cominciare a elaborare la seconda
Unix pipes = elaborazione parallela
30
application | application
input output output input
Friday, October 26, 12
![Page 31: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/31.jpg)
I comandi inseriti alla tastiera possono andare a finire in diverse applicazioni
Dove vanno i vostri comandi
31
23
shell prompt $ mysql
mysql client
mysql server
Friday, October 26, 12
![Page 32: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/32.jpg)
Il comando inserito al terminale talvolta viaggia lontanoDove sono i destinatari
32
any host
shellmysql client
mysql server
Friday, October 26, 12
![Page 33: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/33.jpg)
Vediamo alcuni casiChi riceve che cosa
33
$ mysql -u username -ppassword
Prima l'applicazione, poi il server
Friday, October 26, 12
![Page 34: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/34.jpg)
Vediamo alcuni casiChi riceve che cosa
34
$ mysql -u username -ppassword \ -e "select VERSION()"
Va al server
Va prima al client mysql, poi al server
Friday, October 26, 12
![Page 35: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/35.jpg)
Vediamo alcuni casiChi riceve che cosa
35
mysql> helpmysql> pager lessmysql> select 1;
va al server
goes to the client onlyVa solo al client (comandi interni)
Friday, October 26, 12
![Page 36: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/36.jpg)
Un componente importante ma poco notoLa linea di comando
‣ READLINE• Componente comune
• nella shell
• nel client mysql
• in molti programmi Unix
36Friday, October 26, 12
![Page 37: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/37.jpg)
Ci sono combinazioni di tasti che salvano la vitareadline (1)
37
Ctrl A Ctrl E
Inizio, fine della riga
Meta B Meta F
Indietro/Avanti di una parola
Comando precedente/seguente
Ctrl L Cancella lo schermo e porta il cursore in alto
Ctrl R Cerca comandi precedenti
Friday, October 26, 12
![Page 38: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/38.jpg)
Per cambiare il testoreadline (2)
38
Meta U Trasforma parola in MAIUSCOLO
Meta L Trasforma parola in minuscolo
Ctrl D Cancella parola
Ctrl K Cancella fino alla fine della riga
Ctrl - Elimina ultima azione
Altri consigli:$ man readline
Friday, October 26, 12
![Page 39: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/39.jpg)
Il primo attorebash
‣ Componenti:• comandi
• alias
• variables
• functions
39
#!
Friday, October 26, 12
![Page 40: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/40.jpg)
Sono i comandi interni ed esterniComandi$ echo "something"
something
40Friday, October 26, 12
![Page 41: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/41.jpg)
Creazione di nuovi comandi al voloAlias$ alias mydate='mysql -e "select NOW()"'
$ mydate
+---------------------+
| now() |
+---------------------+
| 2010-04-12 07:14:19 |
+---------------------+
41Friday, October 26, 12
![Page 42: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/42.jpg)
Un alias si può usare dentro un altro aliasAlias$ alias myself='mysql'
$ myself -e "select NOW()"'
+---------------------+
| now() |
+---------------------+
| 2010-04-12 07:14:19 |
+---------------------+
42Friday, October 26, 12
![Page 43: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/43.jpg)
Valori che sostituiscono testoVariables$ MYBATCH='mysql -N -B'
$ MYQUERY='select curdate()'
$ $MYBATCH -e "$MYQUERY"
2010-04-12
43Friday, October 26, 12
![Page 44: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/44.jpg)
Una dentro l'altra!Variabili annidate# t.sh
CMD="SELECT TABLE_NAME"
TBL='information_schema.tables'
WHC1="TABLE_SCHEMA='mysql'"
WHC2="TABLE_NAME like 'u%' "
Q1="$CMD FROM $TBL"
Q2="WHERE $WHC1 AND $WHC2"
Q="$Q1 $Q2"
echo $Q
echo $Q | mysql
44Friday, October 26, 12
![Page 45: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/45.jpg)
Le variabili vengono espanse durante l'esecuzioneUso delle variabili annidate$ sh t.sh
SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_SCHEMA='mysql' AND TABLE_NAME like 'u%'
TABLE_NAME
user
45Friday, October 26, 12
![Page 46: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/46.jpg)
$ mysql < filename.sqlmysql> source filename.sql
Il client mysql
‣ Esegue i comandi contenuti in filename.sql
‣ I comandi possono essere query che vengono mandate al server
‣ e comandi che solo il client capisce
46
mysql>
Friday, October 26, 12
![Page 47: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/47.jpg)
$ application | mysql$ echo "SELECT 1" | mysql$ mysql -e "SELECT 1"
Il client mysql
‣ Esegue i comandi da "standard input"
‣ I comandi possono essere query che vengono mandate al server
‣ e comandi che solo il client capisce
47Friday, October 26, 12
![Page 48: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/48.jpg)
$ echo "select curdate()" | mysqlcurdate()2010-04-12
Il client mysql - batch mode
‣ In batch mode, l'output non viene abbellito
48Friday, October 26, 12
![Page 49: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/49.jpg)
$ echo "select curdate()" | mysql -t+------------+| curdate() |+------------+| 2010-04-12 |+------------+
Il client mysql - table mode
‣ In table mode, l'output viene abbellito
49Friday, October 26, 12
![Page 50: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/50.jpg)
$ mysql -e "select curdate()"+------------+| curdate() |+------------+| 2010-04-12 |+------------+
Il client mysql - table mode
‣ Table mode viene inteso automaticamente quando il comando e' passato con il parametro "-e"
50Friday, October 26, 12
![Page 51: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/51.jpg)
$ mysql -N -B -e 'select curdate()'2010-04-12
Il client mysql - senza testata
‣ IMPORTANTE! possiamo forzare il batch mode (-B) e allo stesso tempo eliminare i nomi di colonne (-N)
‣ Questo ci serve per costruire comandi complessi
51Friday, October 26, 12
![Page 52: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/52.jpg)
Un comando viene eseguito se inserito in virgolette inverseComandi dentro testoComandi fra virgolette inverse (inverse quotes) ( `` )Oppure usando $()
$ echo "The current server's date is `mysql -N -B -e 'select curdate()'`."
$ echo "The current server's date is $(mysql -N -B -e 'select curdate()')."
52Friday, October 26, 12
![Page 53: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/53.jpg)
Se potete eseguire cicli dalla linea di comando, nulla può resistervi
Cicli (loops)
‣ Non sono cicli da pedalare, ma da eseguire
53Friday, October 26, 12
![Page 54: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/54.jpg)
Un ciclo sempliceCicli - le basi$ for N in 1 2 3> do
> echo $N> done
1
2
3
54Friday, October 26, 12
![Page 55: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/55.jpg)
Lo stesso ciclo, ma su una sola rigaCicli - le basi$ for N in 1 2 3 ; do echo $N ; done1
2
3
55Friday, October 26, 12
![Page 56: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/56.jpg)
Un ciclo può usare una lista create da un programmaCicli - le basi$ seq 1 3
1
2
3
$ for N in $(seq 1 3) ;\ do echo "this is number $N" ;\ done
this is number 1
this is number 2
this is number 3
56Friday, October 26, 12
![Page 57: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/57.jpg)
Ecco una decina di righe da un !leCicli da un file$ head /usr/share/dict/words
A
a
aa
aal
aalii
aam
Aani
aardvark
aardwolf
57Friday, October 26, 12
![Page 58: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/58.jpg)
Usando un !le come input, il ciclo esegue un'operazione su ogni elemento
Cicli da un file
$ for U in \$(head /usr/share/dict/words ) ; \do perl -le 'print uc shift' $U ; doneA
A
AA
AAL
AALII
AAM
AANI
58Friday, October 26, 12
![Page 59: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/59.jpg)
Per esempio, l'elenco di tabelle da un databaseAllo stesso modo si usa output da mysql$ mysql -B -N -e 'show tables from world'
City
Country
CountryLanguage
59Friday, October 26, 12
![Page 60: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/60.jpg)
Creazione di comandi usando l'elenco di tabelleOutput da mysql in un ciclo$ for T in $(mysql -B -N -e 'show tables from world') ;\> do echo "CREATE TABLE world2.$T LIKE world.$T;" ; done
CREATE TABLE world2.City LIKE world.City;
CREATE TABLE world2.Country LIKE world.Country;
CREATE TABLE world2.CountryLanguage LIKE world.CountryLanguage;
60Friday, October 26, 12
![Page 61: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/61.jpg)
Questa e' tutta la magia del ciclo:Il risultato di mysql viene usato come input per il ciclo;Il ciclo produce comandi SQL;Il risultato del ciclo (comandi SQL) viene reindirizzato a mysql stesso, che cosi' facendo esegue i comandi
usare il risultato del ciclo con MySQL
$ for T in $(mysql -B -N -e 'show tables from world') ;\
> do echo "CREATE TABLE world2.$T LIKE world.$T;" ; done \
> | mysql -vv
61Friday, October 26, 12
![Page 62: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/62.jpg)
Un po' piu difficile. Ma questo metodo e' la chiave dell'onnipotenza da sysadmin
Cicli annidati
‣ Un ciclo dentro un ciclo
62Friday, October 26, 12
![Page 63: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/63.jpg)
Vediamo la lista dei databasePer esempio usando mysql$ mysql -e "show databases"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| sakila |
| world |
+--------------------+
63Friday, October 26, 12
![Page 64: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/64.jpg)
Possiamo fare qualcosa con le tabelle di un databaseUsando mysql$ mysql -e "show tables from world"
+-----------------+
| Tables_in_world |
+-----------------+
| City |
| Country |
| CountryLanguage |
+-----------------+
64Friday, October 26, 12
![Page 65: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/65.jpg)
Up to a one line subtitleIn teoriaper ogni database
esegui
per ogni tabella
esegui
Fai qualcosa con la tabella corrente del
database corrente
65Friday, October 26, 12
![Page 66: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/66.jpg)
Usando una combinazione di comandi nel testo e cicli, abbiamoIn praticaalias mybatch='mysql -B -N'
for D in $(mybatch -e "SHOW SCHEMAS")do
for T in \
$(mybatch -e "SHOW TABLES FROM $D") do
echo "SHOW CREATE TABLE $D.$T;" done
done
66Friday, October 26, 12
![Page 67: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/67.jpg)
L'argomento e' lungoConcludendo
‣ Potrei parlare di questo argomento per tre ore‣ (In effetti l'ho fatto alcune volte)‣ Ma oggi, con soli 40 minuti, non possiamo‣ Quindi vi lascio un elenco di letture
67Friday, October 26, 12
![Page 68: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/68.jpg)
Ci sono altri talk, più lunghi, che spiegano molti altri trucchi, e il mio blog ha riferimenti ad argomenti simili
Altri talk come questo e buone letture
‣ http://slideshare.net/datacharmer‣ http://datacharmer.blogspot.com
68Friday, October 26, 12
![Page 69: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/69.jpg)
Se volete considerare la linea di comando seriamente, ecco alcune risorse
ALcuni libri da considerare
69Friday, October 26, 12
![Page 70: Al comando con la command line](https://reader034.vdocuments.us/reader034/viewer/2022051513/545cb04eb1af9f4b0a8b4832/html5/thumbnails/70.jpg)
Troverete le slides nel sito http://slideshare.net/datacharmer qualche ora dopo la presentazione
Questo conclude la mia presentazione
Grazie per l'attenzione
70Friday, October 26, 12