LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-24-2011, 02:41 AM   #1
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Rep: Reputation: Disabled
compare size script


Hi, guys! Can somebody help me? I need a bash script, wich shows wich file is larger by size. E.g. enter first file name
name
enter second file name
name
file "name" is bigger.
Something like that.

Last edited by trintukaz; 09-24-2011 at 02:48 AM.
 
Old 09-24-2011, 02:46 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Hello,

Can I ask what the script will be used for? I ask because you honestly don't need an actual script to calculate a file size difference between only two files. You can do this multiple ways. Now lastly, before I go answering your question, is this homework? I got curious and searched on the internet, only to find this question all over many forums......

Cheers,

Josh
 
1 members found this post helpful.
Old 09-24-2011, 02:48 AM   #3
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Quote:
Originally Posted by trintukaz View Post
Hi, guys! Can somebody help me? I need script, wich shows wich file is larger by size. E.g. enter first file name
name
enter second file name
name
file "name" is bigger.
Something like that.
Hello and Welcome to LinuxQuestions,

Now, how would you learn something when we offer you a ready made solution? LQ is all about helping other Linux users solve their problems and answer their questions but we're not here to do your work for you, be it homework or not. You'll learn a lot more if you search and try. You'll find LQ users at their best if you show us what you've gotten so far and where it's failing.

Looking forward to your participation in the forums. Have fun with Linux.

EDIT: Here are some great starting points for Bash: Bash Guide for Beginners and Advanced Bash Scripting Guide.

Kind regards,

Eric

Last edited by EricTRA; 09-24-2011 at 02:53 AM. Reason: Added some links to guides.
 
1 members found this post helpful.
Old 09-24-2011, 02:52 AM   #4
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
It is a homework but don't want that you think I did not tried to do this by myself .
 
Old 09-24-2011, 02:53 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
So show us what you've tried and where you're stuck.
 
Old 09-24-2011, 02:54 AM   #6
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Quote:
Originally Posted by trintukaz View Post
It is a homework but don't want that you think I did not tried to do this by myself .
Hi,

Thanks for being honest about it being homework. You say you tried so what did you try? Post your 'trials' and we'll be able to tell you where you're missing out and point you in the right direction.

Kind regards,

Eric
 
Old 09-24-2011, 03:05 AM   #7
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
#!/bin/bash

echo File name number 1
read x
echo File name number 2
read y
stat -c%s $x
read a
stat -c%s $y
read b
if [a>b]
then
echo "File $a is larger"
elif [a<b]
echo "File $b is bigger"
else
echo "both file are equal"
fi


MY TRIAL

Last edited by trintukaz; 09-24-2011 at 03:22 AM.
 
Old 09-24-2011, 03:22 AM   #8
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hi,

First thing that pops out is that you're using the stat command to get the size in bytes of a file but then you ask for input to put into variables a and b which later on you check if one is bigger then the other.

Hint: Where do you store the result of your stat command (the actual size of the file)? Look into storing command output in a variable.

Also, put your code between code tags please for readability.

Kind regards,

Eric
 
Old 09-24-2011, 03:24 AM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
1) what are 'a' and 'b' variables for?

if you want to assign the result of a command to them do following:
Code:
a=$(stat -c%s $x)
2) what are these lines supposed to do?
Code:
if [a>b]
elif [a<b]
if you want to want to compare values you should use '$' character like this:
Code:
if [ "$a" -gt "$b" ]
you should also use doublequotes around variables for the case the filenames contain spaces
 
Old 09-24-2011, 03:52 AM   #10
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
#!/bin/bash

echo File name number 1
read "x"
echo File name number 2
read "y"
x=$(stat -c%s $x)
y=$(stat -c%s $y)
if [ "$x" -gt "$y" ]
then
echo "File $a is larger"
elif [ "$y" -gt "$x" ]
echo "File $b is bigger"
else
echo "both files are equal"
fi

Now it looks like. Still not working.
 
Old 09-24-2011, 04:02 AM   #11
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
you're missing 'then' keyword after 'elif'
 
Old 09-24-2011, 04:04 AM   #12
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Again, please use [code] tags for your code. If you click the "Go Advanced" button at the bottom of the "Quick Reply" box, there's a button with a '#' on it that will put [code] tags in your post.
 
Old 09-24-2011, 04:11 AM   #13
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Code:
#!/bin/bash

echo File name number 1
read "x"
echo File name number 2
read "y"
x=$(stat -c%s $x)
read a
y=$(stat -c%s $y)
read b
if [ "$a" -gt "$b" ]
then
echo "File $x is larger"
elif  [ "$b" -gt "$a" ]
then
echo "File $y is bigger"
else
echo "both files are equal"
fi
Still dont work where is the problem?
 
Old 09-24-2011, 04:14 AM   #14
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Presumably, you want, e.g.

Code:
a=$(stat -c%s $x)
Otherwise, the script is waiting for you to enter inputs for a (and later b).

You should also consider using descriptive names for your variables..
 
Old 09-24-2011, 04:15 AM   #15
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
congratulations for using the code tags.
what are you reading and into what variables?
where do you store the file sizes?
and finally, what are you comparing?
why do you read a and b again?
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
script to compare users in files s_linux Programming 7 04-05-2011 10:34 AM
Please help on making a compare script Magil Programming 7 12-14-2009 05:13 AM
Script to compare file size nazs Programming 6 05-24-2006 10:10 AM
I'm looking for a perlscript to compare size of 2 files cccc Programming 5 02-28-2004 04:45 PM
Help with a Directory Compare Script bullfrog Linux - General 1 02-04-2003 08:05 AM

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

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