LinuxQuestions.org
Visit Jeremy's Blog.
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
 
LinkBack Search this Thread
Old 04-02-2008, 02:40 PM   #1
rimvydazas
Member
 
Registered: Dec 2006
Posts: 44

Rep: Reputation: 15
Lightbulb need help with bash script to compare file sizes


Hi guys,
I am not a programmer, but from time to time I need to "bash" scripts for my system. And I need some help with this one.
I have a program which continuously writes info to log files. I want to create a script which would check if the program is running by comparing current log file size with the log size recorded earlier (if sizes are equal, the program isn't running as it is supposed to). This is the script I came up with:

Code:
#!/bin/bash
#I try to store the size of the file into $FILENAME
FILENAME=/tmp/sz.txt
#this is the log file I'm checking the size of
LOGNM=/tmp/GJM-cdrcap.log
#simple IF to check if sz.txt exists
if test -r "$FILENAME"; then
echo "file exists"
else touch "$FILENAME"
echo "file created"
fi
#current size of the log file
NEWSZ=$(stat -c%s "$LOGNM")
#old log file size from sz.txt file
OLDSZ=($(grep -ne "[0-9]\+" $FILENAME))
if ["$NEWSZ" != "$OLDSZ"]; then
echo "old size *$OLDSZ*"
echo "new size *$NEWSZ*"
echo "program is fine. files are not equal"
else
echo "old size *$OLDSZ*"
echo "new size *$NEWSZ*"
echo "program stalled. files are equal"
fi
When I run it I get:
Code:
# ./re.sh 
file exists
./re.sh: line 16: [134127: command not found
old size *128698*
new size *134127*
program stalled. files are equal
Can't figure out what's going on in here. First of all, it returns an error. Also, files are not equal...
 
Old 04-02-2008, 03:24 PM   #2
Maligree
Member
 
Registered: Mar 2008
Distribution: Gentoo, CentOS, Fedora, Arch
Posts: 231
Blog Entries: 1

Rep: Reputation: 42
Code:
if ["$NEWSZ" != "$OLDSZ"]; then
get some spaces between those brackets and quotes:
Code:
if [ "$NEWSZ" != "$OLDSZ" ]; then
also.. doesn't
Code:
$(grep -ne "[0-9]\+" $FILENAME)
return something like 1:12345? I don't think that's what you'd like to compare the new filesize against, try without the -n flag.
 
Old 04-02-2008, 07:43 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.7, Solaris 10
Posts: 10,994

Rep: Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752
Use the correct operator to compare numbers ( -ne ); see http://www.tldp.org/LDP/abs/html/comparison-ops.html

You can also use
set -xv
at the top to debug your script
 
Old 04-03-2008, 07:18 AM   #4
rimvydazas
Member
 
Registered: Dec 2006
Posts: 44

Original Poster
Rep: Reputation: 15
Talking

Quote:
Originally Posted by Maligree View Post
also.. doesn't
Code:
$(grep -ne "[0-9]\+" $FILENAME)
return something like 1:12345? I don't think that's what you'd like to compare the new filesize against, try without the -n flag.
opssss.... sorry, my mistake. I actually had flag -o instead of -ne, but when I was creating the code in the thread I mistakenly changed grep flag instead of comparison operator. Anyway, brackets thing appeared to be an issue. Seems to work fine right now. Thanks for all who replied to my thread!
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to compare two files swatward Linux - Newbie 12 12-31-2009 10:23 AM
Bash script to compare numbers in a txt file leopard86 Programming 4 10-19-2007 08:39 PM
Bash script to compare dir contents Boffy Programming 2 08-02-2005 07:08 AM
Compare installed RPM versions using Bash script jimwelc Linux - Newbie 6 01-28-2005 11:40 AM
Comparing file sizes using a bash script. IanChristie Programming 5 12-19-2003 11:14 PM


All times are GMT -5. The time now is 07:27 PM.

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
Open Source Consulting | Domain Registration