<p>What is the most elegant way to calculate the previous business day in shell ksh script ?</p>

<p>What I got until now is :</p>

<pre><code>#!/bin/ksh

set -x

DAY_DIFF=1
case `date '+%a'` in
"Sun")
   DAY_DIFF=2
   ;;
"Mon")
   DAY_DIFF=3
   ;;
esac

PREV_DT=`perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time()-${DAY_DIFF}*24*60*60);printf "%4d%02d%02d",$year+1900,$mon+1,$mday;'`

echo $PREV_DT
</code></pre>

<p>How do I make the ${DAY_DIFF} variable to be transmitted as value and not as  string ?</p>

Well, if running Perl counts as part of the script, then develop the answer in Perl.  The next question is - what defines a business day?  Are you a shop/store that is open on Sunday?  Saturday?  Or a 9-5 Monday to Friday business?  What about holidays?
Assuming you're thinking Monday to Friday and holidays are temporarily immaterial, then you can use an algorithm in Perl that notes that wday will be 0 on Sunday through 6 on Saturday, and therefore if wday is 1, you need to subtract 3 * 86400 from time(); if wday is 0, you need to subtract 2 * 86400; and if wday is 6, you need to subtract 1 * 86400.  That's what you've got in the Korn shell stuff - just do it in the Perl instead:
```
```
#!/bin/perl -w
use strict;
use POSIX;
use constant SECS_PER_DAY => 24 * 60 * 60;
my(@days) = (2, 3, 1, 1, 1, 1, 1);
my($now) = time;
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($now);
print strftime("%Y-%m-%d\n", localtime($now - $days[$wday] * SECS_PER_DAY));
```
```
This does assume you have the POSIX module; if not, then you'll need to do roughly the same printf() as you used.  I also use ISO 8601 format for dates by preference (also used by XSD and SQL) - hence the illustrated format.