extglob – expression power of rgular expressions in bash/ksh

extglob

In addition to the traditional globs (supported by all Bourne-family shells) that we’ve seen so far, Bash (and Korn Shell) offers extended globs, which have the expressive power of regular expressions. Korn shell enables these by default; in Bash, you must run the command

shopt -s extglob

in your shell (or at the start of your script — see note on parsing below) to use them. The pattern matching reference describes the syntax, which is reproduced here:

?(pattern-list)
Matches zero or one occurrence of the given patterns.
*(pattern-list)
Matches zero or more occurrences of the given patterns.
+(pattern-list)
Matches one or more occurrences of the given patterns.
@(pattern-list)
Matches one of the given patterns.
!(pattern-list)
Matches anything except one of the given patterns.

Patterns in a list are separated by | characters.

Extended globs allow you to solve a number of problems which otherwise require a rather surprising amount of ugly hacking; for example,

# To remove all the files except ones matching *.jpg:
rm !(*.jpg)
# All except *.jpg and *.gif and *.png:
rm !(*.jpg|*.gif|*.png)
# To copy all the MP3 songs except one to your device
cp !(04*).mp3 /mnt

To use an extglob in a parameter expansion (this can also be done in one BASH statement with read):

# To trim leading and trailing whitespace from a variable
x=${x##+([[:space:]])}; x=${x%%+([[:space:]])}

Extended glob patterns can be nested, too.

[[ $fruit = @(ba*(na)|a+(p)le) ]] && echo 'Nice fruit'

Reference:  http://mywiki.wooledge.org/glob

 

script

Requirement:

in below output (4th column) you can observe that some values are higher like 93 , 102 … 

i need a command/script which runs for every 30 min/1 hour and send mail  when ever it detects any process whose value is more thatn 50

db2fenc1 26869926 20775480   0 07:31:00      –  0:01 db2fmp (idle) 0
db2fenc1 35258592 20775480   0   Jan 04      –  0:00 db2fmp (idle) 0
db2fenc1 48824542 20775480  91   Jan 04      – 2390:16 db2fmp (70142) 0
db2fenc1 55509030 20775480 102   Jan 04      – 2429:06 db2fmp (70913) 0
db2fenc1  3408414 20775480  91   Jan 04      – 2396:36 db2fmp (111152) 0
db2fenc1  8651288 20775480  98   Jan 04      – 2432:14 db2fmp (69885) 0
db2fenc1 10683076 20775480  97   Jan 04      – 2401:23 db2fmp (60662) 0
db2fenc1 13238968 20775480   0 10:35:08      –  0:00 db2fmp (95115) 0
db2fenc1 17957440 20775480  85   Jan 04      – 2418:27 db2fmp (74500) 0
db2fenc1 18875132 51970054   0   Dec 15      –  0:01 db2fmp (C) 0
db2fenc1 19268132 20775480  87   Jan 04      – 2442:50 db2fmp (103174) 0
db2fenc1 19792618 20775480   0   Jan 04      –  0:02 db2fmp (idle) 0
db2fenc1 21693040 20775480   0   Jan 04      –  0:00 db2fmp (91774) 0
db2fenc1 23331344 20775480   0   Dec 31      –  0:04 db2fmp (C) 0
db2fenc1 23921376 51970054   0   Dec 15      –  0:00 db2fmp (idle) 0
db2fenc1 25166396 20775480 102   Jan 04      – 2442:36 db2fmp (102109) 0
db2fenc1 26673848 20775480 100   Jan 04      – 2440:56 db2fmp (65756) 0
db2inst1 28115564 66584822   0 15:46:57  pts/1  0:00 grep -i fenc
 
db2inst1@mygbppgodb201:/db2home/db2inst1> ps -ef|grep -i fenc
db2fenc1 22085670 37487042   0 02:55:06      –  0:00 db2fmp (idle) 0
db2inst1 24117472 11665860   0 13:48:02  pts/3  0:00 grep -i fenc
db2fenc1 42205314 37487042   0   05 Jan      –  0:00 db2fmp (idle) 0
db2fenc1  7602520 37487042   0   05 Jan      –  0:00 db2fmp (idle) 0
db2fenc1  8651198 37487042   0   05 Jan      –  0:00 db2fmp (88671) 0
db2fenc1 26214858 37487042   0 13:36:26      –  0:00 db2fmp (C) 0
db2fenc1 27787684 37487042   0 02:50:04      –  0:00 db2fmp (idle) 0
 
Solution:
Use the following line in cron to run for every 30 minutes
#!/usr/bin/bash
#script to parse ps -ef fields
TIMESTAMP=`date +%d%m%Y-%T`
SCRIPT=`basename $0`
LOGFILE=”/var/tmp/$SCRIPT.log”
TMPFILE=/var/tmp/$SCRIPT.$$
FILE=/var/tmp/SCRIPT.$TIMESTAMP
SUBJECT=”Process with above 50 at $TIMESTAMP”
EMAIL=”somemail@domain.com
ps -ef|grep -i fenc|awk ‘$4 > 50 {print $0}’ >$TMPFILE
if [ -s $TMPFILE ]; then
echo “/bin/mail -s “$SUBJECT” “$EMAIL” < $TMPFILE”
if [ $? == 0 ];then
echo “Failed to send email, copied file to $FILE” >> $LOGFILE
mv $TMPFILE $FILE
else
echo “Sent mail successfull to $EMAIL at $TIMESTAMP” >> $LOGFILE
rm $TMPFILE
fi
else
echo “$TIMESTAMP Good news no process with above 50 values” >> $LOGFILE
fi
#end of the script