Solved

How to use Date and Time in the Unix, Bash shell script?

Posted on 2012-03-22
7
933 Views
Last Modified: 2012-06-22
How to configure Date and Timem in Unix bash chell script and then write the message on the screen or into a file?
Please comment what else neededs to be added (or may be deleted from) to my code below:  

# Date and Time configuration

DATETIMESTAMP= date     
DATESTAMP=$DATETIMESTAMP

echo Date: $DATESTAMP    Time:  $DATETIMESTAMP

Open in new window


Thank you!
0
Comment
Question by:labradorchik
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 37754351
DATETIMESTAMP=$(date)
DATESTAMP=$(date "+%Y/%m/%d")
TIMESTAMP=$(date "+%T")
0
 

Author Comment

by:labradorchik
ID: 37754371
Could you please give an example of writing these two on the screen or into a different file?
Thank you!
0
 
LVL 4

Expert Comment

by:petr_hlucin
ID: 37754375
Use "date" (http://oss.sgi.com/LDP/LDP/sag/x2757.html) command to set current date and time, e.g. (to set date to March 22, 2012 16:30):

date 032216302012
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Assisted Solution

by:petr_hlucin
petr_hlucin earned 50 total points
ID: 37754378
To write to a screen use:
echo $TIMESTAMP

To write into a file use:
echo $TIMESTAMP > /path/to/file
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 200 total points
ID: 37754394
On Screen:
DATETIMESTAMP=$(date)
DATESTAMP=$(date "+%Y/%m/%d")
TIMESTAMP=$(date "+%T")
echo "Date Time Stamp : $DATETIMESTAMP"
echo "Date Stamp : $DATESTAMP"
echo "Time Stamp : $TIMESTAMP"

Open in new window


To a file
DATETIMESTAMP=$(date)
DATESTAMP=$(date "+%Y/%m/%d")
TIMESTAMP=$(date "+%T")
echo "Date Time Stamp : $DATETIMESTAMP" >> file
echo "Date Stamp : $DATESTAMP" >> file
echo "Time Stamp : $TIMESTAMP" >> file

Open in new window

0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
ID: 37754395
Your script will not work.

In Unix you basically have to capture the output of the respective commands if you want to have it in a variable.

The syntax to assign the output of a command is

VARIABLE=$( command )

If it's just for displaying - issue the commands directly and that's it.

In your case it's the "date" command which has quite a lot of formatting options:
       %a
            Displays the locale's abbreviated weekday name.
       %A
            Displays the locale's full weekday name.
       %b
            Displays the locale's abbreviated month name.
       %B
            Displays the locale's full month name.
       %c
            Displays the locale's appropriate date and time representation.
            This is the default.
       %C
            Displays the first two digits of the four-digit year as a decimal
            number (00-99). A year is divided by 100 and truncated to an
            integer.
       %d
            Displays the day of the month as a decimal number (01-31). In a
            two-digit field, a 0 is used as leading space fill.
       %D
            Displays the date in the format equivalent to %m/%d/%y.
       %e
            Displays the day of the month as a decimal number (1-31). In a
            two-digit field, a blank space is used as leading space fill.
       %h
            Displays the locale's abbreviated month name (a synonym for %b).
       %H
            Displays the hour (24-hour clock) as a decimal number (00-23).
       %I
            Displays the hour (12-hour clock) as a decimal number (01-12).
       %j
            Displays the day of year as a decimal number (001-366).
       %k

            Displays the 24-hour-clock hour clock as a right-justified,
            space-filled number ( 0 to 23).
       %m
            Displays the month of year as a decimal number (01-12).
       %M
            Displays the minutes as a decimal number (00-59).
       %n
            Inserts a <new-line> character.
       %p
            Displays the locale's equivalent of either AM or PM.
       %r
            Displays 12-hour clock time (01-12) using the AM-PM notation; in
            the POSIX locale, this is equivalent to %I:%M:%S %p.
       %S
            Displays the seconds as a decimal number (00- 59).
       %s
            Displays the number of seconds since January 1, 1970, Coordinated
            Universal Time (CUT).
       %t
            Inserts a <tab> character.
       %T
            Displays the 24-hour clock (00-23) in the format equivalent to
            HH:MM:SS .
       %u
            Displays the weekday as a decimal number from 1-7 (Sunday = 7).
            Refer to the %w field descriptor.
       %U
            Displays week of the year(Sunday as the first day of the week) as
            a decimal number[00 - 53] . All days in a new year preceding the
            first Sunday are considered to be in week 0.
       %V
            Displays the week of the year as a decimal number from 01-53
            (Monday is used as the first day of the week). If the week
            containing January 1 has four or more days in the new year, then
            it is considered week 01; otherwise, it is week 53 of the previous
            year.
       %w
            Displays the weekday as a decimal number from 0-6 (Sunday = 0).
            Refer to the %u field descriptor.
       %W
            Displays the week number of the year as a decimal number (00-53)
            counting Monday as the first day of the week.
       %x
            Displays the locale's appropriate date representation.
       %X
            Displays the locale's appropriate time representation.
       %y
            Displays the last two numbers of the year (00-99).
       %Y
            Displays the four-digit year as a decimal number.
       %Z
            Displays the time-zone name, or no characters if no time zone is
            determinable.
       %%
            Displays a % (percent sign) character.

Open in new window

. The above is from "man date".

A simple version showing e.g. "2012-03-21" is

Datestamp=$(date "+%Y-%m-%d")
echo $Datestamp

and to show the time as e.g. "14:15:16" do

Timestamp=$(date "+%H:%M:%S")
echo $Timestamp

As I said, there are lots and lots of formatting options. . .
0
 

Author Comment

by:labradorchik
ID: 37754656
Thank you everyone!! Script works fine now.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
This article will show, step by step, how to integrate R code into a R Sweave document
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now