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?
 
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
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Mohamed MagdyCTOCommented:
About the result of the two commands, are you sure that they don't have spaces before or after?
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.