Computing.Net > Forums > Programming > unix script help

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

unix script help

Reply to Message Icon

Original Message
Name: jds
Date: March 8, 2006 at 16:17:18 Pacific
Subject: unix script help
OS: windowsXP
CPU/Ram: petium III/ram 256
Comment:

Hi,
I am trying to process the file for next month (if this month is 03 then I'll need to look at 04 files)
I use month = `date + %m` to get value of 03 for this month
If I use month='expr $month + 1` it will give me 4 instead of 04. I then use month=`"0"$month` to covert it to 04.
Also if use my way when it reach to >10 then I'll need to covert again.
I would like to know is there any better way to to this?

I was trying to do like this:
while [condition]; do
something...
#I want add if here like:
month=`date +%m`
month=`$month + 1`
if ($month<10) month=`"0"$month
if ($month=13) month=01
something...
done

for some reason it doesn't work.
anything wrong? Please advice.
Thanks


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: March 8, 2006 at 20:25:15 Pacific
Reply: (edit)

Once you are done with all your arithmetic, use the external printf command (similiar to the "C" version of printf). If the number is only 1 digit it'll prepend a 0:

#!/bin/sh

m=9
m=`printf "%02d\n" $m`
echo $m

n=11
n=`printf "%02d\n" $n`
echo $n


Report Offensive Follow Up For Removal

Response Number 2
Name: Wolfbone
Date: March 8, 2006 at 21:17:44 Pacific
Reply: (edit)

Alternatively, use:

date -d 'next month' '+%m'

if your 'date' command is fully featured.


Report Offensive Follow Up For Removal

Response Number 3
Name: jds
Date: March 9, 2006 at 13:44:21 Pacific
Reply: (edit)

Thank you all for the help.
Nails' way works for me perfect
unfortunately, my 'date' is not fully functioned so I can't use Wolfbone's way

Now I have another question: when I process next month
month=`expr $month + 1`
if $month=13 month=1 -- this one is not working. where I did wrong? it says:" 'if' unmatched"
please advice.

Thanks



Report Offensive Follow Up For Removal

Response Number 4
Name: nails
Date: March 9, 2006 at 22:26:30 Pacific
Reply: (edit)

You don't have the correct if syntax:

month=12
month=`expr $month + 1`
if [ $month == 13 ]
then
month=1
fi
echo $month


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software