LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-14-2007, 05:30 PM   #1
hacker supreme
Member
 
Registered: Oct 2006
Location: As far away from my username as possible
Distribution: Gentoo
Posts: 259
Blog Entries: 1

Rep: Reputation: 31
BASH scripting. Is there a better way of achieving this...


I'm trying to get the size of a file into a variable in a bash script.
currently, I am using:
Quote:
FLSZE='ls -l $FILENAME | awk '{print $5}'
And while it works, and I am pleased with it, I'm wondering if there is a better way of doing this.

for example, if FILENAME=Xerrors (which is a 1.2KB file), then the output of this piece of script is: "1202", and this is correct.
But I feel that the method I am using is clumsy.
 
Old 06-14-2007, 05:48 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
This thread gives several methods:
http://www.linuxquestions.org/questi...d.php?t=410766
 
Old 06-14-2007, 06:30 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If this were something you would use frequently in a script, you could create a function in the start of the script to use.

ex:

Code:
#!/bin/bash
# define bsize function: prints size of file given in argument ($1)
function bsize()
> {
> stat -c'%s' $1
> }

# test the function
bsize ~/.xsession-errors
You could also define an alias.
 
Old 06-14-2007, 09:09 PM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by hacker supreme
I'm trying to get the size of a file into a variable in a bash script.
currently, I am using:
Code:
FLSZE='ls -l $FILENAME | awk '{print $5}'
And while it works, and I am pleased with it, I'm wondering if there is a better way of doing this.

for example, if FILENAME=Xerrors (which is a 1.2KB file), then the output of this piece of script is: "1202", and this is correct.
But I feel that the method I am using is clumsy.

Code:
FLSZE=$(( $( wc -c < FILE ) ))

FLSZE=$( stat -c%s FILE )  ## GNU stat

FLSZE=$( stat -f%z FILE )  ## FreeBSD stat

set -- $( ls -l FILE ); FLSZE=$5 ## Old Solaris, use $4 (IIRC)
 
Old 06-15-2007, 09:18 AM   #5
hacker supreme
Member
 
Registered: Oct 2006
Location: As far away from my username as possible
Distribution: Gentoo
Posts: 259

Original Poster
Blog Entries: 1

Rep: Reputation: 31
Thanks pixellany, cfaj and jschiwal.

It's only going to be used twice in the script at most, but I thought that my way was a bit long winded.
 
  


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
Need help with bash scripting Kleedrac Linux - Software 6 05-09-2006 02:55 PM
help with bash scripting spuzzzzzzz Programming 2 09-02-2004 11:42 PM
bash scripting qu drigz Programming 12 06-15-2004 02:09 AM
BASH Scripting ? eroica Programming 3 06-07-2004 07:51 PM
Bash scripting NSKL Linux - General 2 06-08-2002 12:10 PM

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

All times are GMT -5. The time now is 07:40 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