INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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



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

2
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 :
  • Ask Ksh to not perform expansions on the here-document ( not sure if older Ksh versions support it ) :

    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
    
    ! 
  • 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 \`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
    
    ! 
  • Do not use command substitution ( not sure if all date 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
    
    ! 

Feherke.
feherke.ga

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

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 --> ksh

...
dbaccess -e $DB_NAME <<-"!" >> $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 

!
... 

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

(OP)
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.

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

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close