LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 07-27-2010, 04:20 AM   #1
SilversleevesX
Member
 
Registered: May 2009
Posts: 181
Blog Entries: 9

Rep: Reputation: 15
Posting a better solution to a closed thread


Rather than get in hot (or even slightly-above-comfortable temperature) water for reviving a closed (ca. 2007) thread without moderator permission or acquaintance of the OP personally or by proxy, I'll post this alternate solution (approach?) along with one positive real-world example illustrating how mine is easier to write and is equally reliable, if not altogether better.

The thread is here. Their solution:
Code:
if [ "$myfilesize" eq "0" ];then echo "$file: the file size is zero"; else echo "$file: the file size is NOT zero"; fi
(and this hangs on the output of a stat -c %s command).
"Hangs" is probably a most appropriate word. The two outputs, using two different stat options, the same file, and three marginally-different command-line syntaxes I just tried gave me these:

First, using their approach, I got this:
Code:
tjsfa070929-094.jpg.error: the file size is NOT zero
Even though stat gave me this
Code:
>> stat -c %s $file
0
I thought, "Does stat really know what it's about?" So I just stat-ed the file for the complete page results on the file and saw "regular empty file". I asked myself, "Okay, what tag or flag represents that attribute?" Off to the man page:
Quote:
Originally Posted by man stat (on www.bash-linux.com)
%F File type
looked good, so I tried
Code:
>> stat -c %F $file 
regular empty file
A Match! I rolled that into the myfilesize solution, thusly
Code:
if [[ $myfilesize == *empty* ]];then echo "$file: the file size is zero"; else echo "$file: the file size is NOT zero"; fi
and ran it:
Code:
>> tjsfa070929-094.jpg.error: the file size is zero
From where I sit, getting the file type string instead of an integer, which can be equally (if not often more) accurate, makes for an easier time of setting the conditional and, maybe only in terms of a few ticks, a faster script from start to finish. I think my method is better, and proven so to the limits of my knowledge and ability here in this OP.

Comments?

BZT

Last edited by SilversleevesX; 07-27-2010 at 04:31 AM.
 
Old 07-27-2010, 06:27 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by SilversleevesX View Post
Comments?
The question was about file size, not file type, so post #2 is the answer: why require an external binary and parse a string where built-in integer comparison will do fine?..
 
  


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
posting new thread problem bikas LQ Suggestions & Feedback 26 04-28-2010 11:29 PM
glitch when posting to a thread pixellany LQ Suggestions & Feedback 7 11-23-2008 10:18 AM
how to subscribe to a thread without posting llewellyn LQ Suggestions & Feedback 3 03-03-2006 03:21 AM
Subscribe to thread without posting Charlie Spencer LQ Suggestions & Feedback 6 02-15-2006 06:53 PM
Why a closed thread? yadavankur LQ Suggestions & Feedback 4 12-15-2002 02:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:22 PM.

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