Aspetti Avanzati
Linea di Comando
Durante l'editazione di un comando vi sono una serie di caratteri di controllo che si possono usare per avere più produttività.
Comando | Effetto |
---|---|
Ctrl-A | All'inizio del comando |
Ctrl-E | Alla fine del comando |
Ctrl-U | Cancella intera linea (salva nel buffer) |
Ctrl-W | Cancella parola precedente (salva nel buffer) |
Ctrl-Y | Paste del buffer |
Ctrl-B | Indietro di un carattere |
Ctrl-F | Avanti di un carattere |
Ctrl-P | Comando precedente |
Ctrl-N | Comando seguente |
Ctrl-T | Inverte i due caratteri precedenti |
Ctrl-O | Esegue la linea di comando |
Ctrl-R stringa | Ricerca all'indietro nella storia per la stringa |
Ctrl-L | Pulisce il video |
Ctrl-X Ctrl-E | Inserisce la linea di comando in una sessione di editor, e lo esegue quando l'editor termina |
Quale editor venga usato è dato dal valore della variabile d'ambiente EDITOR
.
tput
Invia una sequenza di controllo al terminale, come specificata in terminfo
.
Comando tput | Effetto |
---|---|
tputcolors | ritorna il numero di colori supportati |
tput lines | ritorna il numero di linee delterminale corrente |
tput cols | ritorna il numero di colonne del terminale corrente |
tput sc | salva la posizione del cursore |
tput rc | restore della posizione del cursore |
tput home | cursore in alto a sinistra (0, 0) |
tput cup x y | cursore a posizione (x, y) |
tput cud1 | cursore giù si una linea |
tput cup1 | cursore su di una linea |
tput civis | cursore invisibile |
tput cnorm | cursore normale |
tput bold | testo in bold |
tput smul | inizio testo sottolineato |
tput rmul | fine testo sottolineato |
tput rev | testo a colori invertiti |
tput blink | testo lampeggiante |
tput invis | testo invisibile |
tput smso | testo a modalità standout |
tput rmso | fine modalità standout |
tput sgr0 | toglie tutti gli attributi al testo |
tput setaf valore | colore di foreground |
tput setab valore | valore di background |
tput smcap | salva l'intero schermo |
tput rmcap | restore dello schermo |
tput el | pulisce fino a fine linea |
tput el1 | pulisce fino a inizio linea |
tput ed | pulisce dal cursore a fine video |
tput clear | pulisce tutto il video |
I valori per i colori sono:
Valore | Colore |
---|---|
0 | nero |
1 | rosso |
2 | verde |
3 | giallo |
4 | blu |
5 | magenta |
6 | ciano |
7 | bianco |
8 | non usato |
9 | restore dei colori originali |
Non tutti i terminali possiedono tutte le capabilities.
Il seguente programma testa l'acquisizione dinamica delle dimensioni del terminale corrente.
Fa uso del fatto che X Window invia il segnale WINCH al terminale al variare delle proprietà della finestra del terminale.
(size.sh):
#!/bin/bash
# term_size2 - Dynamically display terminal window size
redraw() {
clear
echo "Width = $(tput cols) Height = $(tput lines)"
}
trap redraw WINCH
redraw
while true; do
:
done
Il prossimo programma testa vari attributi del testo.
(tput.sh):
!/bin/bash
# tput_characters - Test various character attributes
clear
echo "tput character test"
echo "==================="
echo
tput bold; echo "This text has the bold attribute."; tput sgr0
tput smul; echo "This text is underlined (smul)."; tput rmul
# Most terminal emulators do not support blinking text (though xterm
# does) because blinking text is considered to be in bad taste ;-)
tput blink; echo "This text is blinking (blink)."; tput sgr0
tput rev; echo "This text has the reverse attribute"; tput sgr0
# Standout mode is reverse on many terminals, bold on others.
tput smso; echo "This text is in standout mode (smso)."; tput rmso
tput sgr0
echo
Il prossimo programma prova varie combinazioni di colori.
(colors.sh):
#!/bin/bash
# tput_colors - Demonstrate color combinations.
for fg_color in {0..7}; do
set_foreground=$(tput setaf $fg_color)
for bg_color in {0..7}; do
set_background=$(tput setab $bg_color)
echo -n $set_background$set_foreground
printf ' F:%s B:%s ' $fg_color $bg_color
done
echo $(tput sgr0)
done
L'ultimo programma illustra la visualizzazione di alcune proprietà del sistema, guidata da menù.
(menu.sh):
#!/bin/bash
# tput_menu: a menu driven system information program
BG_BLUE="$(tput setab 4)"
BG_BLACK="$(tput setab 0)"
FG_GREEN="$(tput setaf 2)"
FG_WHITE="$(tput setaf 7)"
# Save screen
tput smcup
# Display menu until selection == 0
while [[ $REPLY != 0 ]]; do
echo -n ${BG_BLUE}${FG_WHITE}
clear
cat <<- EOF
Please Select:
1. Display Hostname and Uptime
2. Display Disk Space
3. Display Home Space Utilization
0. Quit
EOF
read -p "Enter selection [0-3] > " selection
# Clear area beneath menu
tput cup 10 0
echo -n ${BG_BLACK}${FG_GREEN}
tput ed
tput cup 11 0
# Act on selection
case $selection in
1) echo "Hostname: $HOSTNAME"
uptime
;;
2) df -h
;;
3) if [[ $(id -u) -eq 0 ]]; then
echo "Home Space Utilization (All Users)"
du -sh /home/* 2> /dev/null
else
echo "Home Space Utilization ($USER)"
du -s $HOME/* 2> /dev/null | sort -nr
fi
;;
0) break
;;
*) echo "Invalid entry."
;;
esac
printf "\n\nPress any key to continue."
read -n 1
done
# Restore screen
tput rmcup
echo "Program terminated."
Il seguente comando aggiorna perennemente la data e l'ora ponendole nell'angolo in alto a destra dello schermo.
while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-20));date +'%D %T';tput rc;done &
Colori
Alcuni terminali supportano 256 colori. Si possono ottenere mandando direttamente a terminale le sequenze di escape ANSI.
Il seguente programma prova tutti i colori di foreground e background:
(fbcolors.sh):
#! /bin/bash
for code in {0..255}
do
echo -e "\e[38;05;${code}m $code: Test"
done
for code in {0..255}
do
echo -e "\e[48;05;${code}m $code: Test \e[48;05;0m"
done