1

I have a script as below:

#!/bin/bash
df -k | tr -s " " "," | awk 'BEGIN {FS=","} {print $1,$5}'|sed 1d > file1.txt
while read partition percentUsed
do
if [ $percentUsed > 75 ]
then
echo Partition: ${partition} space is ${percentUsed}
else
echo Pration: $partition: OK!!
fi
done < file1.txt

The script is executing properly, however, it is creating a zero byte file by the name 75. Any ideas why this is happening?

$ sh diskUsed.sh
Partition: C:/Users/Public/Documents/CYGWIN/bin space is 75%
Partition: C:/Users/Public/Documents/CYGWIN/lib space is 75%
Partition: C:/Users/Public/Documents/CYGWIN space is 75%
Partition: C: space is 75%
Partition: H: space is 91%


$ ls -lrt
total 2
-rwxrwxrwx 1 diwvibh Domain Users 284 Nov 21 04:17 diskUsed.sh
-rw-r--r-- 1 diwvibh Domain Users 133 Nov 21 04:29 file1.txt
-rw-r--r-- 1 diwvibh Domain Users   0 Nov 21 04:29 75
  • Why do you give the file a shebang line with bash and than run the file through sh (which could be some other file) ? – Anthon Nov 21 '14 at 10:49
2
 if [ $percentUsed > 75 ]

You should use the -gt operator for numeric comparisons; what you are doing is saving the (empty) output of the command "[ $percentUsed ]" into a file named "75".

The script is executing properly, however, it is creating a zero byte file by the name 75. Any ideas why this is happening?

It is not executing properly. The "[ $percentUsed ]" command returns true if the string is non-empty, which it always is for this script, so it will always take the first branch of the if statement.

Because $percentUsed is not an integer (it has a trailing '%') could use ${percentUsed%[%]} to remove the trailing '%' from the variable, or add zero inside awk, as @val0x00ff suggests.

General comments for bash scripts:

  1. Favor [[ expr ]] rather than [ expr ] as it is more powerful and handles empty strings better.
  2. Use double quotes around strings unless you have reason not to do so, eg: getting rid of newlines.
  3. Add braces: ${var} rather than $var -- it can often eliminate confusion when reading and allows variables to abut text, like: echo "Remaining: ${kb}KB"
  4. Generally, avoid creating temporary files when possible. Use pipes between stages instead.
  5. If you're using a tool (like awk) then learn how to use its power, it will make your code more concise and make it easier to read others' code.
  6. Indent your code properly (which perhaps you did in reality but not in the post).
  7. If you plan to type exclamation marks ("!"), the use set +H so they don't screw up your interactive bash by inserting history.

Here is a working version of your script:

#!/bin/bash +H
df -k | awk '(FNR>1){print $1,$5+0}' | while read partition percentUsed
do
    if [[ $percentUsed -gt 75 ]]
    then
        echo "Partition: ${partition} space is ${percentUsed}% used"
    else
        echo "Partition: ${partition}: OK!!"
    fi
done

Nit pick: your "echo" line would output "Partition /home space is 80" if it was 80% used, many would read "space" as "free space" which would be 20%.

2

In line 5 you write the contents of $percentUsed to file "75". Instead, try if [ ``echo $percentUsed | sed 's/%//'`` -gt 75 ] which should do what you desire. At least with bash 4.2.25 on Linux it works. Note: Please only use one backtick instead of two shown here - that's because the Stackexchange web platform interprets those backticks.

  • I tried both, even with "75" it creates a file. when -gt is used, i get the below error: diskUsed.sh: line 6: [: 75%: integer expression expected Pration: C:/Users/Public/Documents/CYGWIN/bin: OK!! diskUsed.sh: line 6: [: 75%: integer expression expected Pration: C:/Users/Public/Documents/CYGWIN/lib: OK!! diskUsed.sh: line 6: [: 75%: integer expression expected Pration: C:/Users/Public/Documents/CYGWIN: OK!! diskUsed.sh: line 6: [: 75%: integer expression expected Pration: C:: OK!! diskUsed.sh: line 6: [: 91%: integer expression expected Pration: H:: OK!! – user80040 Nov 21 '14 at 9:47
  • @user80040 Does writing "if (( percent > 75 ))" as suggested by val0x00ff work accordingly? – Michael Kremser Nov 21 '14 at 10:08
  • @MichaelKremser I don't think that will work the way how he dumped the output to file1.txt. This file will include % in the second field. while read ... percentUsed will have % sign in there and will never be compared. – Valentin Bajrami Nov 21 '14 at 10:26
  • What gets written to the file is not the content of the variable, it's the output of the command [ $percentUsed ], which is empty. – Gilles Nov 21 '14 at 23:21
  • 1
    @val0x00ff: Thanks! I have overseen that. I improved my answer based on your valuable feedback. Also, I tried it out on my machine and here it works. – Michael Kremser Nov 22 '14 at 12:48
0

This is one efficient way to calculate the disk space. In this example, I am using the /home dir.

#!/bin/bash

 { read -r; read -rd '' -a disk_usage; } < <(LC_ALL=C df -Pk "/home"; printf \\0)
    percent=$(echo "${disk_usage[4]}" | awk '{print $0+0}')
    if (( percent > 75 )); then echo "Disk is critical"; else echo "Disk space is OK"; fi

As a side note, The command you have there which is

df -k | tr -s " " "," | awk 'BEGIN {FS=","} {print $1,$5}'|sed 1d

Is very inefficient since it forks a lot of processes like tr, awk, sed. You can do this in one go. Remove the +0 at the end if you want to see the % sign.

df -k | awk 'NR>1{print $1,$(NF-1)+0}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.