Visit Our UNIX and Linux User Community


Date to be displayed in two digits


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Date to be displayed in two digits
# 1  
Old 08-06-2009
Date to be displayed in two digits

I want the date to be displayed in two digits.

I have written a script to calculate yesterday's date. I'm getting the yesterday's date as 2009085.

Desired output:

20090805


Find the script below:

#!/bin/ksh
### Script to get Yesterday Date #############
Y=`date +'%Y'`
M=`date +'%m'`
D=`date +'%d'`
today=$Y$M$D
echo $today
if [ $M -eq 01 -o $M -eq 03 -o $M -eq 05 -o $M -eq 07 -o $M -eq 08 -o $M -eq 10 -o $M -eq 12 ] && [ $D -eq 01 ];then
yesterday=$Y$M`expr $D + 30`
echo $yesterday
else
if [ $M -eq 04 -o $M -eq 06 -o $M -eq 09 -o $M -eq 11 ] && [ $D -eq 01 ]; then
yesterday=$Y$M`expr $D + 29`
echo $yesterday
else
if [ $M -eq 02 -a $D -eq 01 ]; then
yesterday=$Y$M`expr $D + 27`
echo $yesterday
else
yesterday=$Y$M`expr $D - 1`
echo $yesterday
fi
fi
fi


Thanks
# 2  
Old 08-06-2009
Hi.

Perhaps you could try:

Code:
yesterday=$Y$M$(printf "%02d" $(expr $D - 1))

# 3  
Old 08-06-2009
Or if you are in GMT timezone:

Code:
#!/bin/ksh
echo "Yesterday : $(TZ=GMT24BST;date +'%Y%m%d')"
echo "Today     : $(date '+%Y%m%d')"
echo "Tomorrow  : $(TZ=GMT-24BST;date '+%Y%m%d')"

Yesterday : 20090805
Today     : 20090806
Tomorrow  : 20090807

# 4  
Old 08-06-2009
If the version of Korn Shell that you are using is ksh93, there is a much simplier way of doing it
Code:
$ printf "%(%Y%m%d)T\n" now
20090806
$ printf "%(%Y%m%d)T\n" yesterday
20090805

# 5  
Old 08-06-2009
Does anyone actually use ksh93? It has some nice features, but it's never the default, never standard, and Solaris doesn't even ship with it (certainly none of the Solaris servers I look after has it) - which doesn't bode well for portability if you use it.

I'd be interested to know if it has a place.
# 6  
Old 08-06-2009
Rarely used in portable shell scripts.
# 7  
Old 08-11-2009
Quote:
Does anyone actually use ksh93? It has some nice features, but it's never the default, never standard, and Solaris doesn't even ship with it (certainly none of the Solaris servers I look after has it) - which doesn't bode well for portability if you use it.
Actually it is the default shell on the latest version of OpenSolaris and Solaris. And Solaris has shipped with it for years. Just man dtksh. CDE relies on it.

And the Posix shell standard was mostly based on ksh93.

And if you want floating point support within a shell ....

Last edited by fpmurphy; 08-11-2009 at 01:37 PM..
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #570
Difficulty: Medium
The C programming language allows you to pass a parameter to a function by providing its memory address instead of the value stored in it.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

3. Shell Programming and Scripting

Suppressing a message from being displayed

I have a script which checks for *.txt files in a particular directory and if no files were found then it goes into sleep for 10 secs and looks back for files again and if any files were found then the script does some processing with the files found, which is my requirement too. FILE_EXISTS=`ls... (5 Replies)
Discussion started by: vpv0002
5 Replies

4. Shell Programming and Scripting

password getting displayed using sudo

Hi While doing the following command password is gettin dispalyed : ssh <host> "sudo command ; exit" .... while i type my password for 2nd its gettin displayed ... i tried stty -echo and stty echo ... still i am havin problem..:confused: (1 Reply)
Discussion started by: ningy
1 Replies

5. Shell Programming and Scripting

help: single digits inflated to 2 digits

Hi Folks Probably an easy one here but how do I get a sequence to get used as mentioned. For example in the following I want to automatically create files that have a 2 digit number at the end of their names: m@pyhead:~$ for x in $(seq 00 10); do touch file_$x; done m@pyhead:~$ ls file*... (2 Replies)
Discussion started by: amadain
2 Replies

6. Shell Programming and Scripting

how to sort date in decimal values uptp two digits

Hi all, there is a data in a file wich loks likes 00:00:49|24.48| 00:01:49|22.83| 00:02:49|22.07| 00:03:49|20.72| 00:04:49|21.28| 00:05:49|21.22| 00:06:49|21.38| 00:07:49|20.93| 00:08:49|21.27| 00:09:49|20.65| 00:10:49|19.42| 00:11:49|21.93| 00:12:49|20.62| 00:13:49|20.23|... (3 Replies)
Discussion started by: jojo123
3 Replies

7. Shell Programming and Scripting

Data displayed in two lines

Hi, I am a newbie to both Linux and this forum. I was trying to pull out the data from a database but it is not showing up in the multiple lines, with my limited knowledge i linux u tried to format this but wasn't exactly getting what i wanted. The screenshot below shows my problem. ... (2 Replies)
Discussion started by: Ninjaa
2 Replies

8. Shell Programming and Scripting

Password is getting displayed

My shell script which runs on solaris has an execution in it. Contents of shell script BEGIN : Executable_filename username/password@DBinstance parameter2 parameter3 Contents of shell script END : When i launch the shell script, it connects to database to gets few details.. In the... (3 Replies)
Discussion started by: shafi2all
3 Replies

9. HP-UX

swlist is not displayed in HP-UX

Hi swlist command is not displayed in HP-UX ..it displays swlist: Command not found. uname -a HP-UX inccishh B.11.11 U 9000/800 4046719263 unlimited-user license :b: Best Regards vasanthan (4 Replies)
Discussion started by: vasanthan
4 Replies

10. Solaris

hostname not displayed

Hi, I posted a problem earlier on on how to change IP addresses on an E450. I was able to work around this and wanna say thanks to all who assisted. But now on my server, I get this error, trying to configure hosts..RPC timed out. Also, when I type on the command line, hostname, nothing is... (7 Replies)
Discussion started by: Ronny
7 Replies

Featured Tech Videos