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

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
scsijon
Posts: 1596
Joined: Thu 24 May 2007, 03:59
Location: the australian mallee
Contact:

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

#1 Post 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.
Last edited by scsijon on Fri 01 Mar 2013, 09:06, edited 1 time in total.

User avatar
rcrsn51
Posts: 13096
Joined: Tue 05 Sep 2006, 13:50
Location: Stratford, Ontario

#2 Post by rcrsn51 »

What are you testing for? Files that don't exist or files that exist but are empty?

scsijon
Posts: 1596
Joined: Thu 24 May 2007, 03:59
Location: the australian mallee
Contact:

#3 Post 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

User avatar
6502coder
Posts: 677
Joined: Mon 23 Mar 2009, 18:07
Location: Western United States

test for zero bytes filesize

#4 Post 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.

User avatar
mavrothal
Posts: 3096
Joined: Mon 24 Aug 2009, 18:23

#5 Post 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
== [url=http://www.catb.org/esr/faqs/smart-questions.html]Here is how to solve your[/url] [url=https://www.chiark.greenend.org.uk/~sgtatham/bugs.html]Linux problems fast[/url] ==

User avatar
6502coder
Posts: 677
Joined: Mon 23 Mar 2009, 18:07
Location: Western United States

#6 Post by 6502coder »

He mentioned using

if [ -f ... ]

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

User avatar
mavrothal
Posts: 3096
Joined: Mon 24 Aug 2009, 18:23

#7 Post 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
== [url=http://www.catb.org/esr/faqs/smart-questions.html]Here is how to solve your[/url] [url=https://www.chiark.greenend.org.uk/~sgtatham/bugs.html]Linux problems fast[/url] ==

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#8 Post 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.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#9 Post 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
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#10 Post 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'.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#11 Post 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.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#12 Post 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
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

scsijon
Posts: 1596
Joined: Thu 24 May 2007, 03:59
Location: the australian mallee
Contact:

#13 Post by scsijon »

thank you all, I think I have enough to work through

Post Reply