If echo statement return false

I have this code that sometimes return a false value and the code inside the if statement gets executed and error out. Any idea why? thanks.

So I set a debug and see what the value for $ScriptElapsedTime

Here is the value I got ScriptElapsedTime='03:20'. Base on this value the if statement shouldn't even execute.

ScriptElapsedTime=`ps -p $$ -o etime | tail -1 | awk '{print $1}'`

if echo "$ScriptElapsedTime" | grep ":" | grep -q "\-"
then
  mailout.sh NAME "$oradb - ScriptElapsedTime debug" "ScriptElapsedTime='$ScriptElapsedTime'"
  procdays=`echo "$ScriptElapsedTime" | awk -F- '{print $1}'`
  ScriptElapsedTime=`echo "$ScriptElapsedTime" | awk -F- '{print $2}'`
  let TotProcMin=$TotProcMin+$procdays*1440
fi

let: :: invalid character in expression - TotProcMin=0+3:20*1440

What OS & shell versions are you using?

Try:

TotProcMin=$(($TotProcMin+${procdays/:*}*1440))

Solaris 5.11 Korn

---------- Post updated at 11:59 AM ---------- Previous update was at 11:56 AM ----------

sea: the issue is not the code inside the if statement, that if statement is false with value "03:42" and should not excute

@sea, ITYM

TotProcMin=$((${TotProcMin/:*}+${procdays}*1440))

Some optimizations (less calls to external programs); should work in most shells:

ScriptElapsedTime=`ps -p $$ -o etime= | awk '{print $1}'`

if [[ "$ScriptElapsedTime" = *-* ]]
then
  mailout.sh NAME "$oradb - ScriptElapsedTime debug" "ScriptElapsedTime='$ScriptElapsedTime'"
  procdays=${ScriptElapsedTime%-*}
  proctime=${ScriptElapsedTime##*-}
  procminutes=${proctime%:*}
  TotProcMin=$(( $procminutes+$procdays*1440 ))
fi

I've had the post edited, but seems i was too slow.. ${procdays/:*}
It is/was the days that have the hours and minutes, i guess that is the core issue.
EDIT:
Of course i was refering to the original code, not yours :wink:

it should only execute if ScriptElapsedTime="1-3:20"

ScriptElapsedTime="1-3:20" is not a condition, its a variable asigning.

Further, math might work with float operators, usualy dots or comas, but not with : .
You gotta get rid of the : , handle it otherwise, for example as M.I.G provided.

You are right - it shouldn't execute unless the process is older than a day. And, on my system it doesn't. So, you might want to analyse the line step by step, checking every statement's exit status to find the reason for this inexplicable behaviour.

@RudiC its intermitten so its a little hard to troubleshoot

I think this is what happens. It executes with ScriptElapsedTime="1-3:20", and then your code sets ScriptElapsedTime="3:20" and then it fails.
Take my code! It sets proctime="3:20" and keeps the ScriptElapsedTime, so debugging is more clear.