If echo statement return false


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If echo statement return false
# 1  
Old 09-28-2015
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.
Code:
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


Last edited by Franklin52; 09-28-2015 at 10:58 AM.. Reason: Please use code tags
# 2  
Old 09-28-2015
What OS & shell versions are you using?
# 3  
Old 09-28-2015
Try:
Code:
TotProcMin=$(($TotProcMin+${procdays/:*}*1440))


Last edited by sea; 09-28-2015 at 11:54 AM.. Reason: changed code
# 4  
Old 09-28-2015
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
# 5  
Old 09-28-2015
@sea, ITYM
Code:
TotProcMin=$((${TotProcMin/:*}+${procdays}*1440))

Some optimizations (less calls to external programs); should work in most shells:
Code:
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

# 6  
Old 09-28-2015
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 Smilie
# 7  
Old 09-28-2015
it should only execute if ScriptElapsedTime="1-3:20"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Return code 1 when echo to pipe

Hello, Our AIX box has recently been upgraded to TL12 (oslevel -s = 5300-12-04-1119). Now one of our ksh scripts is returning 1 when writing to a pipe, the command to write to the pipe is: echo "A" "B" "C" >> /usr/Pipe.Pipe Anyone have any ideas? Thanks (2 Replies)
Discussion started by: dendright
2 Replies

2. Shell Programming and Scripting

how to read a file to an echo statement

I was searching for an option where i can echo some strings together with the contents of a file. Eg. I was to echo the below string to a file "alter application PMS_ add variable CurrYear Y2009;" >> ${ESS_MAXL_DIR}/PMS_SUB.txt The value 2009 is coming from a file called date.txt without the... (3 Replies)
Discussion started by: Celvin VK
3 Replies

3. Shell Programming and Scripting

Return vs. Echo

In a Ksh functions, when you have both echo and return respectively - what does it do. E.g. Func B () { ..... { ..... echo $Varaible } Return 0 } Func A () { $Var1 = Func B() .... .... } (6 Replies)
Discussion started by: gagan8877
6 Replies

4. Shell Programming and Scripting

echo 2 txt files to screen no carraige return

I have two text files, each of then only containing ONE line and NO carraige return or white space at the end...how do I echo both of these text files to the screen without putting an extra line? I want to do this from the command line. file1.txt: this is file1.txt 1 file2.txt: this is... (4 Replies)
Discussion started by: ajp7701
4 Replies

5. Shell Programming and Scripting

Output of both the echo statement in one line

I have script like echo -n FINISHED FEXP: ${TABLE2EXP} echo $STATUS I want the output of both the echo statement in one line How can i do this (3 Replies)
Discussion started by: scorp_rahul23
3 Replies

6. UNIX for Advanced & Expert Users

Return the last echo in remsh !!!!

Hi, I have a question: the script A run in the HostA call the script B on the HostB: ex.. ksh:B ....... ........ ........ remsh HostB ec........ ...... ...... the prog.B on the host B make more function but the last command is echo of srting : ex ksk script B .... ...... (0 Replies)
Discussion started by: ZINGARO
0 Replies

7. Shell Programming and Scripting

setting width in echo statement

Hello All, I need to set the width or number of columns for my dynamic output in the echo statement. statement is like: echo " <output> " here the <output> is dyamice and can be of any number of characters, the " " should always start in same column everytime it is... (4 Replies)
Discussion started by: s123.radha
4 Replies

8. Shell Programming and Scripting

echo statement issue

Hi All, I am pasting my code below if # e means file exists then echo OFR_Configlist exists >> OFR_Backup_Configfiles.log else echo OFR_Configlist Not exists >> OFR_Backup_Configfiles.log exit fi How can i show the echo message in console also at the same time? I dont want to write... (3 Replies)
Discussion started by: subin_bala
3 Replies

9. Shell Programming and Scripting

Insert TAB in echo statement

Hi, Can some1 help me to output a tab in an echo statement. I have tried echo "RNC: \t NODEB" but dont get the correct output. I am a beginnger to unix, so pls hold back the laughs....if u can (5 Replies)
Discussion started by: sunils27
5 Replies

10. Shell Programming and Scripting

echo statement

Does anyone know the correct syntax for computing arithmetic expressions inside the echo statement? Let me know, thanks (3 Replies)
Discussion started by: circleW
3 Replies
Login or Register to Ask a Question