Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substituting Values for Command Line Arguments

Status
Not open for further replies.

tbohon

Programmer
Apr 20, 2000
293
US
I have a ksh script that I want to run every morning via crontab. It always uses the previous day's date since that is the way the various log files it manipulates are named.

Here is (the start of) my routine to figure out the previous day's date and form the correct filename (I still need to put in end-of-month and end-of-year checks):

Code:
mth=`date +"%m"`
dy=`date +"%d"`
newdy=`expr $dy - 1`

wd=$mth$newdy

The actual checks look something like this:

Code:
if test ! -s /u/carelink/live/queue/oru_in/${1}2005

I'm currently running this script manually every morning so am using ${1} to grab the MMDD value I pass from the command line (e.g., when I ran the script today (4/26) I passed it 0425).

To avoid having to seriously recode a very long script, is there any way to substitute the value I've calculated above (wd) for the ${1} value itself so no coding changes are needed?

Thanks as always for your thoughts.

Tom

"My mind is like a steel whatchamacallit ...
 
${1-$wd}
If argument is passed, returns it else returns the value of wd.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
BTW,

you might want to try this

Code:
wd=$(TZ=ABC23DEF date +'%m%d')

to cheat the system in thinking you are in a timezone 24 hrs before CUT - but just while executing the date +'%m%d' command, no harm to the OS or timekeeping...

I am normally in TZ xxx-1yyy (xxx-2yyy in summertime), so 24 hrs earlier is 23 for me. You may have to adjust the value for your needs. It also depends on when the script is running - early morning or late at night...

Saves you having to code this, accounting for months, days, leapyears... although this can be done in shell scripting if you really want to.
 
for the ${1} value itself
I've reread your question, perhaps this ?
set $wd

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I believe it is (for korn shell)

Code:
 wd=...{code to determine wd automatically}
 set -- ${wd}

=> then you can use $1 to reference wd value

if your script does not yet run the code to determine wd, then do not run the set -- ${wd} and use whatever you specify on the command line using $1


OR (and I prefer this - almost self explanatory)

Code:
if [ $1 != "" ]
then
 wd=$1
else
 wd=... {code to determine wd automatically}
fi

=> then use ${wd} further on in script, that way you can overrule the normal daily run and re-run the script for last week should you need it again...
 
So, my final guess:
set -- ${1-$wd}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Yes,

set -- ${1-$wd}

would do the trick... Of course, in addition to that the script would need a couple of lines of comments to explain exactly what that line is doing and what it is doing it for and why the script then uses $1 further on...

Granted, it is nice and short and precise but a lot of admins when presented with this (if undocumented) might be left clueless... perhaps even if it is documented.
 
Thanks, all ... lots of good ideas here, it's going to make it happen today!

Appreciate the assist!

Tom

"My mind is like a steel whatchamacallit ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top