| Author |
Message |
scsijon
Joined: 23 May 2007 Posts: 923 Location: the australian mallee
|
Posted: 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.
_________________ Mage2 in final Beta! http://www.murga-linux.com/puppy/viewtopic.php?t=72565
Last edited by scsijon on Fri 01 Mar 2013, 05:06; edited 1 time in total
|
|
Back to top
|
|
 |
rcrsn51

Joined: 05 Sep 2006 Posts: 7756 Location: Stratford, Ontario
|
Posted: 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
|
|
 |
scsijon
Joined: 23 May 2007 Posts: 923 Location: the australian mallee
|
Posted: 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
|
|
 |
6502coder
Joined: 23 Mar 2009 Posts: 29
|
Posted: 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
|
|
 |
mavrothal

Joined: 24 Aug 2009 Posts: 1058
|
Posted: 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 
|
|
Back to top
|
|
 |
6502coder
Joined: 23 Mar 2009 Posts: 29
|
Posted: 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
|
|
 |
mavrothal

Joined: 24 Aug 2009 Posts: 1058
|
Posted: Sat 23 Feb 2013, 02:36 Post subject:
|
|
Sorry 6502coder, didn't see your post
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 
|
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1759
|
Posted: 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
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: 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
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1759
|
Posted: 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
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: 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.
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: 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
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
scsijon
Joined: 23 May 2007 Posts: 923 Location: the australian mallee
|
Posted: Fri 01 Mar 2013, 05:07 Post subject:
|
|
thank you all, I think I have enough to work through
|
|
Back to top
|
|
 |
|