"if" and "then" statement is not working in RedHat

Dear experts,

I'm trying to write a script to calculate the usage of Log Archive in a directory, so if it gets to a point where the directory size is 60%, then send out an FYI.. email. So if then it reaches to 80%, move the logs from that directory.

I have written the script as follow but I'm getting errors. Your help is really needed and appreciated!

Script:

#!/bin/csh
set OUTPUTLOG="$HOME/logs/Log_Archive/ORA_Log_Archive.log"
set DATE=`date`
echo "--------------------------------------------------------" >> $OUTPUTLOG
echo "Log_Archive script started at $DATE" >> $OUTPUTLOG
echo "--------------------------------------------------------" >> $OUTPUTLOG
set HOST=`hostname`
set DATE=`date +"%m"-"%d"-"%Y"_"%H":"%M"`
set NUMARCHLOGS=`ls $HOME/xyz | wc -l`
set LOG_ARCH_PERCENT=`df -m $HOME/xyz | grep -v "Filesystem" | awk '{print int(substr($4,0,length($4)-1))}'`
#
echo "There are $NUMARCHLOGS Logs in $HOME/xyz >> $OUTPUTLOG

        if ($LOG_ARCH_PERCENT > 60) then
echo "Log_Arive since it's at $LOG_ARCH_PERCENT" >> $OUTPUTLOG        
mail -s "Subject:Log Archive is at $LOG_ARCH_PERCENT% on $HOST" xyz@xyz.com
endif
 
if ($LOG_ARCH_PERCENT > 80) then
mv $HOME/xyz/* $HOME/xyzbackup >> $OUTPUTLOG
echo "Moving  Log_Arive since it's at $LOG_ARCH_PERCENT" >> $OUTPUTLOG        
mail -s "Subject:Log Archive is moved to a xyzbackup on $HOST" xyz@xyz.com
            endif

Thanks,

You might need a refresher course in bourne/bash scripting.

You could use:

if [ $LOG_ARCH_PERCENT -gt 60 ]; then

for portability... or if you assume bash (common on Linux):

if [[ $LOG_ARCH_PERCENT > 60 ]]; then
1 Like

You have to use the test command to do the comparison. Test can be replaced by square brackets as per the previous post.

1 Like

Hi Cjcox and KevinGB,

I did try this before, however, it gave me the following error

Error:

@NUMARCHLOGS=10: Command not found.
if: Expression Syntax.

I'm still getting this error even after using "[]" syntax. I have cut down the script as follow. Can you please help me to figure out where the problem would be?

Script:

set OUTPUTLOG="$HOME/logs/Log_Archive/ORA_Log_Archive.log"
set DATE=`date`
echo "--------------------------------------------------------" >> $OUTPUTLOG
echo "Log_Archive script started at $DATE" >> $OUTPUTLOG
echo "--------------------------------------------------------" >> $OUTPUTLOG
set HOST=`hostname`
set DATE=`date +"%m"-"%d"-"%Y"_"%H":"%M"`
@NUMARCHLOGS=`ls $HOME/LOG | wc -l`
set LOG_ARCH_PERCENT="df -m $HOME/LOG | grep -v "Filesystem" | awk '{print int(substr($4,0,length($4)-1))}'"
#
if [ $LOG_ARCH_PERCENT -gt 80 ]; then
        echo "Moving  Log_Arive since its at $LOG_ARCH_PERCENT" >> $OUTPUTLOG
        mv $HOME/LOG/* $HOME/backup/archive >> $OUTPUTLOG
        mail -s "Subject:Log Archive is moved to a xyzbackup on $HOST" syz@abc.com< $OUTPUTLOG
endif

Your help is really appreciated!

Thanks,

try below syntax...

if [[ $LOG_ARCH_PERCENT -ge "60" ]]; then

Hi Subhash,

Thanks for the help. I have tried the following commands/sysntaxes but getting the same issues.

if [[ $LOG_ARCH_PERCENT -gt "60" ]]; then
if [[ $LOG_ARCH_PERCENT -ge "60" ]]; then
if [ $LOG_ARCH_PERCENT -gt "60" ]; then
if [ $LOG_ARCH_PERCENT -ge 60 ]; then
if [[ $LOG_ARCH_PERCENT -gt `60` ]]; then
if [[ $LOG_ARCH_PERCENT -ge `60` ]]; then

Any idea?

Thanks,

I'm no C Shell expert but isn't the if sytax something like:

if (whatever) do_something

Anyway, I think the main error is on line 12. You are missing the closing quotes:

"There are $NUMARCHLOGS Logs in $HOME/xyz" >> $OUTPUTLOG

The original "if" syntax in post #1 looks ok. As verdepollo points out there is a double quote missing.

I agree. The problem is the missing double quote. I had missed that the script was using csh (and the missing quote). The if () syntax is correct for csh.

verdepollo is correct, the problem appears to be the mission " on line 12. According to the Solaris csh man page,

 
if (checkstuff) then
endif 

is the correct syntax

Dear,
Please do an echo before you compare this value with if []. I suspect the variable $LOG_ARCH_PERCENT has a non numeric value.

echo $LOG_ARCH_PERCENT