Filesystem alert shell script not working!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filesystem alert shell script not working!!
# 1  
Old 07-02-2019
Filesystem alert shell script not working!!

Hi All,

My below shell script is not capturing %used value in the filesystem alert in the subject of the mail alert:

Code:
Code:
#!/bin/bash

export DBALIST="abc@xyz.com"

df -k /oradata/xyz/archive > dfk.result
archive_capacity=`awk -F" " '{ print $5 }' dfk.result|grep -i %| cut -c 1-4`
if [[ $archive_capacity > 60% ]]
then
mailx -s "Filesystem /oradata is ${archive_capacity} filled" $DBALIST < dfk.result
fi

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags for code, data and terminal output. Thank you.

Last edited by bakunin; 07-02-2019 at 06:44 AM..
# 2  
Old 07-02-2019
Quote:
Originally Posted by harveyclayton
Hi All,

My below shell script is not capturing %used value in the filesystem alert in the subject of the mail alert:

Code:
Code:
#!/bin/bash

export DBALIST="abc@xyz.com"

df -k /oradata/xyz/archive > dfk.result
archive_capacity=`awk -F" " '{ print $5 }' dfk.result|grep -i %| cut -c 1-4`
if [[ $archive_capacity > 60% ]]
then
mailx -s "Filesystem /oradata is ${archive_capacity} filled" $DBALIST < dfk.result
fi

First, i do not understand what you need the temporary file for and you SHOULD AVOID backticks:

Code:
df -k /oradata/xyz/archive > dfk.result
archive_capacity=`awk -F" " '{ print $5 }' dfk.result|grep -i %| cut -c 1-4`

could be replaced by

Code:
archive_capacity=$(df -k /oradata/xyz/archive|awk -F" " '{ print $5 }' |grep -i %| cut -c 1-4)

But, honestly, whenever i see a awk|grep|cut i know there is something deeply amiss. All that grepping and cutting can be done inside awk, but i do not now what your exact df-output is, so it is hard to suggest what the awk-line should exactly look like.

Furthermore, this:

Code:
if [[ $archive_capacity > 60% ]]

cannot work: the ">" operator of the test-utility is not understood by all versions of test but even the ones that do only compare integers and "60%" is not an integer - it is a string. You should get rid of the "%" sign when you create the $archive_capacity content and then do like:

Code:
if [[ $archive_capacity -gt 60 ]]

first, this is an integer operation and second "-gt" (greater than) is an operator every test will understand.

I hope this helps.

bakunin
# 3  
Old 07-02-2019
@bakunin, the [[ ]] compound is not the test command. (But the [ command (that needs ] as its last argument) is test.)
If the shell understands the [[ ]] then it should also understand its > operator: a string comparison.
And here is the problem: [[ 70% > 60% ]] is true but [[ 100% > 60% ]] is false.
While the -gt operator does a number comparison.

A quick-and-dirty trick is to foster a cast to a number:
Code:
df -kP /oradata/xyz/archive | awk 'NR>1 { print $5+0 }'

or
Code:
df -kP /oradata/xyz/archive | awk 'NR>1 { print int($5) }'

or
Code:
df -kP /oradata/xyz/archive | awk 'NR>1 { printf "%d\n", $5 }'

But it might not work with all awk versions.
Very safe is
Code:
df -kP /oradata/xyz/archive | awk 'NR>1 { gsub(/%/,""); print $5 }'

# 4  
Old 07-02-2019
You need to also remove the header line, as shown by MadeInGermany. How about
Code:
cap=( $(df /oradata/xyz/archive | tee dfk.result | tail -n+2) )
if [ ${cap[4]%\%} -gt 60 ]
  then mail ... dfk.result
  fi

# 5  
Old 07-04-2019
Quote:
Originally Posted by MadeInGermany
@bakunin, the [[ ]] compound is not the test command. (But the [ command (that needs ] as its last argument) is test.)
This is right and i am well aware of the difference between [[...]] (built-in) and [...] (/bin/test). Still, the built-in was made to work like (or, at the very least, "similar to") the original test-command and in this specific case there should be no difference. I should have made that thought process clearer, though.

