LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   compare size script (https://www.linuxquestions.org/questions/programming-9/compare-size-script-904746/)

trintukaz 09-24-2011 02:41 AM

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.

corp769 09-24-2011 02:46 AM

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

EricTRA 09-24-2011 02:48 AM

Quote:

Originally Posted by trintukaz (Post 4480800)
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

trintukaz 09-24-2011 02:52 AM

It is a homework but don't want that you think I did not tried to do this by myself .

Nylex 09-24-2011 02:53 AM

So show us what you've tried and where you're stuck.

EricTRA 09-24-2011 02:54 AM

Quote:

Originally Posted by trintukaz (Post 4480811)
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

trintukaz 09-24-2011 03:05 AM

#!/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

EricTRA 09-24-2011 03:22 AM

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

millgates 09-24-2011 03:24 AM

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

trintukaz 09-24-2011 03:52 AM

#!/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. :(

millgates 09-24-2011 04:02 AM

you're missing 'then' keyword after 'elif'

Nylex 09-24-2011 04:04 AM

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.

trintukaz 09-24-2011 04:11 AM

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?

Nylex 09-24-2011 04:14 AM

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..

millgates 09-24-2011 04:15 AM

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?


All times are GMT -5. The time now is 08:17 AM.