Script di gestione della history e dei pacchetti installati
Inviato: sabato 30 luglio 2011, 16:27
Ciao a tutti.
Con le motivazioni espresso in questo post, ho deciso di fare un piccolo script per venire incontro ai nuovi utenti che volessero tenere traccia dei pacchetti installati ma soprattutto di tutti i comandi impartiti al sistema.
Onde evitare di affogare la discussione sul terminale con scambi di messaggi relativi a questo script, su suggerimento anche di altri utenti, ho deciso di aprire questa discussione per continuare qui.
Lo script funziona ma deve ancora passare sotto alcune revisioni.
Ovviamente sono bene accette critiche di qualsiasi tipo, volte al miglioramento dello stesso.
Ogni suggerimento sarà ponderato ed eventualmente inserito nell'ambito di una futura revisione.
Ecco lo script (spero di non aver fatto di nuovi disastri con il copia incolla)
(30 luglio 2011 ore 16:17)
una volta salvato lo script e impostati i giusti permessi, consiglio di lanciare:
per un breve help.
ciao
Con le motivazioni espresso in questo post, ho deciso di fare un piccolo script per venire incontro ai nuovi utenti che volessero tenere traccia dei pacchetti installati ma soprattutto di tutti i comandi impartiti al sistema.
Onde evitare di affogare la discussione sul terminale con scambi di messaggi relativi a questo script, su suggerimento anche di altri utenti, ho deciso di aprire questa discussione per continuare qui.
Lo script funziona ma deve ancora passare sotto alcune revisioni.
Ovviamente sono bene accette critiche di qualsiasi tipo, volte al miglioramento dello stesso.
Ogni suggerimento sarà ponderato ed eventualmente inserito nell'ambito di una futura revisione.
Ecco lo script (spero di non aver fatto di nuovi disastri con il copia incolla)
(30 luglio 2011 ore 16:17)
Codice: Seleziona tutto
#! /bin/bash
#This script creates two kind of txt file
#depending on parameter passed to the script
#and on the BACKUP directory you choose. (default is the one in which you put this script)
#Feel free to write your favorite directory in the line below,
#possibly withtout the ending /
BACKUP=
#Txt file created are
#an historcal list of sorted and univocally occurring bash commands
#and a list of installed packages.
#Txt file name is univocally determinated by date
#FURTHER DEVELOPMENTS
#LANGUAGE recognition
#commands tab-completion
#... others :)
LANGUAGE=$LANG
LANGUAGE=${LANGUAGE:-"it_IT.utf8"}
BACKUP=${BACKUP:-$(dirname `readlink -f $0`)}
if ! [ -w "${BACKUP}" ]; then
echo -e "$BACKUP: Permesso negato"
exit 1
fi
ARGV=("$@")
AVAILABLE_OPTIONS=("-packages" "--remove-list" "-history" "--merge-history" "--merge-all" "--remove-history")
#PACKAGESFILE=`find ${BACKUP} -maxdepth 1 -name "my-packages*txt" | sort | tail -n 1`
HISTORYFILE=`find ${BACKUP} -maxdepth 1 -name "sortedUniqHistory*txt" | sort | tail -n 1`
#echo ${HISTORYFILE}
#date in file name compatible format
DATA=`date +%F-%R:%S`
function contains() {
local n=$#
local value=${!n}
for ((i=1;i < $#;i++)) {
if [ "${!i}" == "${value}" ]; then
return 0
fi
}
return 1
}
function Usage() {
echo -e "\nSYNOPSIS\n"
echo -e "\t$0 \n\t[ -packages | --remove-list ] \n\t[ -history | --merge-history | --merge-all | --remove-history ]\n"
echo -e "DESCRIPTION\n"
echo -e "-packages:\n\tViene creato in\n\t${BACKUP}\n\tun file contenente tutti i pacchetti installati.\n"
echo -e "--remove-list:\n\tVengono eliminati i file my-packages presenti in\n\t${BACKUP}.\n\tViene poi creato un nuovo file my-packages.\n"
echo -e "-history:\n\tViene creato in\n\t${BACKUP}\n\tun file con il contenuto di\n\t$HOME/bash_history.\n\tTale contenuto è elaborato in modo che\n\tsia in ordine alfabetico\n\te ogni comando compaia una volta.\n"
echo -e "--merge-history:\n\tCome -history,\n\tma il file creato viene unito al più recente trovato in\n\t${BACKUP}.\n\tIn caso non venga trovato un file recente,\n\tnon viene elaborato nessun file.\n"
echo -e "--merge-all:\n\tCome -history,\n\tma il file creato viene unito a tutti quelli trovati in\n\t${BACKUP}.\n"
echo -e "--remove-history:\n\tRimuove tutti i file con lo storico dei comandi,\n\tcontenuti in ${BACKUP},\n\tescluso il più recente.\n"
exit 1
}
if [ $# = 0 ]; then
echo -e "Usare «$0 --help» per ulteriori informazioni."
exit 1
elif [ $# = 1 ] && [ ${ARGV[0]} = --help ]; then
Usage
fi
wrong_option_flag=1
for item in ${ARGV[@]}; do
if ! contains "${AVAILABLE_OPTIONS[@]}" "${item}"; then
echo -e "$0": opzione non riconosciuta \"${item}\"
wrong_option_flag=0
fi
done
if [ ${wrong_option_flag} = 0 ] ; then
echo -e "Usare «$0 --help» per ulteriori informazioni."
exit 1
fi
#--remove-list:
#if writing permission is granted, al my-pachages file in $BACKUP dir
#will be removed
#otherwise, a warning will be printed
#and then... a new my-packages file will be created
if contains "${ARGV[@]}" "--remove-list"; then
if contains "${ARGV[@]}" "-packages"; then
echo -e "Usare «$0 --help» per ulteriori informazioni."
else
for item in `find ${BACKUP} -maxdepth 1 -name "my-packages*txt"`; do
if [ -w "${item}" ]; then
rm "${item}"
elif ! [ -w "${item}" ]; then
echo "${item}: Permesso negato"
fi
done
dpkg --get-selections > ${BACKUP}/my-packages`echo -$DATA`.txt
fi
#-packages:
#if an already existing file is finded in $BACKUP a warning is printed
#otherwise, a my-packages file is created
elif contains "${ARGV[@]}" "-packages"; then
if [ -e ${BACKUP}/my-packages`echo -$DATA`.txt ]; then
echo -e "${BACKUP}/my-packages`echo -$DATA`.txt": file esistente
else
dpkg --get-selections > ${BACKUP}/my-packages`echo -$DATA`.txt
fi
fi
#-history:
#if an already existing file is finded in $BACKUP, a warning is printed
#an
if contains "${ARGV[@]}" "-history"; then
if contains "${ARGV[@]}" "--merge-history" || contains "${ARGV[@]}" "--remove-history" || contains "${ARGV[@]}" "--merge-all"; then
echo -e "Usare «$0 --help» per ulteriori informazioni."
else
if ! [ -e ${BACKUP}/sortedUniqHistory`echo -$DATA`.txt ]; then
cat ${HOME}/.bash_history | sort | uniq > ${BACKUP}/sortedUniqHistory`echo -$DATA`.txt
elif ! [ -w ${BACKUP}/sortedUniqHistory`echo -$DATA`.txt ]; then
echo "${BACKUP}/sortedUniqHistory`echo -$DATA`.txt: permesso in scrittura negato"
elif [ -w ${BACKUP}/sortedUniqHistory`echo -$DATA`.txt ]; then
echo -e "${BACKUP}/sortedUniqHistory`echo -$DATA`.txt": file esistente
echo -n "Procedere comunque, sovrascrivendo il file? (y/n): "
read risposta
case ${risposta} in
y)
cat ${HOME}/.bash_history | sort | uniq > ${BACKUP}/sortedUniqHistory`echo -$DATA`.txt
;;
*)
;;
esac
fi
fi
elif contains "${ARGV[@]}" "--merge-history"; then
if contains "${ARGV[@]}" "--remove-history" || contains "${ARGV[@]}" "--merge-all"; then
echo -e "Usare «$0 --help» per ulteriori informazioni."
elif ! [ -e "${HISTORYFILE}" ]; then
echo -e "Non è stato trovato un file da fondere"
elif [ "${HISTORYFILE}" = ${BACKUP}/sortedUniqHistory`echo -$DATA`.txt ]; then
echo -e "${HISTORYFILE}": file di destinazione esistente
elif ! [ -r "${HISTORYFILE}" ]; then
echo -e "${HISTORYFILE}: Permesso in lettura negato"
elif [ -r "${HISTORYFILE}" ]; then
cat ${HISTORYFILE} ${HOME}/.bash_history | sort | uniq >> ${BACKUP}/sortedUniqHistory`echo -$DATA`.txt
fi
elif contains "${ARGV[@]}" "--remove-history"; then
if contains "${ARGV[@]}" "--merge-all"; then
echo -e "Usare «$0 --help» per ulteriori informazioni."
else
for item in `find ${BACKUP} -maxdepth 1 -name "sortedUniqHistory*txt"`; do
if [ "${item}" != "${HISTORYFILE}" ]; then
if [ -w "${item}" ]; then
rm "${item}"
elif ! [ -w "${item}" ]; then
echo "${item}: Permesso negato"
fi
else
echo "${item}: file più recente, non verrà cancellato"
fi
done
fi
elif contains "${ARGV[@]}" "--merge-all"; then
output="${BACKUP}/sortedUniqHistory`echo -$DATA`.txt"
if [ -e "${output}" ]; then
echo "${BACKUP}/sortedUniqHistory`echo -$DATA`.txt": file di destinazione esistente
elif ! [ -e "${output}" ]; then
cat `find ${BACKUP} -maxdepth 1 -name "sortedUniqHistory*txt"` ${HOME}/.bash_history | sort | uniq > "${output}"
#echo `find ${BACKUP} -maxdepth 1 -name "sortedUniqHistory*txt" | wc -l`
if [ `find ${BACKUP} -maxdepth 1 -name "sortedUniqHistory*txt" | wc -l` -gt 1 ]; then
echo -n "Si desidera eliminare i file elaborati? (y/n):"
read risposta
case "${risposta}" in
y)
for item in `find ${BACKUP} -maxdepth 1 -name "sortedUniqHistory*txt"`; do
if [ "${item}" != "${output}" ]; then
if [ -w "${item}" ]; then
rm "${item}"
elif ! [ -w "${item}" ]; then
echo "${item}: permesso negato"
fi
fi
done
;;
*)
;;
esac
fi
fi
fi
Codice: Seleziona tutto
./nomescript.sh --helpciao