Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Echoing a TimeStamp into a log file with Korn Shell script using `date` returns same time 2

Status
Not open for further replies.

Atstone1997

Programmer
Sep 29, 2016
3
US
Hello, First time post

So I added logic to my script to write data to a log file to see how long my sql's are running within a script but the times come back the same even thought the sql takes several minutes. I was wondering if anyone else tried this solution

##-------------------------------------------------------------------------------------
#!/bin/ksh

export OUTPUT_PATH=/billing

set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov

PATH_CURR_MONTH=`date +%m`
PATH_CURR_YEAR=`date +%Y`
PATH_PREV_MONTH=${MONTHS[$((PATH_CURR_MONTH -1))]}

mkdir -p $OUTPUT_PATH/$PATH_PREV_MONTH$PATH_CURR_YEAR

export BLGOUTPUT=$OUTPUT_PATH/$PATH_PREV_MONTH$PATH_CURR_YEAR
export PROGRESS_LOG=$BLGOUTPUT/track_progress.txt

echo `date +"%Y-%m-%d %H:%M:%S"` "Begin Script " >> $PROGRESS_LOG ##This works

dbaccess -e $DB_NAME <<-! >> $BLG_LOG 2>&1

!echo `date +"%Y-%m-%d %H:%M:%S"` "Begin DB Processing" >> $PROGRESS_LOG ##This works

Select * from table1 into temp tmpTable1 with no log; ##This runs for several minutes

!echo `date +"%Y-%m-%d %H:%M:%S"` "Begin DB Processing" >> $PROGRESS_LOG ##This works but the date and time are the same value as the last echo above

!
echo `date +"%Y-%m-%d %H:%M:%S"` "End Script " >> $PROGRESS_LOG ##This works and the time is several minutes later.
##-------------------------------------------------------------------------------------

I ran the ksh script with -x and what I notice is that all the echo commands within the dbaccess section execute even though the select script was still running.

Does anyone have an answer to this? I really don't want to use extra calls to the database to use the current command to set the timestamp

Thanks in advance



 
Hi

When executing that, the shell performs the command substitution in the here-document, so this :
Code:
dbaccess -e $DB_NAME <<-! >> $BLG_LOG 2>&1

!echo `date +"%Y-%m-%d %H:%M:%S"` "Begin DB Processing" >> $PROGRESS_LOG ##This works

Select * from table1 into temp tmpTable1 with no log; ##This runs for several minutes

!echo `date +"%Y-%m-%d %H:%M:%S"` "Begin DB Processing" >> $PROGRESS_LOG ##This works but the date and time are the same value as the last echo above

!

Becomes nothing more than something like this :
Code:
echo '
!echo 2016-09-30 10:38:17 "Begin DB Processing" >>  ##This works

Select * from table1 into temp tmpTable1 with no log; ##This runs for several minutes

!echo 2016-09-30 10:38:17 "Begin DB Processing" >>  ##This works but the date and time are the same value as the last echo above

' | dbaccess -e $DB_NAME >> $BLG_LOG 2>&1

I mean, all command substitutions being expanded at the same time before passing it to the dbaccess command, is correct to be all the same.

What you could try :
[ul]
[li]Ask Ksh to not perform expansions on the here-document ( not sure if older Ksh versions support it ) :
Code:
dbaccess -e $DB_NAME <<-[COLOR=red yellow]'[/color]![COLOR=red yellow]'[/color] >> $BLG_LOG 2>&1

!echo `date +"%Y-%m-%d %H:%M:%S"` "Begin DB Processing" >> $PROGRESS_LOG ##This works

Select * from table1 into temp tmpTable1 with no log; ##This runs for several minutes

!echo `date +"%Y-%m-%d %H:%M:%S"` "Begin DB Processing" >> $PROGRESS_LOG ##This works but the date and time are the same value as the last echo above

!
[/li]
[li]Escape the backticks ( ` ) to prevent the expansion ( this has to work everywhere, but is messy ) :
Code:
dbaccess -e $DB_NAME <<-! >> $BLG_LOG 2>&1

!echo [COLOR=red yellow]\[/color]`date +"%Y-%m-%d %H:%M:%S"[COLOR=red yellow]\[/color]` "Begin DB Processing" >> $PROGRESS_LOG ##This works

Select * from table1 into temp tmpTable1 with no log; ##This runs for several minutes

!echo [COLOR=red yellow]\[/color]`date +"%Y-%m-%d %H:%M:%S"[COLOR=red yellow]\[/color]` "Begin DB Processing" >> $PROGRESS_LOG ##This works but the date and time are the same value as the last echo above

!
[/li]
[li]Do not use command substitution ( not sure if all [tt]date[/tt] implementations support it ) :
Code:
dbaccess -e $DB_NAME <<-! >> $BLG_LOG 2>&1

!date +"%Y-%m-%d %H:%M:%S Begin DB Processing" >> $PROGRESS_LOG ##This works

Select * from table1 into temp tmpTable1 with no log; ##This runs for several minutes

!date +"%Y-%m-%d %H:%M:%S Begin DB Processing" >> $PROGRESS_LOG ##This works but the date and time are the same value as the last echo above

!
[/li]
[/ul]


Feherke.
feherke.ga
 
Hi,
the whole heredoc is evaluated before it is passed to dbaccess, which will be fed with the results of the command substitution of the date command, not the date command itself.
Quote the heredoc delimiter to turn of all substitutions and expansions:
Code:
...
dbaccess -e $DB_NAME <<-[COLOR=#EF2929]"[/color]![COLOR=#EF2929]"[/color] >> $BLG_LOG 2>&1

 !echo `date +"%Y-%m-%d %H:%M:%S"` "Begin DB Processing" >> $PROGRESS_LOG 

 Select * from table1 into temp tmpTable1 with no log; ##This runs for several minutes

 !echo `date +"%Y-%m-%d %H:%M:%S"` "Begin DB Processing" >> $PROGRESS_LOG 

!
...
 
Thanks feherke and stefanhei. The double quotes around the exclamation mark within the dbaccess call did the trick.

feherke: I tried your suggestions as well, they didn't work, but I appreciate the help.
 
Spoke too soon, The timestamp is coming back correctly, now the unloads to the variable isn't working.

unload to "$BLGOUTPUT/cycl_dates.txt"
select * from wrk_cycl;

it doesn't evaluate the $BLGOUTPUT variable, I get the following:

806: Cannot open file for unload

No such file or directory
Error on line 2
Near character position 8

When I change the DBACCESS call to not include the double quotes the file unloads fine

dbaccess -e $DB_NAME <<! 2>&1 ##works
dbaccess -e $DB_NAME <<"!" 2>&1 ##Doesn't work
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top