I am trying to do a simple condition check, but it doesn\'t seem to work.
If $#
is equal to 0
or is greater than 1
then say he
And in Bash
line1=`tail -3 /opt/Scripts/wowzaDataSync.log | grep "AmazonHttpClient" | head -1`
vpid=`ps -ef| grep wowzaDataSync | grep -v grep | awk '{print $2}'`
echo "-------->"${line1}
if [ -z $line1 ] && [ ! -z $vpid ]
then
echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` ::
"Process Is Working Fine"
else
echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` ::
"Prcess Hanging Due To Exception With PID :"${pid}
fi
OR in Bash
line1=`tail -3 /opt/Scripts/wowzaDataSync.log | grep "AmazonHttpClient" | head -1`
vpid=`ps -ef| grep wowzaDataSync | grep -v grep | awk '{print $2}'`
echo "-------->"${line1}
if [ -z $line1 ] || [ ! -z $vpid ]
then
echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` ::
"Process Is Working Fine"
else
echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` ::
"Prcess Hanging Due To Exception With PID :"${pid}
fi
From Bash Reference Manual → 3.4.2 Special Parameters
#
($#) Expands to the number of positional parameters in decimal.
Therefore, $#
will always be either 0 or a bigger integer.
So if you want to do something whenever $#
is either 0 or bigger than 1, you just have to check if $#
is or is not 1
:
[ $# -eq 1 ] && echo "1 positional param" || echo "0 or more than 1"
This uses the syntax:
[ condition ] && {things if true} || {things if false}