PREV Capitolo 7 - Sequenza di Boot |
NEXT Capitolo 9 - Struttura del File System |
In un ambiente grafico, l'interfaccia e' fornita da un programma che crea finestre, barre di scorrimento, menu, ecc. In un ambiente a linea di comando, l'interfaccia utente e' fornita da una "shell", che interpreta comandi e piu' generalmente rende il tutto adoperabile. Immediatamente dopo la procedura di accesso (il login , trattato in questo capitolo), gli utenti vengono interfacciati ad una shell, la quale permette loro di svolgere le piu' disparate attivita'. Questo capitolo ha come scopo l'introduzione alla shell, e alla piu' comune shell fra gli utenti linux -- la Bourne Again Shell (bash). Per maggiori dettagli su qualsiasi cosa segnalata in questo capitolo, si controlli la man page della bash(1).
Il computer e' stato avviato e si presenta qualcosa simile a questo:
Welcome to Linux 2.4.22 |
Purtroppo non si e' stati avvertiti da nessuno riguardo ad un login. E cos'e' darkstar? Non c'e' da preoccuparsi; e' improbabile che si sia accidentalmente creato un collegamento iperspaziale con la luna artificiale dell'Impero (purtroppo il protocollo di collegamento iperspaziale non e' ancora supportato dal kernel di Linux... ma ci stiamo lavoorando :). Darkstar e' solo il nome di un computer, il quale viene mostrato di default. Se si e' specificato un nome per la propria macchina in fase di installazione, si dovrebbe vederlo al posto di darkstar.
Ed ora passiamo al login... Se e' la prima volta che si accede a Slackware Linux, si dovra' accedere come root. Verra' richiesta una password; se e' stata impostata durante l'installazione, la si inserisca, altrimenti premere semplicemente invio. Ecco fatto il login!
Okay, chi o cosa e' "root"? E cosa sta facendo con un account nel sistema? Bene, nel mondo di Unix e sistemi operativi simili (come Linux), ci sono utenti e... utenti. La cosa verra' vista piu' in dettaglio dopo, ora la cosa importante da sapere e' che quel "root" e' l'utente al di sopra di tutti gli altri utenti; root e' onnipotente e conosce tutto. Nessuno disobbedisce a root: non è consentito. Root e' anche chiamato "superutente". Ma il fatto piu'; interessante e' che si puo' essere root in qualsiasi momento. La cosa importante da tenere a mente e' che a root e' consentito rompere qualsiasi cosa egli desideri. Si potrebbe voler andare al Capitolo 12 per imparare come si aggiunge un utente; poi entrare come quell'utente e lavorare cosi'. Il buonsenso tradizionale consiglia di diventare root solo quanto e' assolutamente necessario, in modo da minimizzare la possibilita' di fare danni. Comunque, se si desidera essere root mentre si e' loggati come qualcun'altro, non c'e' problema. Utilizzando il comando su(1), verra' chiesta la password di root e si rimarra' tale finche' non verra' digitato exit o logout (oppure la sequenza control-d).Si puo' diventare un altro utente usando su (sempre che si conosca la password di quell'utente): su logan, ad esempio, farebbe assumere l'identita' dell'utente "logan".
E' difficile realizzare qualcosa senza avviare nemmeno un programma; il lettore potrebbe essere in grado di puntellare qualcosa con il computer oppure tenere aperta una porta; ma questo e' tutto. E penso che possiamo essere tutti d'accordo che usare un computer come ferma porta non e' quello che ha portato il personal computer alla popolarita' di cui oggi gode.
Bene, si ricorda come in Linux qualsiasi cosa e' un file? Questo vale anche per i programmi. Ogni comando che viene eseguito (a menoche' non sia interno alla shell) risiede in un file da qualche parte. Si esegue un programma semplicemente specificando il percorso completo dello stesso. Ad esempio, il comando su di cui abbiamo parlato si trova nella directory /bin: /bin/su fara' girare il programma. Ma allora perche' scrivendo solo su funziona? Nessuno ha specificato che si trova in /bin, potrebbe anche essere stato in /usr/local/share. La risposta a tutto questo risiede nella variabile d'ambiente PATH molte shell hanno un PATH o qualcosa di molto simile. Fondamentalmente essa contiene una lista di directory dove cercare il programma che si vuole avviare. Nel momento in cui si avvia su, la shell controlla la sua lista di directory, cercando un file eseguibile chiamato su. La prima occorrenza trovata viene eseguita. Questo succede ogni volta che un programma viene avviato senza specificarne l'intero percorso; se viene restituito l'errore Command not found, vuol dire che il programma che si e' tentato di eseguire non e' nel PATH (Naturalmente, questo varrebbe anche se il programma non esistesse...). Le variabili d'ambiente verranno discusse meglio nella sezione chiamata Bourne Again Shell (bash). Si ricordi inoltre che "." e' una scorciatoia per la directory corrente, percio' se si e' in /bin, "./su" funziona come se si avesse specificato il percorso completo.
Quasi ogni shell riconosce alcuni caratteri come sostituti o abbreviazioni che significano "qui va qualsiasi cosa". Questi caratteri vengono chiamati "wildcards" (jolly); i piu' comuni sono * e ?. Per convenzione, ? significa un qualsiasi carattere singolo. Ad esempio, si supponga di essere in una directory contenente tre file: ex1.txt, ex2.txt, e ex3.txt. Si vuole copiaretutti questi file (usando il comando cp, spiegato nella sezione chiamata cp nel capitolo 10) in un altra directory chiamata /tmp. Scrivere
$ cp ex1.txt ex2.txt ex3.txt /tmp |
per intero richiede troppo lavoro. E' molto piu' facile scrivere
$ cp ex?.txt /tmp |
il ? combacia con i caratteri "1", "2", e "3", i quali a turno verranno sostituiti. C'e' ancora troppo lavoro da fare? E' vero. E' spaventoso; abbiamo leggi sul lavoro per da questo tipo di cosa. Fortunatamente esiste "*". Come gia' menzionato, "*" eguaglia "un qualsiasi numero di caratteri", incluso lo 0. Percio' se i tre file sono gli unici presenti nella directory, si puo' copiare il tutto semplicemente con
$ cp * /tmp |
Un altro esempio: si supponga che ci siano un file chiamato example.txt e uno chiamato hejaz.txt. Si desidera copiare example.txt ma non hejaz.txt. Il comando da eseguire e'
$ cp example* /tmp |
cp example?.txt /tmp copiera', naturalmente, i nostri tre file originali; non ci sono caratteri in example.txt che possano combaciare con ?, percio' sara' lasciato in pace.
(Ecco qualcosa di forte.)
$ ps > blargh |
Che cosa e'? Sono io che eseguo ps per vedere quali processi sono in esecuzione; ps e' trattato nel Capitolo 11. Quella comunque non e' la parte interessante. La parte interessante e' > blargh, che significa, in parole povere, "prendi l'output di ps e scrivilo in un file chiamato blargh". Ma ora la cosa si fa ancora piu' intrigante.
$ ps | less |
Questo prende l'output di ps e lo "incanala" (pipe vuol dire tubo) attraverso less, in modo da poterlo visualizzare a mio piacimento.
$ ps >> blargh |
Questo e' il terzo redirector piu' usato; esso fa le stesse cose di ">", eccetto che ">>" aggiunge l'output di ps al file blargh, se il file esiste. Se non esiste, come con ">", esso viene creato. (">" sovrascrive il contenuto corrente di blargh.) C'e' anche un operatore "<", che significa "prendi il tuo input dal seguente", ma non e' usato spesso.
$ fromdos < dosfile.txt unixfile.txt |
La redirezione diventa veramente divertente quando si comincia a combinare gli operatori:
$ ps | tac >> blargh |
Questo eseguira' ps, rovesciando le linee del suo output, e le aggiungera' al file blargh. Si puo' aggiungere quante redirezioni si desidera; basta fare attenzione ad interpretarle da sinistra a destra. Si veda la man page di bash(1) per informazioni piu' dettagliate sulla redirezione.
Un sistema Linux e' una bestia complessa, e ci sono molte cose da seguire, una moltitudine di piccoli dettagli che entrano in gioco durante le normali interazioni con i vari programmi (di alcuni probabilmente non si dovra' mai preoccuparsi). Nessuno vuole passare un mucchio di opzioni ad ogni programma da avviare, dicendo il tipo di terminale da usare, l'hostname del computer, come dovrebbe apparire il prompt... Cosi' come un meccanismo di gestione, gli utenti hanno quello che che viene comunemente chiamato ambiente (enviroment). L'ambiente definisce le condizioni nelle quali viene eseguito il programma, ed alcune di queste definizioni sono variabili; l'utente puo' alterarle e giocare con esse, come e' di diritto in un sistema Linux. Molte shell hanno le variabili d'ambiente (altrimenti non sarebbero molto utili). Diamo uno sguardo ai comandi che che bash fornisce per manipolare le sue variabili d'ambiente.
$ set |
set da solo mostrera' tutte le variabili d'ambiente attualmente definite, con i loro rispettivi valori. Come molti comandi bash integrati, esso puo' fare molte altre cose (con i parametri); lasceremo alla man page della bash(1) il compito di trattare questi altri argomenti. L'output tipico di set potrebbe essere il seguente:
PATH=/usr/local/lib/qt/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin: |
Si presti attenzione alla variabile PATH della quale abbiamo discusso prima; posso feseguire qualsiasi cosa di quelle directory richiamando semplicemente il nome del file.
$ unset VARIABILE |
unset rimuovera' ogni variabile che gli passate, annullando sia la variabile che il suo valore; bash si dimentichera' che la variabile sia mai esistita (Non ci si preoccupi. A meno che non sia una cosa definita per ogni sessione nei file di configurazione, essa riapparira' immediatamente avviando un altra sessione).
$ export VARIABLE=some_value |
Ora, export e' veramente utile. Nel caso sopra si assegna alla variabile d'ambiente VARIABLE il valore "some_value" se la variabile non esiste, verra' creata. Se la variabile ha gia' un valore verra' sostituito con il nuovo valore. Questo non va bene se si sta aggiungende una directory al PATH. In questo caso si dovra' usare una cosa simile a questa:
$ export PATH=$PATH:/some/new/directory |
Da notare l'utilizzo di "$PATH" qui: quando si vuole che bash interpreti una variabile (scambiarla con il suo valore), si deve mettere un "$" prima del nome della variabile. Ad esempio, echo $PATH mostrera' il contenuto di $PATH:
$ echo $PATH |
(Qui c'e' qualcosa di molto interessante.)
1) Una interfaccia a linea di comando significa un mucchio di cose da scrivere.
2) Scrivere vuol dire lavorare.
3) A nessuno piace lavorare.
Da 3 e 2, possiamo determinare
4) A nessuno piace scrivere.
Fortunatamente, bash ci salva da 5 (a nessuno piace un'interfaccia a linea di comando). Come fa bash a fornire questa splendida caratteristica? In aggiunta alle sostituzioni dovute alle wildcard discusse prima, bash e' caratterizzata dal "Tab completition", ovvero il completamento tramite il tasto Tab. La Tab completition funziona cosi': si sta scrivendo il nome di un file. Forse e' nel PATH, forse lo si sta scrivendo esplicitamente. Tutto quello che si deve fare e' scrivere una parte del nome del file per identificarlo univocamente. Premere poi il tasto Tab. bash si immaginera' cosa si ha intenzione di scrivere e la nostra amica bash completera' il nome! E' tempo per un esempio. /usr/src contiene 2 sottidirectory: usr/src/linux e /usr/src/sendmail. Siamo interessati a cosa c'e' in /usr/src/linux. Per fare cio' basta scrivere
$ ls /usr/src/l |
premere il tasto TAB, e bash restituisce
$ ls /usr/src/linux |
Ora, supponiamo che ci siano due directory /usr/src/linux e /usr/src/linux-old; scrivendo /usr/src/l e premendo TAB, bash riempira' finche puo' e ritornera'
$ ls /usr/src/linux |
Possiamo fermarci qui o premere ancora TAB: bash mostrera' una lista di directory che assomigliano a quelle appena scritte. D'ora in poi, meno battitura (e da ora, le persone possono apprezzare la linea di comando). L'avevo detto che era interessante. :)
Nel mezzo di un lavoro si ha improvvisamente bisogno di fare qualcos'altro. Si puo' tranquillamente smettere di fare quello che si stava facendo e scambiare i task, ma questo e' un sistema multiutente, giusto? E si puo' entrare simultaneamente tante volte quante si vuole , giusto? Allora perche' dovremmo fare le cose una alla volta? Non ce n'e' bisogno. Non possiamo avere tastiere, mouse, e monitor multipli per una sola macchina. Non vogliamo andare per tentativi e chiaramente l'hardware non e' la soluzione. Lasciamo che software e Linux ci forniscano "terminali virtuali", chiamati anche "VTs". Premendo Alt ed un tasto funzione, si puo' scegliere i terminali virtuali; ad ogni tasto funzione corrisponde un terminale virtuale. Slackware prevede login su 6 terminali virtuali di default. Alt+F2 ti portera' sul secondo terminale virtuale, Alt+F3 al terzo, ecc. Il resto dei tasti funzione sono riservati per le sessioni di X. Ogni sessione di X usa il suo terminale virtuale cominciando dal settimo (Alt+F7) e successivi. Quando si e' in X, la combinazione di tasti Alt+tasto_Funzione e' sostituita da Ctrl+Alt+tasto_Funzione; percio' se si e' in X e si vuole tornare alla console testuale senza uscire dalla sessione di X, si puo' usare la combinazione Ctrl+Alt+(F1-F6) (con Alt+F7, si ritorna ad X, supponendo di utilizzare la prima sessione di X).
top
In questo capitolo e' stato discusso di utenti, shell, linea di comando, e dei terminali virtuali. Ora si dovrebbe sentirsi a proprio agio lavorando con la linea di comando, avviando programmi, e usando pipes e operatori di redirezione per combinare i comandi. Infine, si dovrebbe avere un idea sul potere dell'utente root e perche' usare sempre root e' sconsigliato.
top
PREV Capitolo 7 - Sequenza di Boot |
NEXT Capitolo 9 - Struttura del File System |