Tuesday, October 26, 2010

Get date information from commands.

Get the Date output.
===============
$ date +%d -- Date
26
$ date +%D -- Date format
10/26/10
$ date +%m -- Month
10
$ date +%b -- Month
Oct
$ date +%M -- Minutes
45
$ date +%W -- Week of the year
43
$ date +%w -- Week of the day
2
$ date +%Y -- Year
2010
$ date +%y -- Year (last 2 digits)
10
$ date +%n -- New line
$ date +%l -- hour (0..12)
4
$ date +%k -- hour (0..23)
16
$ date +%N -- nano seconds
855022663
$ date +%T -- time format
16:48:14
-----------------------------------
$ set `date`
$ echo $1
Tue
$ echo $2
Oct
$ echo $3
26
$ echo $4
16:52:45
$ echo $5
IST
$ echo $6
2010
$ echo $# -- Total number of command variables.
6
-----------------------------------------------
date |cut -d" " -f1 # Replace f1 to f2,3,4,5,6 to get the values.
Tue
-------------------------
You can run a loop to check the day and run some commands according using case loop.

case `date |cut -d" " -f1` in
Mon) commands ;;
Tue) commands ;;
Wed) commands ;;
...
esac

No comments: