Computing.Net > Forums > Programming > unix script help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

unix script help

Reply to Message Icon

Name: jds
Date: March 8, 2006 at 16:17:18 Pacific
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



Sponsored Link
Ads by Google

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

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


0

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

Alternatively, use:

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

if your 'date' command is fully featured.


0

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

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



0

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

You don't have the correct if syntax:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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


Sponsored links

Ads by Google


Results for: unix script help

Unix scripting www.computing.net/answers/programming/unix-scripting-/16383.html

Need help with Unix Scripting www.computing.net/answers/programming/need-help-with-unix-scripting/18035.html

How to read a file using unix script www.computing.net/answers/programming/how-to-read-a-file-using-unix-script/18795.html