Creating a function for timestamp in shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Creating a function for timestamp in shell script
# 1  
Old 04-07-2009
Creating a function for timestamp in shell script

I have the following code for installing a BSD application stack:
Code:
#!/bin/sh

# install dos2unix and unix2dos utilities
echo `date "+%Y-%m-%d %H:%M:%S"` "unix2dos and dos2unix: installing..." >> ~/post_install.log
pkg_add -r unix2dos

# install xfce4 desktop environment
echo `date "+%Y-%m-%d %H:%M:%S"` "xfce: installing..." >> ~/post_install.log
pkg_add -r xfce4

I would like the timestamp to be recalculated so that the log shows actual timings. But a function should be created instead for efficiency purposes, say datetime:
Code:
datetime() { echo `date "+%Y-%m-%d %H:%M:%S"` }

so that the following follows
Code:
#!/bin/sh

# install dos2unix and unix2dos utilities
echo datetime "unix2dos and dos2unix: installing..." >> ~/post_install.log
pkg_add -r unix2dos

# install xfce4 desktop environment
echo datetime "xfce: installing..." >> ~/post_install.log
pkg_add -r xfce4

But then the literal 'datetime' shows up in the post_install log. What is the exact syntax to use here?
# 2  
Old 04-07-2009
Quote:
Originally Posted by figaro
[...]
I would like the timestamp to be recalculated so that the log shows actual timings. But a function should be created instead for efficiency purposes
I don't see how a function that performs the same
code as the command substitution could be more efficient.


Quote:
But then the literal 'datetime' shows up in the post_install log. What is the exact syntax to use here?
You still need command substitution:

Code:
echo $(datetime)

P.S. I believe the correct syntax is:

Code:
datetime() { echo `date "+%Y-%m-%d %H:%M:%S"` ;}

Or:

Code:
datetime() { 
  echo `date "+%Y-%m-%d %H:%M:%S"` 
  }

# 3  
Old 04-07-2009
Thanks, your suggestion worked very well. On the efficiency note: I was referring to refactoring, not memory usage or performance.
# 4  
Old 04-07-2009
Quote:
Originally Posted by figaro
Thanks, your suggestion worked very well. On the efficiency note: I was referring to refactoring, not memory usage or performance.
Agreed, that makes sense.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script Help..Renaming Quoted files removing the timestamp

Hi all, i am new to this forum, unix and shell scripting. I would really appreciate if you all can help me here.. I have files coming in the below format 'filename20513'13May06:03:45 filename are characters.. like 'ABDDUT20513'13May06:03:45 i need it to be renamed as... (17 Replies)
Discussion started by: khman
17 Replies

2. Shell Programming and Scripting

How to sort the timestamp in the filename in shell script?

originally the shellscript #ln_file_name=`echo $ld_interface_date"_"${8}".csv"` #ln_file_name=`echo 201202011527_HL_HLTM1_B04A.csv` ln_file_name="*"`echo ${7}".csv"` get_file_list_1=$log_path"tm1_file_list.gfl1" cd ${source_path} echo "Try to find any file exist in the... (10 Replies)
Discussion started by: feilhk
10 Replies

3. UNIX for Dummies Questions & Answers

How to convert R$Timestamp in Sql*Plus within a UNIX Shell Script?

I need to compare a R$Timestamp field sql within a Unix Shell Script. In straight SQL the following code works fine: Table Name: LL_UNIT_TRANSACTION UT Field: R$Timestamp Where TRUNC(UT.R$Timestamp) >= TRUNC(SYSDATE -7) the following returns no data within the Unix Shell Script... (2 Replies)
Discussion started by: Dapconsult
2 Replies

4. Shell Programming and Scripting

Help creating a timestamp script to record mem usage

Hi, I'm looking into doing a few performance tweaks by adjusting my max memory on a few lpars. I would to create a time stamp script so i could review it for a week and determine how much space i can lower my max memory to so i could reclaim and allocate that memory to where it is needed the... (2 Replies)
Discussion started by: vpundit
2 Replies

5. Shell Programming and Scripting

How to get the timestamp for file using shell script

I am writing a script to move the executable from one node to another and take the backup of the existing one. For that I want to fetch the timestamp of file using shell script. Could anyone help? (2 Replies)
Discussion started by: gagankinra
2 Replies

6. Shell Programming and Scripting

Call Shell Function from mysql timestamp

Hi all, Actually my aim is to call the shell script when ever there is a hit in a mysql table which consist of 3 values. Acter some research I came to know that it is not possible and can achive with timestamp. Can someone please tell me how to read the table timestamp which should done... (3 Replies)
Discussion started by: santhoshvkumar
3 Replies

7. Shell Programming and Scripting

Shell Script to Search for a particular String and copy the timestamp to a variable

Hi, We Perfrom Loads to the database through a Perl script which generates a statistics file. I need to read the statistics. the Statistics file looks something like below: Process Beginning - 08-26-2010-23.41.47 DB2 CONNECTION SUCCESSFUL! Ready to process and load file: FILENAME # of... (2 Replies)
Discussion started by: Praveenkulkarni
2 Replies

8. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

9. AIX

Need timestamp conversion shell script !!

Can anyone provide me with a ksh or bash script which will accept a timestamp (format is YYYY-MM-DD-HH24.Mi.Ss) and time offset (in hours). The output will be (timestamp passed - time offset passed deducted from it) in the same YYYY-MM-DD-HH24.Mi.Ss format. Basically I am trying to convert the... (1 Reply)
Discussion started by: shibajighosh
1 Replies

10. Shell Programming and Scripting

shell: creating different arrays based on function argument

hi, I was wondering if there was a good way to create an array within a function, where the name is based on a passed argument? I tried this: _____________________________ func(){ #take in 1st arg as the arrayname arrayName=$1 let i=0 while read line do arrayName=${line} let i+=1... (5 Replies)
Discussion started by: nix21
5 Replies
Login or Register to Ask a Question