LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > AIX
User Name
Password
AIX This forum is for the discussion of IBM AIX.
eserver and other IBM related questions are also on topic.

Notices


Reply
  Search this Thread
Old 04-01-2014, 11:36 PM   #1
azheruddin
Member
 
Registered: Dec 2011
Posts: 91
Blog Entries: 1

Rep: Reputation: Disabled
wanted to pass previous month number argument to the script


Hello

Iam scheduling one script which takes a previous month as a argument like..

Iam running on 22nd of Mar then it will take

Sh test.sh -f 02 2014

for if iam running on 22 April then

sh test.sh -f 03 2014

How to get the previous month number through command iam doing for the curret month iam using like..

sh test.sh -f fuelevts_fdm `date '+%m'` `d ate '+%Y'` but for previous month

Any Expert a tricky code.

Note: Flaour is AIX.
 
Old 04-02-2014, 01:09 AM   #2
anotherlinuxuser
Member
 
Registered: Jan 2007
Location: Alberta Canada
Distribution: Fedora/Redhat/CentOS
Posts: 70

Rep: Reputation: 19
I would suggest doing the conversion to last month inside the test.sh script. Keep in mind you must decrement the year if the current month is January.

# For example, with command line: sh test.sh -f fuelevts_fdm `date '+%m'` `date '+%Y'`
# read in command line args into vars $MONTH and $YEAR:
[ "$3" ] && MONTH="$3"
[ "$4" ] && YEAR="$4"

# You could then set initial values for MONTH and YEAR, if none were given on command line:
[ "$MONTH" ] || MONTH=`date '+%m'`
[ "$YEAR" ] || YEAR=`date '+%Y'`

# Now calc last month and year, if required.
if [ "$MONTH" = "01" ]; then # January is the only special case.
MONTH=12
(( YEAR = $YEAR - 1 ))
# I am not sure the (( .. )) works in AIX, but if it doesn't, this will:
# YEAR=`expr $YEAR - 1`
else
(( MONTH = $MONTH - 1 ))
# MONTH=`expr $MONTH - 1`
[ "$MONTH" -lt 10 ] && MONTH="0$MONTH" # Pad the month with a zero, for consistency with date command.
fi
echo "Month=$MONTH, Year=$YEAR"

Last edited by anotherlinuxuser; 04-02-2014 at 01:21 AM.
 
Old 04-02-2014, 02:30 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Many implementations of the date command allow you to specify a date to use, not sure about AIX but you could try:

Code:
date -d "last month" +%m
Which should return the last month based on the current date.
 
Old 04-03-2014, 01:58 AM   #4
azheruddin
Member
 
Registered: Dec 2011
Posts: 91

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Thanks all, The first is the most sbvious solution for this problem

AIX doesnt have capabilty to execute date -d.
 
Old 04-03-2014, 02:21 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Moved: This thread is more suitable in AIX forum and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 04-03-2014, 05:08 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
When installing GNU coreutils, be careful not to overwrite the original programs. For example, install GNU-date as /usr/local/bin/date, and also make a symlink to it: ln -sf date /usr/local/bin/gdate, then call it:

Code:
PATH="/usr/local/bin:$PATH"
gdate -d "last month" +%m
 
Old 04-04-2014, 05:12 PM   #7
wingnut64
Member
 
Registered: Sep 2004
Distribution: AIX, RHEL, Ubuntu
Posts: 51

Rep: Reputation: 23
If you don't want to install GNU date you can cheat and not do any date math by just taking the numerical month from 'date' and subtracting 1. Then you just test for and change 0->12 so it will properly wrap on January.

This works in ksh93; I didn't test it on my work boxes but it should work fine on the stock korn shell AIX ships with.
Code:
typeset -i lastmonth=$(( $(date '+%m') - 1))
[ $lastmonth -gt 0 ] || lastmonth=12

Last edited by wingnut64; 04-04-2014 at 05:14 PM.
 
Old 04-05-2014, 07:59 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Using Bourne shell and a single math expression to manage January to December transformation, you can try something like this:
Code:
m=`date +%m`
m=`expr 12 \* \( $m == 1 \) + $m - 1`
 
  


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
How to pass an argument to a variable that has been assigned a script? abhiJ Linux - Newbie 9 10-25-2013 02:30 AM
How do I pass wildcard as an argument to a shell script? Jykke Programming 8 03-09-2013 10:33 AM
How do I pass complicated commands (With bar's) as an argument to another script? theaceoffire Programming 8 04-22-2010 09:40 AM
Howto pass an argument to alias of a script? Goose1 Programming 1 02-17-2008 06:14 PM
PHP pass argument to shell script monzter Programming 2 08-14-2004 06:16 AM

LinuxQuestions.org > Forums > Other *NIX Forums > AIX

All times are GMT -5. The time now is 05:17 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