LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-06-2005, 07:05 AM   #1
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Rep: Reputation: 15
Quick Bash Scripting question


I am trying to create a script that makes a directory for the day prior to today.

here is the code so far.

#!/bin/bash



#sets up variables
month=`date +'%m'`
day=$((`date +'%d'`- 1))
year=`date +'%y'`

date="$month.$day.$year"


mkdir /home/$date


It works fine, but the problem is that the date needs a zero in front of it, so that all directories have the same amount of characters. ie todays date would be 05.05.05.

I can get a zero in front of the 6 in 2 ways, but once the day is above 11 it gives me 3 characters. Basically i want the variable to have a length of 2 characters.

Right now i can get 05.5.05 as is, but i need it to be 05.05.05. I also need the 05.10.05 to stay like that and not have more than 2 characters in each field. I know it may not make sense, but I am unable to find how to make a variable have to return 2 characters only.

Thanks

Last edited by Kedelfor; 05-06-2005 at 07:07 AM.
 
Old 05-06-2005, 07:20 AM   #2
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
Try something like this, after first setting day, month and year:

Code:
if [ $month -le 9 ]; then
    month="0$month"
fi
# And do the same for the others
Incidentally, have you considered what will happen if you run on the 1st of a month?
 
Old 05-06-2005, 07:22 AM   #3
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Original Poster
Rep: Reputation: 15
No i didn't, but thank you for pointing that out. Any idea how I could handle that?
 
Old 05-06-2005, 07:40 AM   #4
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Just use this
Code:
day=`date -d yesterday +%d`
for the day

Last edited by bathory; 05-06-2005 at 07:42 AM.
 
Old 05-06-2005, 07:42 AM   #5
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Original Poster
Rep: Reputation: 15
I found a good way to do it, but thatnks for your help.

date=`date --date yesterday +'%m.%d.%y'`

This seems to be supported by the gnu version of date. I believe it should be on most if not all linux distros. If I am wrong someone please let me know. Thank you again for your prompt reply.
 
Old 05-06-2005, 07:52 AM   #6
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
OK, something like this (hmm, this is more complicated than I thought actually):

Code:
# Set up inital values using date
.....

# Check for the first of January
if [ $day -eq 1 ] && [ $month -eq 1 ]; then
    day=31
    month=12
    let "year=$year-1"
elif [ $day -eq 1 ]
    let "month=$month-1"
    if [ $month -eq 4 ] || [ $month -eq 6 ] || ...continue for all the months with 30 days... || [ $month -eq 11 ]; then
        $day=30
    elif [ $month=2 ]; then
        # Februrary - WARNING this is wrong in leap years
        $day=28
    else
        $day=31
    fi
fi

# Check for length of variables, etc
...
I hope this is some help - I haven't worked out the leap year problem yet though.
 
Old 05-06-2005, 07:53 AM   #7
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
Doh, after all that typing too! Maybe I should learn to read man pages properly
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Scripting Question fiod Linux - Newbie 4 11-19-2005 05:09 AM
bash scripting question Red Squirrel Linux - General 1 07-11-2005 09:57 PM
BASH Scripting question flagg0204 Programming 4 12-23-2004 07:59 AM
Bash Scripting Question Rezon Programming 2 10-30-2003 10:16 AM
Scripting Question (BASH) merana Linux - General 4 01-12-2003 01:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration