Please help me with this:
I need to compare two values in if condition in shell script but its goes always to else condition:
TIME_CHECK=PM
TIME-CLOCK=PM
if [ $TIME_CHECK -eq $TIME-CLOCK ]; then
echo "You have access!"
else
echo "ACCESS DENIED!"
fi
Try this:
TIME_CHECK="PM"
TIME-CLOCK="PM"
if [ $TIME_CHECK = $TIME-CLOCK ]; then
echo "You have access!"
else
echo "ACCESS DENIED!"
fi
or this:
TIME_CHECK=PM
TIME-CLOCK=PM
if [ $TIME_CHECK = $TIME-CLOCK ]; then
echo "You have access!"
else
echo "ACCESS DENIED!"
fi
This is what i am doing:
TIME_DATE_SPLIT=`date +%Y-%m-%d-%p`
TIME_CHECK="`(echo $TIME_DATE_SPLIT | cut -d "-" -f 4)`"
TIME_CLOCK="AM"
if [ $TIME_CHECK -eq $TIME_CLOCK ]; then
echo "You have access!"
else
echo "ACCESS DENIED!"
fi
This always goes in else condition if AM or PM and gives the error "[: AM: integer expression expected"
Please help
See what you are doing is storing the results of TIME_DATE_SPLIT and TIME_CHECK as strings. The "-eq" expression only works with integers so replace that with "=" and it should work.
methyl
June 28, 2012, 9:51am
5
You can get the AM/PM directly from the date command.
TIME_CHECK="`date +%p`" # AM or PM
TIME_CLOCK="AM"
if [ "${TIME_CHECK}" = "${TIME_CLOCK}" ]; then
echo "You have access!"
else
echo "ACCESS DENIED!"
fi
TIME_DATE_SPLIT=`date +%Y-%m-%d-%p`
TIME_CHECK="`(echo $TIME_DATE_SPLIT | cut -d "-" -f 4)`"
#######FUNCTION START########
PREVIOUS_DATE_FUNCTION()
{
date '+%m %d %Y' |
{
read MONTH DAY YEAR
DAY=`expr "$DAY" - 1`
case "$DAY" in
0)
MONTH=`expr "$MONTH" - 1`
case "$MONTH" in
0)
MONTH=12
YEAR=`expr "$YEAR" - 1`
;;
esac
DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac
PREVIOUS_TEMP_DATE=$YEAR-$MONTH-$DAY
echo $PREVIOUS_TEMP_DATE
}
}
#######FUNCTION END########
TIME_CLOCK="AM"
if [ $TIME_CHECK = $TIME_CLOCK ]; then
PREVIOUSDATE="`PREVIOUS_DATE_FUNCTION`" #FUNCTION CALL#
tempfile=CANCEL_FILE_$PREVIOUSDATE_AM
Problem here is that $PREVIOUSDATE value is null.I am not getting the tempfile=CANCEL_FILE_27-06-2012_AM
I am getting
tempfile=CANCEL_FILE__AM