Sto creando uno script per il mio tema ha 6 opzioni:
1) Controlla l'aggiornamento del tema
2) Reinstalla il tema
3) Installa il carattere
4) Installa lo sfondo
5 ) Controlla l'aggiornamento dello strumento
6) Esci
Ecco il codice
clear
echo "==========================="
echo "Tool for theme"
echo "==========================="
function check_update {
echo "checking theme update"
}
function reinstall_theme {
echo "Reinstalling"
echo "==========================="
}
function font {
echo "Installing font"
}
function wall {
echo "Installing wallpaper"
}
function check_update_tool {
echo "Checking tool update"
}
all_done=0
while (( !all_done )); do
options=("Check theme update" "Reinstall theme" "Install font" "Install wallpaper" "Check tool update" "Quit")
echo "Choose an option: "
select opt in "${options[@]}"; do
case $REPLY in
1) check_update; break ;;
2) reinstall_theme; break ;;
3) font; break ;;
4) wall; break ;;
5) check_update_tool; break ;;
6) all_done=1; break ;;
*) echo "Invalid option" ;;
esac
done
done
echo "Exiting"
sleep 2
Ma quando lo eseguo, le selezioni del menu si confondono
==================
Tool for theme
==================
Choose an option:
1) Check theme update 2) Reinstall theme 3) Install font
4) Install Wallpaper 5) Check tool update 6) Quit
Ma quello che voglio è
===============
Tool for theme
===============
Choose an option:
1) Check theme update
2) Reinstall theme
3) Install font
4) Install wallpaper
5) Check tool update
6) Quit
Quindi come posso correggere il menu?
Risposta accettata:
Puoi impostare il COLUMNS
variabile per limitare la larghezza del display, ad esempio, se la imposti su 12, formatterà il tuo esempio in una singola colonna:
COLUMNS=12
select opt in "${options[@]}"; do
case $REPLY in
1) check_update; break ;;
2) reinstall_theme; break ;;
3) font; break ;;
4) wall; break ;;
5) check_update_tool; break ;;
6) all_done=1; break ;;
*) echo "Invalid option" ;;
esac
produce
===========================
Tool for theme
===========================
Choose an option:
1) Check theme update
2) Reinstall theme
3) Install font
4) Install wallpaper
5) Check tool update
6) Quit
#?
Il manuale bash descrive COLONNE:
Utilizzato da select
comando per determinare la larghezza del terminale durante la stampa degli elenchi di selezione. Impostato automaticamente se il checkwinsize
l'opzione è abilitata (vedi The Shopt Builtin) o in una shell interattiva al ricevimento di un SIGWINCH
.
Oltre a vedere la funzione nella pagina del manuale, aiuta a leggere il codice sorgente per ottenere la storia completa. Questa variabile viene utilizzata nella select_query
funzione, con il commento
/* Print the elements of LIST, one per line, preceded by an index from 1 to LIST_LEN. Then display PROMPT and wait for the user to enter a number. If the number is between 1 and LIST_LEN, return that selection. If EOF is read, return a null string. If a blank line is entered, or an invalid number is entered, the loop is executed again. */
e successivamente, nella select_query
funzione
t = get_string_value ("COLUMNS"); COLS = (t && *t) ? atoi (t) : 80;
Se dai un ragionevole valore, atoi
fornisce risultati ragionevoli (anche zero in questo caso sarebbe plausibile, poiché è meno di 80 colonne e verrebbe restituito da atoi
se imposti COLUMNS
a un valore non numerico). Se non è presente alcun valore, (ad es. COLUMNS=""
), bash
utilizza 80 colonne.
Ulteriori letture:
- atoi – converte una stringa in un numero intero
La chiamata atoi(str) deve essere equivalente a:
(int) strtol(str, (char **)NULL, 10)
- strtol, strtoll – converte una stringa in un intero lungo
Se non è stato possibile eseguire alcuna conversione, verrà restituito 0