Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 02 Sep 2014, 22:08
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
how do I use if-then-else-fi on a filesize?[solved]
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [13 Posts]  
Author Message
scsijon

Joined: 23 May 2007
Posts: 1026
Location: the australian mallee

PostPosted: Fri 22 Feb 2013, 20:50    Post subject:  how do I use if-then-else-fi on a filesize?[solved]  

I'm trying to do something like this in a simple ash script.

Code:

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:

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


then it would become

Code:

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.

_________________
Puppy T290 - BarryK's Racy but with T2 Version 9.0 is under development.
http://www.murga-linux.com/puppy/viewtopic.php?t=94575

Last edited by scsijon on Fri 01 Mar 2013, 05:06; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website 
rcrsn51


Joined: 05 Sep 2006
Posts: 9066
Location: Stratford, Ontario

PostPosted: Fri 22 Feb 2013, 22:47    Post subject:  

What are you testing for? Files that don't exist or files that exist but are empty?
Back to top
View user's profile Send private message 
scsijon

Joined: 23 May 2007
Posts: 1026
Location: the australian mallee

PostPosted: Fri 22 Feb 2013, 23:33    Post subject:  

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
Back to top
View user's profile Send private message Visit poster's website 
6502coder

Joined: 23 Mar 2009
Posts: 86
Location: Western United States

PostPosted: Sat 23 Feb 2013, 02:19    Post subject: test for zero bytes filesize  

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.
Back to top
View user's profile Send private message 
mavrothal


Joined: 24 Aug 2009
Posts: 1603

PostPosted: Sat 23 Feb 2013, 02:26    Post subject:  

Code:
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:
file /filepath/filename | grep empty
also works if the file name does not have "empty" in it
_________________
Kids all over the world go around with an XO laptop. They deserve one puppy (or many) too Very Happy
Back to top
View user's profile Send private message 
6502coder

Joined: 23 Mar 2009
Posts: 86
Location: Western United States

PostPosted: Sat 23 Feb 2013, 02:32    Post subject:  

He mentioned using

if [ -f ... ]

so I assumed he was testing a regular file, as the "-f" test does not apply otherwise.
Back to top
View user's profile Send private message 
mavrothal


Joined: 24 Aug 2009
Posts: 1603

PostPosted: Sat 23 Feb 2013, 02:36    Post subject:  

Sorry 6502coder, didn't see your post Embarassed
regarding "-f", I have no idea what will be used for so try to cover all bases

_________________
Kids all over the world go around with an XO laptop. They deserve one puppy (or many) too Very Happy
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2240

PostPosted: Sat 23 Feb 2013, 02:54    Post subject:  

Code:
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.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4348

PostPosted: Sat 23 Feb 2013, 04:06    Post subject:  

I _think_ you can get the size using only shell
Code:
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

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2240

PostPosted: Sat 23 Feb 2013, 05:40    Post subject:  

Code:
IFS=""
read VAR < the_file
echo ${#VAR}

Ouch, that could be hard on RAM for something like 'read VAR < seamonkey-static-bin'.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4348

PostPosted: Sat 23 Feb 2013, 07:02    Post subject:  

amigo wrote:
Code:
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.

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4348

PostPosted: Sun 24 Feb 2013, 15:54    Post subject:  

apparently ash read does not support the -d parameter, so this will work in bash:
Code:
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

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
scsijon

Joined: 23 May 2007
Posts: 1026
Location: the australian mallee

PostPosted: Fri 01 Mar 2013, 05:07    Post subject:  

thank you all, I think I have enough to work through
Back to top
View user's profile Send private message Visit poster's website 
Display posts from previous:   Sort by:   
Page 1 of 1 [13 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0742s ][ Queries: 11 (0.0063s) ][ GZIP on ]