0

I am new to bash scripting, but I am working on a Unix AIX server and hoping to pull capacities for multiple environments on a single server; then run a conditional statement to compare those current capacities against my threshold variable, and then send an email notification if the capacity for specific environments is exceeded.

The problem I am having is with the conditional statement, and comparing my threshold against the output which is a column of capacity values. Any input would be greatly appreciated. Ultimately, I'd like to get this to the point where I can have the email specify which environments are exceeding capacity within the email. Then I can setup a cron job on multiple servers to run this script at specific times to automate our disk space management.

Any constructive feed back is welcomed and appreciated, I'm here to learn. Thanks in advance!

#!/bin/ksh

threshold=10

summary=$(df -Pg|grep -E 'opt|tmp|admin'|awk 'BEGIN {printf ("%- 40s %- 40s\n", "Partition", "Capacity") }{printf ("%- 40s %- 40s\n", $1, $5 )}')
date=$(date)


df -Pg|grep -E 'opt|tmp|admin'|awk '{print $5}'|sed 's/%//g'|while read -r output; do

if [ "$output" -ge "$threshold" ]; then

echo "$summary"|mailx -s "$date IMPORTANT:Disk Space Alert" JonMonJovi@abc.org

fi

done
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
Jonmonjovi
  • 23
  • 1
  • 7
  • http://shellcheck.net/ would catch this for you. – Charles Duffy May 10 '17 at 04:17
  • ...btw, while ksh is capable of floating-point comparisons, I'd tend to lean towards doing your math in awk -- that way your code isn't shell-specific, as most other POSIX-family shells can only do integer math. (In addition of being able to do math, `awk` can also do the work you're using `grep` and `cut` for here -- it's a full-fledged programming language on its own). – Charles Duffy May 10 '17 at 04:19
  • Thank you, I made the suggested changes. However, the conditional statement isn't working. I am thinking that the issue is that its trying to compare the threshold against a column of values which is the result of the capacity variable. Also, I agree that awk is a very powerful language and I will work on utilizing that to remove the grep and cut statements. – Jonmonjovi May 10 '17 at 16:08
  • `>=` isn't a correct numeric comparison either. See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html for the list of operators guaranteed to be supported. – Charles Duffy May 10 '17 at 16:09
  • Beyond that, I'd suggest running your script with `ksh -x yourscript` to log the actual commands being run, and including an appropriate subset of that in the question. (Use the `{}` button, with content selected, to code-format question content). – Charles Duffy May 10 '17 at 16:10
  • (...btw, the reason shellcheck didn't tell you to quote `$threshold` is that it's being assigned from a constant value that doesn't contain any characters likely to be string-split or globbed -- but it's still good practice to do so regardless, so if it's assigned from somewhere else in the future you don't get bugs). – Charles Duffy May 10 '17 at 16:11
  • As an aside -- which version of ksh do you have? (What I have handy for testing is ksh93u+; if you're running ksh88, I'm not as sure about floating-point support). – Charles Duffy May 10 '17 at 16:13
  • ...I'm also really unclear why you'd run `df` a second time inside the loop, instead of just once on the outside. If the issue is that you're unclear on how to split that line of output that `while read` is parsing into more than one variable, see [BashFAQ #1](http://mywiki.wooledge.org/BashFAQ/001). – Charles Duffy May 10 '17 at 16:14
  • btw, please preview your edits before saving them. Remember how after I changed it earlier your code was actually formatted as code, with indentation and syntax highlighting? It's back to being an unreadable mess again. Use the `{}` button to add four-space indents to the selected text, thus formatting it a multi-line code block. – Charles Duffy May 10 '17 at 16:41
  • Thanks for all your assistance. It helped a lot, I am getting closer. Will keep working on it. I am unable to determine my ksh version - echo $KSH_VERSION; echo "${.sh.version}" is not returning anything. – Jonmonjovi May 10 '17 at 16:50

0 Answers0