LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-11-2007, 02:27 PM   #1
jonfa
Member
 
Registered: Mar 2001
Location: FL
Posts: 257

Rep: Reputation: 30
shell script to determine if file size equals zero


Hi All,

I am attempting to write a shell script to determine if a particular file has a size of zero or not. here is my script:

#!/bin/sh

myfilesize=`exec ls -ld file.txt | awk '{ s+=$5 }'`
if [ "$myfilesize = 0" ];then
echo "the file size is zero"
else
echo "the file size is NOT zero"
fi
exit 0


Any ideas on why it doesn't work?
 
Old 12-11-2007, 02:35 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,386

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
just use the -s option to test. much much simpler...

if [ -s $file ]
then
echo yes
else
echo no
fi

don't reinvent the wheel. as for what's wrong... quite a few things to be honest, you're test doens't make sense as the entire thing is in quote marks, meaning that it'll always be "true" as if you just provide a string it is true if the string has a length, false if it's null.
 
Old 12-11-2007, 02:35 PM   #3
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
This will work:
Code:
#!/bin/bash

myfilesize=`stat -c %s file.txt`
if [ $myfilesize = 0 ];then
echo "the file size is zero"
else
echo "the file size is NOT zero"
fi
exit 0
 
Old 12-11-2007, 02:46 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Any ideas on why it doesn't work?
Yes.....the syntax is wrong.

ls -ld looks only for directories

What is this supposed to do?
Code:
awk '{ s+=$5 }'
 
Old 12-11-2007, 02:47 PM   #5
jonfa
Member
 
Registered: Mar 2001
Location: FL
Posts: 257

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by MicahCarrick View Post
This will work:
Code:
#!/bin/bash

myfilesize=`stat -c %s file.txt`
if [ $myfilesize = 0 ];then
echo "the file size is zero"
else
echo "the file size is NOT zero"
fi
exit 0
Thank you...that worked. I do have another question. I see you used the "stat" command. I have this on my linux box, but not on my solaris 10 box. Is there another command I could substitute?
 
Old 12-11-2007, 04:48 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Use acid_kewpie's answer; like he said, don't re-invent the wheel.
See : http://www.tldp.org/LDP/abs/html/fto.html
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
File Size Shell Script BlackLabel Programming 7 11-27-2007 07:48 PM
How can I determine the path of a shell script that I'm running right now ? basher400 Linux - General 7 10-25-2006 08:31 AM
shell script to read ps -e output and determine process double processes. dr_zayus69 Programming 1 09-21-2005 05:37 PM
How to determine file system block size? provo1234 Linux - Newbie 2 02-14-2005 09:30 AM
K3B error could not determine size of resulting image file mdm_green Fedora 1 12-11-2004 04:34 PM


All times are GMT -5. The time now is 06:56 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration