LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-26-2008, 07:31 PM   #1
lorenww
LQ Newbie
 
Registered: Feb 2008
Location: Was rainy Oregon now sunny Florida
Posts: 8

Rep: Reputation: 0
bash and filesize for noob


Hi all,

I am new to bash and live off of php and mysql

I have been searching for the answer and found this on this website as well as a few others.

Code:
FILENAME=/home/heiko/dummy/packages.txt
FILESIZE=$(stat -c%s "$FILENAME")
echo "Size of $FILENAME = $FILESIZE bytes."
Changed
FILENAME=/home/heiko/dummy/packages.txt
to
FILENAME=/z-heartbeat.php

and gets the error "file not found".

I have no idea what the path is but the files are in jffs

Anyway this is what I am looking for

Code:
echo ============== with quotes ===============
if [ "z-heartbeat.php -s" ] 
then
echo heartbeat file is empty
else
echo heartbeat File is full
fi

if [ "z-chilli.php -s" ] 
then
echo chilli file is empty
else
echo chilli File is full
fi

echo =============== with out quotes ==============

if [ z-heartbeat.php -s ] 
then
echo heartbeat file is empty
else
echo heartbeat File is full
fi

if [ z-chilli.php -s ] 
then
echo chilli file is empty
else
echo chilli File is full
fi
with quotes is always empty
without quotes is always full

everyting I read tells me this should sort out the empty files.

After 5-6 hrs with no joy prompted me to join this forum and ask directions.

Any suggestions to sort this out are greatly appreciated.
I can tell there will be more questions as this progresses.

TIA
Loren
 
Old 02-26-2008, 08:10 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
The syntax is around the wrong way. Have a look at the advanced bash scripting guide, it's useful for tasks like this. The "-s" test is for non-zero status. Try it this way:
Code:
if [ -s "z-heartbeat.php"]
then
  echo heartbeat file is not-zero size
else
  echo heartbeat file is zero size
fi
if [ -z "z-heartbeat.php"]
then
  echo heartbeat file is zero length
else
  echo heartbeat file is not-zero length
fi
Is that what you were trying to do?
 
Old 02-26-2008, 08:32 PM   #3
lorenww
LQ Newbie
 
Registered: Feb 2008
Location: Was rainy Oregon now sunny Florida
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks for the reply

Code:
if [ -s "z-heartbeat.php"]
then
  echo heartbeat file is not-zero size
else
  echo heartbeat file is zero size
fi
if [ -z "z-heartbeat.php"]
then
  echo heartbeat file is zero length
else
  echo heartbeat file is not-zero length
fi

if [ -s "z-chilli.php"]
then
  echo chilli file is not-zero size
else
  echo chilli file is zero size
fi
if [ -z "z-chilli.php"]
then
  echo chilli file is zero length
else
  echo chilli file is not-zero length
fi
output:
heartbeat file is zero size
heartbeat file is not-zero length
chilli file is zero size
chilli file is not-zero length

the heartbeat is 0B according to winscp
chilli is 588B

The routers do a auto update and all I need to know is if the downloaded file is 0 or more bytes. I appereciate the help because I am totally lost.
 
Old 02-26-2008, 08:58 PM   #4
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Sorry - I wasn't paying attention to what I was typing. The -s test should work because it checks whether files are non-zero in size. The -z test is checking the string in quotes, not the file of that name. That means the -z test will always return false unless the test is [ -z "" ].

Can you check the files please? When I run it here I get the correct results using the -s test:
Code:
$ ls -l z-*
-rw-r--r-- 1 steve steve 12 2008-02-27 12:45 z-chilli.php
-rw-r--r-- 1 steve steve  0 2008-02-27 12:45 z-heartbeat.php

$ cat test.sh
#!/bin/sh

if [ -s "z-heartbeat.php" ]
then
  echo heartbeat file is not-zero size
else
  echo heartbeat file is zero size
fi

if [ -s "z-chilli.php" ]
then
  echo chilli file is not-zero size
else
  echo chilli file is zero size
fi

$ ./test.sh
heartbeat file is zero size
chilli file is not-zero size
 
Old 02-26-2008, 09:08 PM   #5
lorenww
LQ Newbie
 
Registered: Feb 2008
Location: Was rainy Oregon now sunny Florida
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks, I will try this tomorrow cuz I got the family thing going, after hours now, awesome and really appreciate the help.
 