Quote:
Originally Posted by MadeInGermany
And here is the problem: [[ 70% > 60% ]] is true but [[ 100% > 60% ]] is false.
While the -gt operator does a number comparison.
Exactly. Which is why i suggested to get rid of the percent-symbol so that the shell deals with integers instead of strings.

I hope this helps.

bakunin
# 6  
Old 07-08-2019
Could I chip in that I prefer to add the -P flag to df in case there are long device or filesystem names that cause the output to split the output on to separate lines.



Just a thought,
Robin
# 7  
Old 07-08-2019
Good point; the -P works around a long-term bug in GNU df.
Meanwhile, in recent distros, it should have been fixed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to send mail alert

Hi I have below shell script to send mail alert , however I want to add more functionality in this script and that is , script should only check that file between 9 am to 5pm , and if there is no activity in this time 9 am to 5 pm for 2hours then it should give me mail alert, please help... (2 Replies)
Discussion started by: scazed
2 Replies

2. Shell Programming and Scripting

Shell script to send mail alert

HI Guys, I am writing one shell script to send the mail alert to some email id's if the file not modified in last 10 mins but its not working, I believe MTIME is null string is wrong . can you please assist me on this. script :- filename="abc.txt" echo "Filename is $filename"... (1 Reply)
Discussion started by: abhigrkist
1 Replies

3. Shell Programming and Scripting

Shell script for alert

Hi Experts, Im new in shell script , please help to achieve the below requirement, We have some replication setup in unix server, in that if there is any exception or error occurs immediately the rep_exception.log will have the exception detail, this log will be updated if any error occurs no... (8 Replies)
Discussion started by: pandiyan
8 Replies

4. Shell Programming and Scripting

shell script to alert if a file has been modified

Hi , I want a script who will send alert the moment someone edit any file in a directory in LINUX. Can some one throw some light on this please.!! (4 Replies)
Discussion started by: d8011
4 Replies

5. Shell Programming and Scripting

Shell script to find filesystem capacity on 50 servers

Hi all, I am new to Unix and I want to write a shell script in a jumpbox for finding the filesystem capacity on 50 unix servers ( by ssh ) and then email the result in HTML format with server name and capacity % to a specific outlook distribution list. any suggestion would be of great help. (17 Replies)
Discussion started by: amitbisht9
17 Replies

6. Shell Programming and Scripting

Unix Shell Script to automate email alert

Hi all, I have a task on my plate which is of high priority. I need an automated email alert that checks FTP notices subdirectory on a daily basis and forwards any word files to a group of people. This word files gets created whenever there is an issue with FTP connectivity. Please help...... (1 Reply)
Discussion started by: stunnerz_84
1 Replies

7. Shell Programming and Scripting

simple script to alert if internet not working?

Hi, I am constantly automaticaly downloading a few things on the internet but since my internet connection is unstable, it sometimes wont work. Thing is the internet will appear to be connected, but no website can be accessed and no program can successfully connect to any location. I can fix... (4 Replies)
Discussion started by: fuzzylogic25
4 Replies

8. Shell Programming and Scripting

Filesystem alert shell script not working!!

Hi All, My below shell script is not capturing %used value in the filesystem alert in the subject of the mail alert: #!/bin/bash export DBALIST="abc@xyz.com" df -k /oradata/xyz/archive > dfk.result archive_capacity=`awk -F" " '{ print $5 }' dfk.result|grep -i %| cut -c 1-4` if ] then... (5 Replies)
Discussion started by: a1_win
5 Replies

9. Shell Programming and Scripting

shell script to mount filesystem

Hi, Gurus: I need your help to finish a script that will mount two file systems automatically when saver is reboot or start. I am working on a new Sun Sparc machine with Solaris 9 on it. This box got two disk. disk one has been partitioned to hold Solaris OS. disk two has been partitioned as... (6 Replies)
Discussion started by: duke0001
6 Replies

10. AIX

Creating a shell script to check filesystem space

I need to create a simple shell script to check filesystems space in a file system called "/arch_nb" then based on the percentage use either run another script or exit. I was thinking of something simple along the lines of: df -k | then some action to pipe for percentage used ...place... (10 Replies)
Discussion started by: heprox
10 Replies
Login or Register to Ask a Question