ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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......
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.
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.
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
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.
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
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.
#!/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
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.