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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-11-2007, 02:27 PM
|
#1
|
|
Member
Registered: Mar 2001
Location: FL
Posts: 257
Rep:
|
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?
|
|
|
|
12-11-2007, 02:35 PM
|
#2
|
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 42,711
|
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.
|
|
|
|
12-11-2007, 02:35 PM
|
#3
|
|
Member
Registered: Jul 2004
Distribution: Fedora
Posts: 241
Rep:
|
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
|
|
|
|
12-11-2007, 02:46 PM
|
#4
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
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?
|
|
|
|
12-11-2007, 02:47 PM
|
#5
|
|
Member
Registered: Mar 2001
Location: FL
Posts: 257
Original Poster
Rep:
|
Quote:
Originally Posted by MicahCarrick
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?
|
|
|
|
12-11-2007, 04:48 PM
|
#6
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,021
|
Use acid_kewpie's answer; like he said, don't re-invent the wheel.
See : http://www.tldp.org/LDP/abs/html/fto.html
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:45 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|