Script to get last working date

Problem : we need a command that will give previous day date and previous day should be mon-fri only.
Example :if we are running a command on Monday , it will give Friday �s date not Sunday�s .
Few more restrictions :

  1. Command should be working in NM n/w.
  2. Hard coding of date should be avoided.
  3. Command will be running on NM and market holidays also .
  4. Avoid the use of sysin or other static file .

I created a .sh script. The code is given below.

  
 #!/bin/bsh
  
 if [ date '+%w' ==1 ]; then
    date -v -3d + '%Y/%m/%d'
 else
    date -v - 1d + '%Y/%m/%d'
 fi
 

When I execute this script I get the following error

 
 date: illegal option --v
  

Please could a unix expert provide some insights.

Hello Downsouth,

Welcome to forum, please use code tags for commands/inputs/codes which you use in your posts as per forum rules. Following may help you in same.

cat check_day.ksh
if [[ `date -d "1 day ago" +%A` == "Sunday" ]]
then
        PRE_VAL=`date -d "3 day ago"`
elif [[ `date -d "1 day ago" +%A` == "Saturday" ]]
then
        PRE_VAL=`date -d "2 day ago"`
else
        PRE_VAL=`date -d "1 day ago"`
fi
echo $PRE_VAL " is the previous day."
 

Here name of the script is check_day.ksh .
When I execute it on my system following is the output.

 ./check_day.ksh
Fri Sep 4 07:58:17 EDT 2015  is the previous day.
 

Thanks,
R. Singh

Well, i suggest:

date -v
date: invalid option -- 'v'
Try 'date --help' for more information.

Meaning, remove that invalid option and try again.

Hi Ravinder,

Thanks for the reply. I get the following error.

Syntax error '==' unexpected

Hello Downsouth,

Could you please check your date's command version by doing date --version as suggestion I gave will be for GNU dates. Also please mention your os details too(you can hide your box name but can provide version details and os name etc).

Thanks,
R. Singh

HP-UX cadaman B.11.31 U ia64

date --version returned
 date [-u]
 date [-a]
 

I wonder if its not your shell syntax... It is maybe expecting != for a "Not equal" and = for equal ( or "matches" as you prefer...)

Missed that part:

_#!/bin/bsh
  
 if [ date '+%w' ==1 ]; then

Should become:

#!/bin/bash
  
 if [ $(date '+%w') = 1 ]; then

hth

I tried = instead of ==. Even that did not work. My script name is Priorworkday.ksh. When I execute it I get the error interpreter "/bin/bash: not found file link resolves to "/usr/bin/bash"

Well then, use ksh instead of bsh/bash for the shebang.

tried it doesn't work. Date issues start popping up

Did you note that you have a leading space before the shebang?
The leading hash (#) is a must, while the explamation mark (!) is optional.

Try one of these:

#!/bin/ksh
#!/usr/bin/ksh
#!/usr/bin/env ksh

What is the error message regarding the Date issues ?

ty & hth