I'm trying to get specific parameter with awk every 15 minutes in my shell script. For example i want to get freemem:
$sar -r
SunOS CRP 5.10 Generic_138888-03 sun4v 06/01/2016
00:00:00 freemem freeswap
00:15:00 40317 14989925
00:30:00 43652 15043259
00:45:00 43650 15043147
......
Here my script.
#!/usr/bin/bash
projectDashboard=/opt/player/test/logResc
date=`date +%Y%m%d`
host=`hostname`
cm=$(date +%M)
ch=$(date +%H)
if [ $cm -ge 0 ] && [ $cm -le 15 ];then
sar -r | awk '$1 ~ /$ch:00:00/ {print ($2)}' | awk '{print '$date ' "|" '$host' "|" "memory" "|" $1}' >> $projectDashboard/"$host"_"$date"_00_15.log
elif [ $cm -ge 16 ] && [ $cm -le 30 ];then
sar -r | awk '$1 ~ /$ch:15:00/ {print ($2)}' | awk '{print '$date ' "|" '$host' "|" "memory" "|" $1}' >> $projectDashboard/"$host"_"$date"_00_15.log
elif [ $cm -ge 31 ] && [ $cm -le 45 ];then
sar -r | awk '$1 ~ /$ch:30:00/ {print ($2)}' | awk '{print '$date ' "|" '$host' "|" "memory" "|" $1}' >> $projectDashboard/"$host"_"$date"_00_15.log
elif [ $cm -ge 46 ] && [ $cm -le 59 ];then
sar -r | awk '$1 ~ /$ch:45:00/ {print ($2)}' | awk '{print '$date ' "|" '$host' "|" "memory" "|" $1}' >> $projectDashboard/"$host"_"$date"_00_15.log
fi
Expected output:
20160307|00:15:00|CRP|memory|40317
20160307|00:30:00|CRP|memory|40317
20160307|00:45:00|CRP|memory|40317
20160307|01:00:00|CRP|memory|40317
20160307|01:15:00|CRP|memory|40317
.......
What I get:
awk: syntax error near line 1
awk: illegal statement near line 1
I already try to change the $ch
to ${ch}
or awk "$1 ~ /$ch:30:00/ {print ($2)}"
but still doesn't work. Is there any mistyped or wrong code in my script? I appreciate your help.
UPDATE
I try to change the $ch
with awk -v
so the script is like this awk -v ch="$(date +%H)" '$1 ~ /ch:00:00/'
. But I still get an error.
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
can someone tell me what is wrong in my script?