Page 1 of 1

how do I use if-then-else-fi on a filesize?[solved]

Posted: Sat 23 Feb 2013, 00:50
by scsijon
I'm trying to do something like this in a simple ash script.

Code: Select all

If [size of a-b-c = 0bytes]; then  #where a-b-c is a filename
{do a multiple lines of nested code something} 
else {dosomethingsimple}
fi
I know that if the test could be the other way around so it became

Code: Select all

If [size of a-b-c != 0bytes]; then 
then it would become

Code: Select all

If [-f a-b-c]; then 
but doing so would require quite a rewrite of the {do a multiple lines of nested code something} code to suit and i'm rather reluctant to do it again for the xxth time unless absolutely necessary.

I can't seem to find any guide or examples online to follow without having to write a 'page' of code. I have looked at the bash guide for beginners and ash code sites but no joy.

Can anyone provide a pointer or code please, surely it's not that hard!

thanks


For those wondering what i'm up to at the moment. I'm well on the way to adding opensuse to our woof distro list. It's where I came from when I found and started with Puppy 2.12 and with opensuse having both i586 and x86_64 available, plus having a packagelist counting at well over 10,000 packages, it could just be an interesting addition. And no, mage2 is still on the boil, I'm just awaiting a couple of fixes upstream to occur, then it should be out.

Posted: Sat 23 Feb 2013, 02:47
by rcrsn51
What are you testing for? Files that don't exist or files that exist but are empty?

Posted: Sat 23 Feb 2013, 03:33
by scsijon
I was looking for it being empty (0 bytes), the file has to exist to get to this point.

However it's been 'dead' quiet here (so far) for once, and I brought all the code in with me on a stick 'to play with'.

i've rewritten the code sections to work the other way by rearanging some = and != and moving some of the code back into the previous perl script that creates it.

If you have an idea, i'd still like it, as I will be able to use it elsewhere.

thanks

test for zero bytes filesize

Posted: Sat 23 Feb 2013, 06:19
by 6502coder
Does this help?

#!/bin/bash
fsize=`ls -l $1 | cut -d ' ' -f 5`
if [ $fsize -eq 0 ]
then
echo "Zero bytes"
fi

That string after "cut -d " is a single space, in single quotes.
The whole string after "fsize=" is in backquotes.

Of course you replace $1 with the name of the file you want to test.

Posted: Sat 23 Feb 2013, 06:26
by mavrothal

Code: Select all

ls -l /filepath/filename | cut -f 5 -d ' '
will tell you the size in bytes. Zero if it is empty.
Does not work with empty folders. Does not work for symlinks (even if they point to an empty file)
awk instead of cut should work too.

Code: Select all

file /filepath/filename | grep empty
also works if the file name does not have "empty" in it

Posted: Sat 23 Feb 2013, 06:32
by 6502coder
He mentioned using

if [ -f ... ]

so I assumed he was testing a regular file, as the "-f" test does not apply otherwise.

Posted: Sat 23 Feb 2013, 06:36
by mavrothal
Sorry 6502coder, didn't see your post :oops:
regarding "-f", I have no idea what will be used for so try to cover all bases

Posted: Sat 23 Feb 2013, 06:54
by amigo

Code: Select all

if [[ -s file ]] ; then
echo "File is non-zero size"
fi
Running 'help test' in the shell will show you more. If you need to check for a certain size of file, then 'stat -c %s file' is the most direct way. Of course you can use 'ls -l' or 'du' as well.

Posted: Sat 23 Feb 2013, 08:06
by technosaurus
I _think_ you can get the size using only shell

Code: Select all

IFS=""
read VAR < the_file
echo ${#VAR}
...but I can't check it at the moment and would definitely not recommend it for large files, but handy if you will be using it often since you have a cached copy (not sure if setting VAR="" will free the memory either) and you may want to save your IFS first like OLDIFS="$IFS" so you can restore it

Posted: Sat 23 Feb 2013, 09:40
by amigo

Code: Select all

IFS=""
read VAR < the_file
echo ${#VAR}
Ouch, that could be hard on RAM for something like 'read VAR < seamonkey-static-bin'.

Posted: Sat 23 Feb 2013, 11:02
by technosaurus
amigo wrote:

Code: Select all

IFS=""
read VAR < the_file
echo ${#VAR}
Ouch, that could be hard on RAM for something like 'read VAR < seamonkey-static-bin'.
Which is why I didn't recomend it for large files. If it were a bash script you could limit the number of characters, but I can't recall if ash can.

Posted: Sun 24 Feb 2013, 19:54
by technosaurus
apparently ash read does not support the -d parameter, so this will work in bash:

Code: Select all

IFS="" read -n1 -d "" LINE <file
NONZEROSIZE=${#LINE}
but ash would need the relatively easy to implement -d parameter (I think it is currently hard coded to \n)... it seems like I have needed this feature before

Posted: Fri 01 Mar 2013, 09:07
by scsijon
thank you all, I think I have enough to work through