View Full Version : [HELP] Creare uno script bash x comprimere in tar
Vorrei poter creare uno script ke mi facesse questo :
Scrivendo "compatta etc" lui mi esegua il comando "tar -cvzf backup_etc.tar.gz etc"; se invece scrivo "compatta home/user1" mi esegua "tar -cvzf backup_user1.tar.gz home/user1" ecc ecc
Qlc anima pia me lo può scrivere ke io d bash nn ne so nulla ?
TNK
#!/bin/bash
nome_file=backup_`echo $1 |tr / _`
tar cvfz $nome_file $1
sbomberino
14-07-2004, 10:53
#!/bin/sh
tar zcvf backup_usr1.tar.gz $1
$1 sarebbe la prima stringa che viene dopo lo script lanciato, in questo caso compatta.sh
un'affinamento potrebbe essere quello di verificare che $1 non sia nullo
#!/bin/sh
if [ "$1" = "" ] ; then
echo "Usare compatta.sh <file o directory da comprimere>"
exit
else
tar zcvf backup_usr1.tar.gz $1
fi
si potrebbero fare altri controlli... ma a diventerebbe più semplice usare direttamente tar...
al limite puoi tarti un alias... tipo
alias compatta="tar zcvf backup.tar.gz"
poi useresti compatta /home/pippo
CIAO!
:D
ilsensine
14-07-2004, 11:08
Questo fa esattamente quello che chiedi:
#!/bin/bash
name=`echo $1 | sed s/\\\\/$// |sed s/.*\\\\///`
tar czvf "backup_$name.tar.gz" $1
Puoi aggiungere il controllo che $name non sia nullo, se vuoi.
Grazie a tutti e 2.... combinando i vs script ho fatto esattamente ciò ke volevo :D
#!/bin/bash
#
#Usage ./compatta.sh <dirname>
#
nome_file=backup_`echo $1 |tr / _`.tar.gz
if [ "$1" = "" ] ; then
echo "Errore : Usare compatta.sh <file o directory da comprimere>"
exit
else
tar cvfz $nome_file $1
echo "OK : Backup effettuato con successo !!!"
fi
:P
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.