LinuxQuestions.org
Help answer threads with 0 replies.
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 09-03-2009, 08:46 AM   #1
the_fornicator
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Rep: Reputation: 0
Question Basic bash script question re: file size or # of lines in a file


Hi there,

Sorry, sorta new to scripting. I tried doing a bunch of searches, but it's more of a syntax error. Hoping someone can help.

I'm trying to find either the number of lines in a file or the size of a file. Assuming both will yield the same result if it's empty or not -which is all I care about (i.e. if the file is empty)

My script looks like this:

#!/bin/bash


somefile="/home/dir/file.txt"

# do stuff to the file

filesize=$(awk 'END {print NR}' $somefile)
echo "The file size is $filesize"

---
But when I try to run the script, I'm getting a syntax error saying that the '(' is unexpected in the "filesize=$(awk 'END {print NR}' $somefile)" line.

Thoughts? Suggestion?

Thanks in advance!

Last edited by the_fornicator; 09-03-2009 at 08:47 AM.
 
Old 09-03-2009, 09:11 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your script worked for me. Are you sure your shell is a genuine bash of recent vintage? If you replace the '$( .... )' notation with backticks, does it work?
--- rod.
 
Old 09-03-2009, 09:14 AM   #3
the_fornicator
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Original Poster
Rep: Reputation: 0
p.s I searched the forums some more and tried this:

#!/bin/bash

somefile="dir/file.txt"

filesize=$(stat -c%s $somefile)
echo "filesize is $filesize"

When executed, I get a, 'filesize=$' not expected.


Wondering what I'm doing wrong. Thanks in advance again.
 
Old 09-03-2009, 09:21 AM   #4
the_fornicator
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
Your script worked for me. Are you sure your shell is a genuine bash of recent vintage? If you replace the '$( .... )' notation with backticks, does it work?
--- rod.

Do you mean like:
filesize=$(awk `END {print NR}` $somefile)

(I replaced the original ' with ` (backticks) and I'm still getting a 'filesize=$' not expected)



To answer your other question, I honestly couldn't tell you. I'm trying to throw something together on a work server so I don't know anything about it. Might be an environment variable... or something, but this stuff worked the other day.

I can run it in the command prompt no problems, if that's of any consolation.
 
Old 09-03-2009, 09:31 AM   #5
the_fornicator
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
Your script worked for me. Are you sure your shell is a genuine bash of recent vintage? If you replace the '$( .... )' notation with backticks, does it work?
--- rod.
Wait.. I'm a tool.

I just understood what you were saying. i.e. I did this:

filesize=`awk 'END {print NR}' $somefile`

and that worked.

Care to educate a person as to why this is?

Tbanks for your help, btw.
 
Old 09-03-2009, 09:32 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Sorry... posted by mistake.

Last edited by colucix; 09-03-2009 at 09:33 AM.
 
Old 09-03-2009, 09:41 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
theNbomr is talking about the old way to embed commands using ` backticks instead of $(), not changing the single quotes, which in this case protect the expression that's being passed to awk.
Code:
filesize=`awk 'END {print NR}' $somefile`
This shouldn't be your problem though, unless your version of bash is incredibly old (run 'bash --version' to find out).

You should probably also use double-quotes around the string when setting variables, to ensure that the output is seen as a single string and to escape any bad substitutions. Not that that's necessarily the problem here, however.
Code:
filesize="$(awk 'END {print NR}' $somefile)"
But if all you want to do is determine if the file is empty, there are already tests available for that.
Code:
if [[ -f "$1" && ! -s "$1" ]]; then
        echo "Regular file $1 is an empty file."
elif [[ -f "$1" && -s "$1" ]]; then
        echo "Regular file $1 is not empty."
else
        echo "$1 is not a regular file."
fi
Edit: I see you figured the backquote thing out on your own.

I don't know why the original code isn't working for you. It works for me with no problem. Is there anything unusual about your setup that we should know about?

Last edited by David the H.; 09-03-2009 at 09:51 AM.
 
  


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
File size using BASH script cb951303 Programming 16 11-07-2012 07:45 AM
Need a bash shell script which will delete lines from file scjohnie Linux - Newbie 1 09-13-2008 08:51 PM
bash script to remove the blank lines in the file naveensankineni Programming 7 03-25-2008 08:34 PM
Bash script to read file and alter lines depending on create date ChristianHein Linux - General 13 08-04-2007 05:39 AM
Can't get lines of a file with a Bash script.. barisdemiray Programming 2 08-11-2004 12:42 PM

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

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