Variable not displaying in subject line of mailx email

Hi

Newbie here with first post. I've got a shell script (ksh) whereby I run a SQL*Plus script and output the results to a file. I then check the output file in an if statement that looks like this:

if [[ -e $OUTFILE ]]; then

export GAPNUM=`awk '{print $4}' $OUTFILE`

  if [[ $GAPNUM -ge 5 ]] then

  mailx -s "Possible Redo Apply Issue at $ORACLE_SID - Log Gap is "$GAPNUM" $MAIL_TO < $OUTFILE

  mv $OUTFILE $OUTFILE.$DAYHOUR

  fi

else

    echo "$OUTFILE does not exist" | mailx -s $SUBJECT $MAIL_TO
fi

Everything works fine except when the email is sent, although $ORACLE_SID value displays, $GAPNUM does not. I've searched on this site and others and am not sure what I'm doing wrong. If I place an echo $GAPNUM just before the mailx line it displays. What am I missing?

Thanks,

Laura

You've got an extra quotation mark in there:

mailx -s "Possible Redo Apply Issue at $ORACLE_SID - Log Gap is "$GAPNUM" $MAIL_TO < $OUTFILE

Which might be swallowing up quite a bit of code below it.

1 Like

Thanks Corona, but I'm not sure how that code in my post as it's not in my code. The line reads as-follows:

 mailx -s "Possible Redo Apply Issue at $ORACLE_SID - Log Gap is ${GAPNUM}" $MAIL_TO < $OUTFILE

Any other thoughts?

Thanks,

Laura

Unless

is a single line, this line will produce a variable with line breaks:

Update the statement to read a single variable.

Hi rdrtx

Thanks, but that is a single line and works fine. My issue is that the variable $GAPNUM does not show in the subject line, even though the other variable in the subject line ($ORACLE_SID) does.

Thanks,

Laura

Can you echo what "$GAPNUM" is set to prior to the mailx statement?

Yup, did that and it echoes the correct value of 0, which stumped me even more. I totally don't understand why it displays in the previous line (i.e the echo) but not in the subject line.

Are you going to tell us that:

  if [[ $GAPNUM -ge 5 ]] then

isn't your code either?

There are at least two problems here that don't match with your description. If $GAPNUM expands to 0 as you say it does, the mailx won't be executed because zero is not greater than or equal to 5. This statement is also missing a semicolon before the then .

1 Like

Hi Don

My apologies, I'm trying to do too much at once. For testing purposes, I changed my code to -le 5 so I could get the email. The current code is as-follows:

if [[ -e $OUTFILE ]]; then

export GAPNUM=`awk '{print $4}' $OUTFILE`

  if [[ $GAPNUM -le 5 ]] then

  mailx -s "Possible Redo Apply Issue at $ORACLE_SID - Log Gap is $GAPNUM" $MAIL_TO < $OUTFILE

  mv $OUTFILE $OUTFILE.$DAYHOUR

  fi

else

    echo "$OUTFILE does not exist" | mailx -s $SUBJECT $MAIL_TO
fi

If I execute this as-is, following is the email I receive:

-----Original Message-----
From: Oracle <oracle@xxxxxxxxx> 
Sent: Friday, August 2, 2019 11:11 AM
To:  Laura 
Subject: Possible Redo Apply Issue at omsprd - Log Gap is 

0


         3                  23200                 23200          0              

So it appears to put the $GAPNUM in the body of the email instead of the Subject line.

I think rdrtx1 in post #4 might be on the right track. Please post the contents of $OUTFILE , and the output of

echo $GAPNUM | od -tx1c

.

You might also note that (as I mentioned in post #8), there is a missing semicolon before the then in:

  if [[ $GAPNUM -le 5 ]] then

which should have given you a syntax error.