3

I am new to shell scripting and trying to make a few small scripts. I got stuck when i tried to write if condition. In the code below i am trying to get the $5 value from df and trying to use it in if condition. However the code doesn't work.

 #!/bin/sh

temp = $(df -h | awk '$NF=="/"{$5}')
if [ $temp > 60 ] ; then
 df -h | awk '$NF=="/" {printf("%s\n"),$5}'
 (date -d "today" +"Date:%Y.%m.%d"" Hour: %H:%M")
fi

#end

So i've figured out something and changed my code into this:

temp=$(df -h | awk '$NF=="/"{$5}')
if [ "$((temp))" -gt 0 ] ; then
 df -h | awk '$NF=="/" {printf("%s\n"),$5}'
 (date -d "today" +"Date:%Y.%m.%d"" Hour: %H:%M")
fi

#end

And now , i'm trying to get the integer value of the $5 variable. It returns a percentage and i want to compare this percentage with %60. How can i do that ?

5
  • Do you simply want to retrieve fifth column from df -h output? Commented Jun 12, 2018 at 18:33
  • Yes and also i want if statement to work for both of the df -h and (date ... ) part. Commented Jun 12, 2018 at 18:48
  • 2
    shellcheck.net tells you about most problems in your snippet. Commented Jun 12, 2018 at 18:54
  • Thank you that helped a lot. Commented Jun 12, 2018 at 19:33
  • In your first line of the updated code, you're not telling awk to actually print the 5th field. Commented Jun 12, 2018 at 19:57

2 Answers 2

2

Let's see what shellcheck.net has to tell us:

Line 1:
  #!/bin/sh
^-- SC1114: Remove leading spaces before the shebang.

Line 3:
temp = $(df -h | awk '$NF=="/"{$5}')
     ^-- SC1068: Don't put spaces around the = in assignments.

Line 4:
if [ $temp > 0 ] ; then
     ^-- SC2086: Double quote to prevent globbing and word splitting.
           ^-- SC2071: > is for string comparisons. Use -gt instead.
           ^-- SC2039: In POSIX sh, lexicographical > is undefined.

Um, ok, after a little fixing:

#!/bin/sh
temp=$(df -h | awk '$NF=="/"{$5}')
if [ "$temp" -gt 0 ] ; then  
   df -h | awk '$NF=="/" {printf("%s\n"),$5}'
   (date -d "today" +"Date:%Y.%m.%d"" Hour: %H:%M")
fi

The [ ... ] command is the same as test command. Test does not have < comparison for numbers. It has -gt (greater then). See man test.
This will run now, but definitely not do what you want. You want the fifth column of df output, ie. the use percents. Why do you need -h/human readable output? We dont need that. Which row of df output do you want? I guess you don't want the header, ie. the first row: Filesystem 1K-blocks Used Available Use% Mounted on. Let's filter the columns with the disc name, I choose /dev/sda2. We can filter the row that has the first word equal to /dev/sda2 with grep "^/dev/sda2 ". The we need to get the value on the fifth column with awk '{print $5}'. We need to get rid of the '%' sign too, otherwise shell will not interpret the value as a number, with sed 's/%//' or better with tr -d '%'. Specifying date -d"today" is the same as just date. Enclosing a command in (...) runs it in subshell, we don't need that.

#!/bin/sh
temp=$(df | grep "^/dev/sda2 " | awk '{print $5}' | tr -d '%')
if [ "$temp" -gt 0 ]; then  
   echo "${temp}%"
   date +"Date:%Y.%m.%d Hour: %H:%M"
fi

This is a simple, that if use percentage on disc /dev/sda2 is higher then 0, then it will print the use percentage and print current date and time in a custom format.

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you're using GNU tools, you can narrow the df output to just what you need:

pct=$( df --output=pcent / | grep -o '[[:digit:]]\+' )
if [[ $pct -gt 60 ]]; then ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.