Old 02-27-2008, 08:49 AM   #6
lorenww
LQ Newbie
 
Registered: Feb 2008
Location: Was rainy Oregon now sunny Florida
Posts: 8

Original Poster
Rep: Reputation: 0
I have this so far, test.sh is in jffs
Code:
#!/bin/sh

ls

if [test -s "./jffs/z-heartbeat.php" ]
then
  echo heartbeat file is not-zero size
else
  echo heartbeat file is zero size
fi

if [test -s "./jffs/z-chilli.php" ]
then
  echo chilli file is not-zero size
else
  echo chilli file is zero size
fi
Outputs (ls shows the root directory and not the files in jffs as I expected so I changed the path in the if statemets)

bin
dev
etc
jffs
lib
mmc
mnt
opt
proc
sbin
sys
tmp
usr
var
www

./jffs/test.sh: ./jffs/test.sh: 8: [test: not found
heartbeat file is zero size
./jffs/test.sh: ./jffs/test.sh: 15: [test: not found
chilli file is zero size

for some reason it is not finding the file
 
Old 02-27-2008, 10:32 AM   #7
lorenww
LQ Newbie
 
Registered: Feb 2008
Location: Was rainy Oregon now sunny Florida
Posts: 8

Original Poster
Rep: Reputation: 0
Now I'm really confused.

Code:
cd ./tmp

file2="z-chilli.php"

if [ -e $file2 ]; then
	echo "chilli File exists"
else 
	echo "chilli File does not exists"
fi

if [ test -s $file2 ]
then
  echo chilli file is not-zero size
else
  echo chilli file is zero size
fi
Outputs

chilli File exists
heartbeat file is zero size
chilli file is zero size

I'm beginning to think that there is a problem in the dd-wrt firmware.

Any thoughts on this?
 
Old 02-27-2008, 03:39 PM   #8
lorenww
LQ Newbie
 
Registered: Feb 2008
Location: Was rainy Oregon now sunny Florida
Posts: 8

Original Poster
Rep: Reputation: 0
After trying searches and many things I finallydecided to go with grep.

Thanks and I am good to go
 
Old 02-27-2008, 05:29 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
A couple of points:
1. in your prev post, your code doesn't mention heartbeat file, but your output does... I don't think you are running the code you think.
2. Don't use 'test' and [[ ]] ( or [ ] ), it's either, not both. Effectively they both do the same thing.

Last edited by chrism01; 02-27-2008 at 06:44 PM.
 
Old 02-27-2008, 06:24 PM   #10
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
I'm glad you've got a working solution (grep), I was looking at the example you posted - it shouldn't run under bash without errors. You normally use [] or test, not both together. For example, these will work:
Code:
if [ -s "z-chilli.php" ]
if test -s "z-chilli.php"
This will not work (on my version of bash anyway):
Code:
if [ test -s "z-heartbeat.php" ]
So, for a script like this:
Code:
#!/bin/sh

cd /jffs
ls *php

if [ -s "z-heartbeat.php" ]
then
  echo heartbeat file is not-zero size
else
  echo heartbeat file is zero size
fi

if [ -s "z-chilli.php" ]
then
  echo chilli file is not-zero size
else
  echo chilli file is zero size
fi

if [ -e "z-heartbeat.php" ]
then
  echo heartbeat file exists
else
        echo heartbeat file does not exist
fi

if [ -e "z-chilli.php" ]
then
  echo chilli file exists
else
  echo chilli file does not exits
fi

if [ test -s "z-heartbeat.php" ]
then
  echo heartbeat file is not-zero size
else
  echo heartbeat file is zero size
fi
The output is:
Code:
$ ./test.sh
z-chilli.php  z-heartbeat.php
heartbeat file is zero size
chilli file is not-zero size
heartbeat file exists
chilli file exists
./test.sh: line 34: [: -s: binary operator expected
heartbeat file is zero size
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash noob question sir-lancealot Programming 8 01-03-2008 01:56 PM
getting filesize munna_dude Programming 2 04-10-2007 01:23 AM
BSD noob [adding bash] simplebob *BSD 5 04-03-2006 02:27 AM
noob bash script question babag Programming 9 04-30-2005 02:25 AM
stuck again (noob bash math question) babag Programming 6 04-25-2005 01:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration