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. |
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 |
Quote:
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 |
It is a homework but don't want that you think I did not tried to do this by myself .
|
So show us what you've tried and where you're stuck.
|
Quote:
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 |
#!/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 |
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 |
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) Code:
if [a>b] Code:
if [ "$a" -gt "$b" ] |
#!/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. :( |
you're missing 'then' keyword after 'elif'
|
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.
|
Code:
#!/bin/bash |
Presumably, you want, e.g.
Code:
a=$(stat -c%s $x) You should also consider using descriptive names for your variables.. |
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. |