Shell Scripting -DateTime Manipulat
|
Original Message
|
Name: pmjain
Date: February 23, 2004 at 19:32:45 Pacific
Subject: Shell Scripting -DateTime Manipulat OS: Window/Unix CPU/Ram: --
|
Comment: Thanks a lot for ur responses those have really helped me a newbie in this world of shell scripting i would like to know after sourcing the functions to the main script can i call the functions just by the name functions.sh has a function s_interval eg main_script.sh is as follows functions.sh echo "functions called" s_interval will this suffice or do i need to use any other way of calling the s_interval function i have to also find the total execution time of the script is it possible to take the start time and the completion time and then subtract the start time from the completion time. can anyone of u help me in doing this or atleast providing a hint. can i do it this way, let me know if its correct. start_time() { date+%H%M%S } end_time() { date+%H%M%S } s_interval() { arg1=start_time() arg2=end_time() arg3=expr arg2-arg1 } thanks in advance Shalini
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: fpmurphy
Date: February 23, 2004 at 21:40:51 Pacific
Subject: Shell Scripting -DateTime Manipulat |
Reply: (edit)See the time(1) man page. This is what you should use to determine the total execution time of your script.
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: aigles
Date: February 23, 2004 at 23:26:49 Pacific
Subject: Shell Scripting -DateTime Manipulat
|
Reply: (edit)Hi Shalini, After sourcing the functions (with '. functions.sh') call the functions by their name like for a standard unix command. You can use the shell variable SECONDS to determine the execution time.
$SECOND : nb seconds since shell was invoked #Script example echo "Start of script" echo "SECONDS=$SECONDS" echo "Sleep 5s" sleep 5 echo "SECONDS=$SECONDS" echo "Sleep 3s" sleep 3 echo "SECONDS=$SECONDS" echo "End Of script" echo "SECONDS=$SECONDS" #----- Execution : Start of script SECONDS=0 Sleep 5s SECONDS=5 Sleep 3s SECONDS=8 End Of script SECONDS=8 Jean-Pierre.
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: pmjain
Date: February 24, 2004 at 00:36:31 Pacific
Subject: Shell Scripting -DateTime Manipulat
|
Reply: (edit)Thanks But How can i get the execution time in the following format Time has to be in the format of HH:MM:SS script_name|1:30:30|Total Time| can we pass parameters to function if yes then how?
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: aigles
Date: February 24, 2004 at 01:31:06 Pacific
Subject: Shell Scripting -DateTime Manipulat
|
Reply: (edit)The following function print total time : Total_Time() { ss=${1:-SECONDS} mm=`expr $ss / 60` ss=`expr $ss % 60` hh=`expr $mm / 60` mm=`expr $mm % 60` echo "$0|$hh:$mm:$ss|Total Time|" } Jean-Pierre.
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: pmjain
Date: February 24, 2004 at 02:33:59 Pacific
Subject: Shell Scripting -DateTime Manipulat
|
Reply: (edit)Thanks So this will give me the total execution time of the shell script in HH:MM:SS format but in this script have not considered the start time and the completion i.e end time of the script
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: aigles
Date: February 24, 2004 at 03:00:41 Pacific
Subject: Shell Scripting -DateTime Manipulat
|
Reply: (edit)The variable $SECONDS contains the number of seconds since the script was invoked. The function Total_time print the total time since the start of the script. The following version of the function accept end and start time as arguments : # # Total_Time : Print interval time in HH:MM:SS format # Arg1 = End time in seconds, default $SECONDS # Arg2 = Start time in seconds, default 0 (start of script) # Total_Time() { etime=${1:-SECONDS} stime=${2:-0} ss=`expr $etime - $stime` mm=`expr $ss / 60` ss=`expr $ss % 60` hh=`expr $mm / 60` mm=`expr $mm % 60` echo "$0|$hh:$mm:$ss|Total Time|" } # Get Start time start_time=$SECONDS # Work . . . . . . . # Get End time end_time=$SECONDS # Print Interval time Total_Time $end_time $start_time Jean-Pierre.
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: pmjain
Date: February 24, 2004 at 04:09:57 Pacific
Subject: Shell Scripting -DateTime Manipulat
|
Reply: (edit)Thanks for the response can u let me know if the way i am doing is right or wrong The Statement echo $SCRIPT_NAME "|"`date '+%m/%d/%y |%H:%M:%S'"|Started |"`>> ${OUTPUT} -- this is reqired to be logged in a file date +%H%M%S -- this i have given so that this function returns the the time in HHMMSS so that it can be passed on to the execution_time function my functions.sh has the following ################################################################################ # Record the Start Time ################################################################################ start_time() { echo $SCRIPT_NAME "|"`date '+%m/%d/%y |%H:%M:%S'"|Started |"`>> ${OUTPUT} date +%H%M%S } ################################################################################ # Record the Completion Time ################################################################################ completion_time() { echo $SCRIPT_NAME "|"`date '+%m/%d/%y |%H:%M:%S'"|Completed |"`>> ${OUTPUT} date +%H%M%S } ################################################################################ # Record the Execution Time ################################################################################ execution_time() { h1=`echo $1 | cut -c1-2` # Get Start Hour m1=`echo $1 | cut -c3-4` # Get Start Minute s1=`echo $1 | cut -c5-6` # Get Start Second h2=`echo $2 | cut -c1-2` # Get Stop Hour m2=`echo $2 | cut -c3-4` # Get Stop Minute s2=`echo $2 | cut -c5-6` # Get Stop Second s3=`expr $s2 - $s1` # Calculate Second Difference if [ $s3 -lt 0 ] # Test for Negative Seconds then s3=`expr $s3 + 60` # If yes - add one minute... m1=`expr $m1 + 1` # ... and to subtractor fi m3=`expr $m2 - $m1` # Calculate Minute Difference if [ $m3 -lt 0 ] # Test for Negative Minutes then m3=`expr $m3 + 60` # If yes - add one hour... h1=`expr $h1 + 1` # ... and to subtractor fi h3=`expr $h2 - $h1` # Calculate Hour Difference if [ $h3 -lt 0 ] # Test for Negative Hours then h3=`expr $h3 + 24` # If yes - add one day fi for number in $h3 $m3 $s3 # Loop through numbers... do if [ $number -lt 10 ] # If number is single digit... then echo "0$number\c" # ... add leading zero else echo "$number\c" # ... else - don't fi done echo "" # Terminate the string } my main_script.sh has the following . ./functions.sh arg1=start_time //work that is to bedone arg2=completion_time
execution_time arg1 arg2
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: aigles
Date: February 24, 2004 at 05:42:15 Pacific
Subject: Shell Scripting -DateTime Manipulat
|
Reply: (edit)seems to be Ok. If your shell is 'ksh' or 'bash' don't use 'expr'. Instead of : s3=`expr $s3 + 60` m3=`expr $m2 - $m1` you can do : (( s3 += 60 )) (( m3 = m2 - m1 )) Jean-Pierre.
Report Offensive Follow Up For Removal
|
|
Response Number 9
|
Name: pmjain
Date: February 24, 2004 at 19:18:41 Pacific
Subject: Shell Scripting -DateTime Manipulat
|
Reply: (edit)Hi But when i run the following in the main script . ./functions.sh arg1=start_time //work that is to bedone arg2=completion_time execution_time arg1 arg2 arg1 does not consist of the date time and hence the error comes that numeric argument is expected for the execution_time can u please let me know if its possible to return time type of data from the start_time function that i have specified in the previous communication thanks in advance
Report Offensive Follow Up For Removal
|
|
Response Number 10
|
Name: aigles
Date: February 24, 2004 at 23:25:44 Pacific
Subject: Shell Scripting -DateTime Manipulat
|
Reply: (edit)The correct syntax for arg1 and arg2 assignment is :
arg1=`start_time` arg2=`completion_time`
Jean-Pierre.
Report Offensive Follow Up For Removal
|
|
Response Number 11
|
Name: pmjain
Date: February 25, 2004 at 00:44:07 Pacific
Subject: Shell Scripting -DateTime Manipulat
|
Reply: (edit)Thanks a lot Jean for your help. can you please let know how do i proceed to get more knowledge on shell scripting
Report Offensive Follow Up For Removal
|
Use following form to reply to current message: