[SOLVED] Interrupt the script, if the compress file size more than 20M
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Interrupt the script, if the compress file size more than 20M
Hi friends,
I'm trying to make a program for compress some files of a particular directory, but I want to Interrupt the process if the generated file size more than 20M.
I was Thinking in use while, but It doesn't work..
Some Idea?
Code:
Code:
#!/bin/bash
#######################
cd /home/user/
while (( $(ls -s $1$(date +%y%d%m).tgz | awk '{print $1}') < 20000 ))
do
tar cvzpf $1.tgz $1/ --exclude=file1 --exclude=file2 --exclude=file3
done
####################
I think you could pipe it through head which will close when/if it gets too many bytes:
Code:
M=$((1024 * 1024))
tar cvp "$1/" --exclude=file1 --exclude=file2 --exclude=file3 | gzip | head -c $((20*M)) > "$1.tgz"
# see if it was interrupted
if gzip -t $1.tgz ; then
echo "$1.tgz fits in less than 20M"
else
echo "$1.tgz is too big"
rm $1.tgz
done
Last edited by ntubski; 10-11-2013 at 10:41 AM.
Reason: have my "when" and "if" it too.
It helps me a lot I could finish my script I share it with you maybe Someone found something helpful:
Code:
Code:
#!/bin/bash
MINE_CLIENT=$1
if [ "{$MINE_CLIENT:-NULL}" = "NULL" ] ; then
echo "No se Ingreso nombre de Cliente, Escriba TarClient.sh xyz(Iniciales de Cliente)"
else
#Creando el tgz de los ddb's.
cd /dsp/client/opns/
mkdir ftddbs
ls *.ddb | grep -v 0 | grep -v 1 | grep -v 2 > t
for ddb in ` cat t ` ; do
cp -p $ddb ftddbs
done
tar cvzpf ftddbs$(date +%y%m%d%H%M).tgz ftddbs
if gzip -t ftddbs$(date +%y%m%d%H%M).tgz ; then
echo "Se creo Satisfactoriamente el ftddbs$(date +%y%m%d%H%M).tgz"
rm -rf ftddbs/ t
mkdir /dsp/cli/vl$(date +%y%m%d)
mv ftddbs$(date +%y%m%d%H%M).tgz /dsp/cli/vl$(date +%y%m%d)
else
echo "Ocurrio un error al crear el archivo"
fi
#Creando el tgz del cliente.
cd /dsp/cli/
M=$((1024 * 1024))
tar cvzp "$1/" --exclude=opns --exclude=surface --exclude=qt --exclude=psw/gpslog --exclude=psw/log --exclude=psw/old \
--exclude=backup --exclude=zbckp2/ | head -c $((20*M)) > "$1.tgz"
#Revisar si el archivo pesara mas de 20 Megas.
if gzip -t $1.tgz ; then
echo "$1.tgz se comprimio con menos de 20 Megas"
mv $1.tgz /dsp/cli/vl$(date +%y%m%d)
tar cvpf vl$(date +%y%m%d).tgz vl$(date +%y%m%d)/
rm -rf vl$(date +%y%m%d)/
else
echo "$1.tgz es demasiado pesado, excluya mas carpetas"
rm $1.tgz
fi
fi
P.D.: Sorry for the late replay, but I was very busy these days, so Thanks again.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.