Scripts
Guide specifiche
- Guida avanzata di Bash-Scripting (tradotto dal Pluto)
oppure l'originale in inglese Advanced Bash-Scripting Guide (di Mendel Cooper);
I miei script
Nome script: | Tipo: | Versione: | Descrizione: |
---|---|---|---|
my_arxiv | Bash | 0.4 (2010/06/27) | Effettua il backup di una directory creando un archivio e il checksum |
my_backup | Bash | 0.3 (2008/07/02) | Effettua il backup della propria HOME |
my_cddump | Bash | 0.4 (2008/09/26) | Estrae le tracce audio da un CD, convertendole in vari formati |
my_check_burned_iso | Bash | 0.2 (2012/01/14) | Controlla che l'immagine masterizzata coincida con la ISO su disco |
my_find_double | Perl | 0.6c (2010/08/03) | Cerca file doppi ricorsivamente in una o più directory |
my_make_audio_db | Perl | 0.2 (2010/11/01) | Crea un database (file di testo) contenente tutti i dati sui file audio in un certo percorso |
my_make_webgallery | Perl | 0.1 (2010/08/03) | Crea delle pagine html contenenti tutte le immagini in una certa directory (ricorsivamente) |
my_mount_media | Bash | 0.9 (2012/11/05) | Tenta di montare il nuovo dispositivo inserito (pen-drive, hard-disk esterno, dvd, cdrom) |
my_umount_media | Bash | 0.8 (2012/11/11) | Tenta di smontare uno o più dispositivi inseriti |
Oltre agli script in Bash, possono tornare utili le seguenti funzioni (che vanno inserite nel file ~/.bashrc).
my_batt_info(){
declare -i now=0 tot=0
now=$(grep remaining `find /proc/acpi/battery -name state` | awk '{print $3}')
tot=$(grep "last full capacity" `find /proc/acpi/battery -name info` | awk '{print $4}')
if [[ $tot -gt 0 ]]; then
echo $((now*100/tot))"%"
else
echo "[No]"
fi
}
my_burn_iso(){
if [[ $# -ne 2 ]]; then
echo "Usage: my_burn_iso image.iso /dev/optical-device"
else
if [[ -f $1 ]]; then
if [[ -e $2 ]]; then
growisofs -speed=4 -dvd-compat -Z ${2}=${1}
sleep 2
eject $2
else
echo "Device $2 not found"
fi
else
echo "File $1 not found"
fi
fi
}
my_search_history(){
if [ -n "$1" ]; then
grep --colour -- "$1" ~/.bash_history
else
echo "Usage: my_search_history <word>"
return 1
fi
}
my_search_word(){
if [ -n "$1" ]; then
find . -type f -exec grep -H --color -- "$1" {} \;
else
echo "Usage: my_search_word <word>"
return 1
fi
}
declare -i now=0 tot=0
now=$(grep remaining `find /proc/acpi/battery -name state` | awk '{print $3}')
tot=$(grep "last full capacity" `find /proc/acpi/battery -name info` | awk '{print $4}')
if [[ $tot -gt 0 ]]; then
echo $((now*100/tot))"%"
else
echo "[No]"
fi
}
my_burn_iso(){
if [[ $# -ne 2 ]]; then
echo "Usage: my_burn_iso image.iso /dev/optical-device"
else
if [[ -f $1 ]]; then
if [[ -e $2 ]]; then
growisofs -speed=4 -dvd-compat -Z ${2}=${1}
sleep 2
eject $2
else
echo "Device $2 not found"
fi
else
echo "File $1 not found"
fi
fi
}
my_search_history(){
if [ -n "$1" ]; then
grep --colour -- "$1" ~/.bash_history
else
echo "Usage: my_search_history <word>"
return 1
fi
}
my_search_word(){
if [ -n "$1" ]; then
find . -type f -exec grep -H --color -- "$1" {} \;
else
echo "Usage: my_search_word <word>"
return 1
fi
}
Le seguenti funzioni servono rispettivamente ad avviare e fermare un processo che aggiorna l'ora in alto a destra sul terminale:
my_start_clock(){
if [ -n "$clockd_pid" ]; then
echo "running!"
else
while true; do
yc=$((`tput cols`-10))
echo -en "\033[s\033[1;${yc}H\033[1;31m[`date +%H:%M:%S`]\033[0m\033[u"
sleep 1
done &
export clockd_pid=$!
fi
}
my_stop_clock(){
if [ -n "$clockd_pid" ]; then
kill -3 $clockd_pid
yc=$((`tput cols`-10))
echo -en "\033[s\033[1;${yc}H \033[u"
else
echo "not running!"
fi
unset clockd_pid
}
if [ -n "$clockd_pid" ]; then
echo "running!"
else
while true; do
yc=$((`tput cols`-10))
echo -en "\033[s\033[1;${yc}H\033[1;31m[`date +%H:%M:%S`]\033[0m\033[u"
sleep 1
done &
export clockd_pid=$!
fi
}
my_stop_clock(){
if [ -n "$clockd_pid" ]; then
kill -3 $clockd_pid
yc=$((`tput cols`-10))
echo -en "\033[s\033[1;${yc}H \033[u"
else
echo "not running!"
fi
unset clockd_pid
}