./test.ksh[9]: [: argument expected - arguments

We are running to script to find the zfs file system monitoring and having error as below.
argument expected
#!/bin/sh
USED_SPACE_PERCENT_WARN=20
PATH=/usr/bin:/usr/sbin; export PATH
# check zfs File system
if [ `df -F zfs | wc -l` -gt 0 ]; then
/usr/sbin/zpool list -H | while read line
do
USED_SPACE_PERCENT=`echo "$line" | nawk -F'[ % ]+' '{print $5}'`
if [ $USED_SPACE_PERCENT -gt $USED_SPACE_PERCENT_WARN ]; then
POOL=`echo "$line" | nawk -F'[ % ]+' '{print $1}'`
echo "ZFS pool $POOL has used $USED_SPACE_PERCENT% of its space."
fi
done
fi

When USED_SPACE_PERCENT is empty (line without 5 fields), the command
if [ $USED_SPACE_PERCENT -gt $USED_SPACE_PERCENT_WARN ]; then
will turn into
if [ -gt 20 ]; then
and that causes the error

Related

pb nagios bash script null result in if

I have a problem with a bash script in Nagios. this is a script to get the space disk. When I used an IF section it returns (null) and when I don't set the variable in IF section, nagios display the variable correctly. I tried to run the script with nagios user and the result is good. Ex:
TOTAL=`/srv/eyesofnetwork/nagios/plugins/check_nt -H $2 -p 12489 -s "" -v USEDDISKSPACE -l $4 |awk -F"-" '{print $2}' |awk '{print $2}'`
if [ $TOTAL -gt 2 ] && [ $TOTAL -le 99 ];then
RUN=`/srv/eyesofnetwork/nagios/plugins/check_nrpe -H $2 -c ``check_drivesize -a drive=$4 'warning=free<2G' 'critical=free<1G' show-all 'perf-config=*(unit:G)' top-syntax='${status} : ${problem_list}'`
VAR=$(echo $RUN |grep -i ok |wc -l)
if [ $VAR -eq 1 ];then
echo "$RUN"
exit 0
fi
I tried all possibilities (for me..), with "", with '', with nothing. The variable $RUN is not displayed.
Thanks
Finally, I wrote a script in python and now it works correctly

How to include 4 more JVM instances?

I want to include 4 more JVMs to check logs are exiting or not and also want include those 4 More jvms to pull logs to path, as per below script. How can I modify the script which can used for multiple JVM?
#!/bin/bash
Minute=$(expr `date +%M` - 15)
Hour=$(expr `date +%H`)
Current_Time=$(date +%d/%b/%Y:%H:%M:%S)
f=$(find /apps_01/webapps/JBoss/logs/xxxx JVM1_server*/http_logs/ -name access_log.`date +%Y-%m-%d`.log |wc -l)
if [ "$f" -gt 0 ]
then
tail -500 /apps_01/webapps/JBoss/logs/xxxx JVM1_server*/http_logs/access_log.`date +%Y-%m-%d`.log|awk '{print $4}'| cut -d '[' -f2 >> /home/tmp/xxx_logs
h=$(awk -F'[: ]' '{print $2}' /home/tmp/xxx_logs|tail -1)
if [ "$h" -ge "$Hour" ]
then
for m in `awk -F'[: ]' '{print $3}' /home/tmp/xxx_logs`
do
if [ "$m" -ge "$Minute" ]
then
echo $m
fi
done
fi
fi

execute mailx using shell script

I am trying to run the below script.
val = `wc -l /home/validate.bad | awk '{print $1}' | tail -n1`
valCount = `wc -l /home/validation.txt | awk '{print $1}'`
if [ "$val" -gt 1 ] && ["$valCount" -gt 1]
then
mailx -s "Validation failed" -r xyz#abc.com xyz#abc.com<<-EOF
Hi ,
Validation has failed. Please check.
EOF
elif [ "$valCount" -gt 1 ]
then
mailx -s "Validation pass" -r xyz#abc.com xyz#abc.com<<-EOF
Hi Team,
Validation success.
EOF
fi
But I am getting this error.
Error:
val: comand not found
valCount: command not found
line 3[: : integer expression expected
You can't have spaces around = :
val = `wc -l /home/validate.bad | awk '{print $1}' | t` # wrong
and should have been
val=`wc -l /home/validate.bad | awk '{print $1}' | t`
or preferrably
val=$(wc -l </home/validate.bad)
#`..` is legacy , $() supports nesting, one good reason to go for it
# You use awk and tail uselessly
Also
["$valCount" -gt 1]
should have been
[ "$valCount" -gt 1 ] # mind the spaces for the test constructie
# [spaceSTUFFspace] is the correct form
Sidenote
You may use [ shellcheck ] to check your scripts.

BASH better way to monitor files

I've made a Bash script to monitor some server log files for certain data and my method probably isn't the most efficient.
One section specifically bugs me is that I have to write a newline to the monitored log so that the same line wont be read over continually.
Feedback would be greatly appreciated!
#!/bin/bash
serverlog=/home/skay/NewWorld/server.log
onlinefile=/home/skay/website/log/online.log
offlinefile=/home/skay/website/log/offline.log
index=0
# Creating the file
if [ ! -f "$onlinefile" ]; then
touch $onlinefile
echo "Name Date Time" >> "$onlinefile"
fi
if [ ! -f "$offlinefile" ]; then
touch $offlinefile
echo "Name Date Time" >> "$offlinefile"
fi
# Functions
function readfile {
# Login Variables
loginplayer=`tail -1 $serverlog | grep "[INFO]" | grep "joined the game" | awk '{print $4}'`
logintime=`tail -1 $serverlog | grep "[INFO]" | grep "joined the game" | awk '{print $2}'`
logindate=`tail -1 $serverlog | grep "[INFO]" | grep "joined the game" | awk '{print $1}'`
# Logout Variables
logoutplayer=`tail -1 $serverlog | grep "[INFO]" | grep "left the game" | awk '{print $4}'`
logouttime=`tail -1 $serverlog | grep "[INFO]" | grep "left the game" | awk '{print $2}'`
logoutdate=`tail -1 $serverlog | grep "[INFO]" | grep "left the game" | awk '{print $1}'`
# Check for Player Login
if [ ! -z "$loginplayer" ]; then
echo "$loginplayer $logindate $logintime" >> "$onlinefile"
echo "Player $loginplayer login detected" >> "$serverlog"
line=`grep -rne "$loginplayer" $offlinefile | cut -d':' -f1`
if [ "$line" > 1 ]; then
sed -i "$line"d $offlinefile
unset loginplayer
unset line
fi
fi
# Check for Player Logout
if [ ! -z "$logoutplayer" ]; then
echo "$logoutplayer $logoutdate $logouttime" >> "$offlinefile"
echo "Player $loginplayer logout detected" >> "$serverlog"
line=`grep -rne "$logoutplayer" $onlinefile | cut -d':' -f1`
if [ "$line" > 1 ]; then
sed -i "$line"d $onlinefile
unset logoutplayer
unset line
fi
fi
}
# Loop
while [ $index -lt 100 ]; do
readfile
done
Thanks!
instead of using multiple
tail -n 1 file
try the following construct:
tail -f file | while read line;do
echo "read: $line"
done
it will be much more reliable...and won't read the same line twice ;)
note: by using new processes of grep/awk/etc you are burning away processes...it's not that it is critical, but usually process creation is expensive...but if new lines occur rarely it's perfectly fine
where i want'ed to get is: if you are intrested, take a look at bash builting string manipulator function replace $(x/aa} ${x//aa} and friends..or try to use extended regexpes with grep

Bash error echo a command

I have a problem. I need to show a echo from a while, I use two echo the first one work but the second it give a error.
#!/bin/bash
conexiuni="/tmp/conexiuni"
if [ "$1" != "" ]; then
netstat -tuan | grep $1 | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n > $conexiuni
else
netstat -tuan | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n > $conexiuni
fi
cat $conexiuni | while read line
do
con=`echo ''$line'' | awk '{print $1}'`
ip=`echo ''$line'' | awk '{print $2}'`
if [ "$con" -gt "4" ]; then
`echo -e "$ip" >> /var/log/drop_sc_ip`
`echo -e "$ip"`
fi
done
if [ -f "$conexiuni" ];
then
`rm -rf $conexiuni`
fi
The error is :
./show_conn: line 15: 8.97.80.2: command not found
./show_conn: line 15: 8.76.109.13: command not found
./show_conn: line 15: 8.33.15.2: command not found
./show_conn: line 15: 9.118.226.3: command not found
You can write this part without the backticks:
if [ "$con" -gt "4" ]; then
echo -e "$ip" >> /var/log/drop_sc_ip
echo -e "$ip"
fi
also same in this part:
rm -rf $conexiuni
with the backticks, it first executes what is inside the backticks and then tries to execute the output of the backticks.
and change the loop:
while read con ip
do
if [ "$con" -gt "4" ]; then
echo -e "$ip" >> /var/log/drop_sc_ip
echo -e "$ip"
fi
done < $conexiuni

Resources