Linux Shell Script compare FileSize with available Space

Hi experts,

I think is is simple, bu I don`t have much experience with Shell Scripts.
What I want to do:

Read the filesize of a file which is already on the System,
Read the available disk space remaining on the System.
Multiplicate remaining space with 0,95

Compare the values:
If filesize > remaning space : Exit 1 Stop Skript
else: go on.

I have following:

#### Funktion füor-Handling #####
errorhandling() {
if [ "$?" != "0" ]; then
    echo "[Error] bei Schritt $STEP " 1>&2
    echo "[Error] bei Schritt $STEP See $LOGFILE for more information " | mail -s "Fehler Backup $SYSTEMNAME" $RECIPIENT
    exit 1
fi
}
#### Abfrage von Speichergroeà ######
BACKUPGROESSE= ls -l /daten/backup/daily/backup.tar.gz | awk '{print $5 }'
FREESPACEFULL= df / | awk '{print $4}' | tail -1
#BACKUPGROESSE=3
#FREESPACEFULL=200
#echo "BACKUPGROESSE=$BACKUPGROESSE"
#echo "FREESPACEFULL=$FREESPACEFULL"
echo $BACKUPGROESSE
echo $FREESPACEFULL
if [ $BACKUPGROESSE -gt $FREESPACEFULL  ]
then
 echo "zu wenig Speicherplatz"
 exit 1
else
 echo "alles OK"
fi
errorhandling

echo "Skriptende"

Open in new window


Output is following, although the first number is smaller that the second.

976343
27925384


zu wenig Speicherplatz

Open in new window


What am I doing wrong?
SystemadministrationAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mohamed MagdyCTOCommented:
-gt means greater than, I guess you are mixing them.

-lt means less than

Then in your script, you tell it if file size greater than disk space then exit.

Where is the problem exactly?
0
SystemadministrationAuthor Commented:
As you cann see the fist number is lower than the second.
In the script I`m asking:

if 1st greater than 2nd, exit1

Bit the output shows, that the 1st number is lower than the second. And as you can see, it is aborting with exit1
0
Mohamed MagdyCTOCommented:
About the result of the two commands, are you sure that they don't have spaces before or after?
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

woolmilkporcCommented:
"df" shows 512-byte ("POSIXLY_CORRECT") or 1K blocks (see the header), while "ls" shows bytes.

So either use "df --block-size=1" (GNU Linux only) or multiply FREESPACEFULL with 512 or 1024 and you should be fine ("alles sollte funktionieren" in our language).

The easiest way to perform arithmetic operations under bash/ksh is using (( )) :

((FREESPACEFULL*=1024))

will do the trick, or

FREESPACEFULL=$(df / | awk '!/^Filesystem/ {printf "%d\n", $4*1024}')

It might be (at least with older bashes) that you must use double square brackets around the if condition instead of single ones:

if [[ $BACKUPGROESSE -gt $FREESPACEFULL ]]

Note: I always use $(  ) instead of  ` ` , it's just better readable.
Strange thing: the backticks ` ` don't show up in my browser, or did you miss them?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SystemadministrationAuthor Commented:
I could find a solution in my own:

FILESIZE=$(du -k "$FILENAME" | cut -f1)
FREESPACEFULL=$(df / | awk '{print $4}' | tail -1)

if [ "$FILESIZE" -gt "$FREESPACEFULL"  ]
then
 echo "less Space"
    exit 1
else
 echo "OK"
fi

Open in new window

0
SystemadministrationAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for insideAC's comment #a40737512

for the following reason:

New investigation
0
Mohamed MagdyCTOCommented:
woolmilkporc comment is right and is the solution to this question, author got another code but the same concept as woolmilkporc said.

author in the last comment wrote du -k (to get size in kb) and in his question he wrote (ls), so woolmilkporc told him that (ls) shows bytes.

author came again in last comment and changed (ls) at all and replaced it with du -k to get size in kb as woolmilkporc said.

so woolmilkporc deserves the points for this question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.