Script to get last working date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to get last working date
# 1  
Old 09-07-2015
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.
Code:
  
 #!/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
Code:
 
 date: illegal option --v

Please could a unix expert provide some insights.

Last edited by rbatte1; 09-07-2015 at 12:35 PM.. Reason: code tags by vbe; Added LIST=1 tags to replace text list
# 2  
Old 09-07-2015
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.
Code:
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.
Code:
 ./check_day.ksh
Fri Sep 4 07:58:17 EDT 2015  is the previous day.

Thanks,
R. Singh
# 3  
Old 09-07-2015
Well, i suggest:
Code:
date -v
date: invalid option -- 'v'
Try 'date --help' for more information.

Meaning, remove that invalid option and try again.
# 4  
Old 09-07-2015
Hi Ravinder,

Thanks for the reply. I get the following error.

Code:
Syntax error '==' unexpected

# 5  
Old 09-07-2015
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
# 6  
Old 09-07-2015
HP-UX cadaman B.11.31 U ia64

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

# 7  
Old 09-07-2015
I wonder if its not your shell syntax... It is maybe expecting != for a "Not equal" and = for equal ( or "matches" as you prefer...)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Date Validation not working

Hi Experts, I have a date validation script in that i will validate the date for a given format and search in the logs for that date. The script logic is very simple like below. Validate_Date() { is_valid=1 while do date_format=$(date "+$1") echo -e "Please enter the $2 date like... (6 Replies)
Discussion started by: senthil.ak
6 Replies

2. Shell Programming and Scripting

Date command is not working properly

Hi, in my script, i take the last month by a=$(date --date '1 month ago' +%Y%m) i expect that it give me in this month "March" as result 201402, but linux gave me 201403. IMPe@ABC123:> ~/date --date '1 month ago' +%Y%m 201403 i'm reasonably confused. Any idea? Thanks in advance, ... (2 Replies)
Discussion started by: IMPe
2 Replies

3. Shell Programming and Scripting

Date Format not working in python script

Good Morning, I am working on a pyhton script, where it has to fetch a file from our vendor's server over an ftp. The file is having a date format of "MMDDYYYY" (i have included in my python script), but when the script is executed it is unable to fetch the file. At the same time, if i... (1 Reply)
Discussion started by: AReddy
1 Replies

4. Shell Programming and Scripting

[Solved] Working with date (add minutes using variables)

Dear all, today I'm scratching my head with a simple (I believe) issue. Working with date is quite simple, so if I Need to add some seconds to current time, I'll use: date --date='+30 seconds' +"%Y-%m-%d %H:%M:%S"But, how to pass the value to add from a variable? I tried the following without... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

5. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

6. Shell Programming and Scripting

Script is not working from cron while working manually

Hello, I am facing a very strange problem when I run my script manuallu ./Fetchcode which is using to connect with MKS integrity from linux end it workks fine but when I run it from cron it doesn't work.Can someone help me 1) How could I check my script when it is running from cron like... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

7. Shell Programming and Scripting

date command not working

hi #!usr/bin/perl -w local ($date) = `/sbin/date "+%D %X" ` ; print $date when i run this in ksh shell it is giving the below error sh: /sbin/date: not found but same code is working and displaying date and time in sh shell. what could be the reason. pls help (10 Replies)
Discussion started by: psthariharan
10 Replies

8. Shell Programming and Scripting

Working with bash and date

Hello all, I'm trying to substract 1 minute from the current date and take the hour and minute (for filename purpose). 1) If I want hour an minute from current time I can use: timetmp=$(date +"%H:%M") 2) To substract 1 minute from current time I can use: timetmp=$(date --date "$dte -1... (8 Replies)
Discussion started by: Lord Spectre
8 Replies

9. UNIX for Dummies Questions & Answers

Date command to obtain the last month is not working correctly..

Hello, I could not find the exactly same post here.. so I will explain what I did to get the last month using date command. I used date +%Y-%m -d "-1 months" to get the last month. However, the returned value of above command on 2009/10/31 was 2009 10 and not 2009 09.. and the... (9 Replies)
Discussion started by: tigersk
9 Replies

10. Shell Programming and Scripting

working with date

I wan to convert any field having date in the file from the follwoing format 5/1/2003 to 2003-05-01. This file is tab delimited. Please help. (2 Replies)
Discussion started by: dsravan
2 Replies
Login or Register to Ask a Question