Incrementing the date depending on the Counter (parameter) passed to the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Incrementing the date depending on the Counter (parameter) passed to the script
# 1  
Old 10-10-2011
Incrementing the date depending on the Counter (parameter) passed to the script

Hello Everyone,

I have been trying to complete a shell script where, I need to increment the date depending on the file (depending on the date) availability on the remote server.
i.e.
Basically, I will be passing a counter (like parameter 1 or 2 or 3 or 4).
First I will check for the availability of 7days file on the remote server.
If the file is not available then depending on the counter, the script should increment the date and check for the files (zip & non-zip) on the remote server.

Code:
For example:
Day=`date -d'1 week ago' +%Y%m%d`
bd=$Day

If the script is running on 10/14/2011, then I am calculating the Day value as 10/07/2011 and the file name as 
file='fixed.out.'$bd.'gz' i.e. file='fixed.out.20111007.'gz'
ssh  remote_cmp 'ls -ltr /home/brittany/$file'  --> to check whether the file is available on the remote server.

Code:
if [[ $? -ne 0 ]]
then
<1.If the counter (parameter) is 1, I need to increment the date to +1 i.e. the new date would be 10/08/2011 and 
     then check if the file is available (check for zip file and non zip file) on the remote host.
2. If the counter is 2, I need to increment the date to +2 i.e. the new data would be 10/09/2011 and 
      then check if the file is available (check for zip file and non zip file) on the remote host.
3. ..... so on until counter is 4 >

Could someone please share the thoughts how can I increment the date, i.e. set the new date to the variable and
then check for the files on the remote host.

Really appreciate your thoughts.

Last edited by jim mcnamara; 10-10-2011 at 05:49 PM.. Reason: truncate long lines
# 2  
Old 10-11-2011
since your command date supports -d option, you can set the 7 days by this way.

Code is not tested
Code:
for d in 1 2 3 4 5 6 7
do
  Day=`date -d "$d day ago " +%Y%m%d`
  file="fixed.out.$Day"
  if [ -f "$file.gz" ]; then 
      echo "$file.gz is exist"
  esle
      if  [ -f "$file" ]; then
          echo "$file is exist"
      else
          echo "file.gz and $file are both not exist"
      fi
  fi
done


Last edited by rdcwayx; 10-11-2011 at 02:26 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Resolving a parameter which is passed as parameter

Hi, I have the following files. ->cat scr.sh export TMP_DIR=/home/user/folder1 export TMP_DIR_2=/home/user/folder2 while read line do cat "$line" done<file_list.dat ------------------------ -> cat file_list.dat $TMP_DIR/file1.txt $TMP_DIR_2/file2.txt --------------------------- -> cat... (6 Replies)
Discussion started by: barath
6 Replies

2. Shell Programming and Scripting

Command that takes one parameter and then searches for the passed in parameter

Hi I am looking for a unix command or a small shell script which can takes one parameter and then searches for the passed in the parameter in any or all files under say /home/dev/ Can anyone please help me on this? (3 Replies)
Discussion started by: pankaj80
3 Replies

3. UNIX for Dummies Questions & Answers

checking if parameter passed is a number

I have written a function that fills an array and another function where if a parameter is supplied it will jump to that part of the array and cat it to the screen. I need to put in some checks to make sure the parameter supplied is firstly a number and then not a number great than the length of... (2 Replies)
Discussion started by: magnia
2 Replies

4. Shell Programming and Scripting

How can i passed a parameter to a file?

i got a file called Pass1, and then i need to passed a number to my script with the '-p pass_mark' option. Example type Pass1 -p 18 to pass 18 to my script for comparing things, so how can i do it? (7 Replies)
Discussion started by: mingming88
7 Replies

5. Shell Programming and Scripting

Detecting Doublequotes(") When passed as parameter to shell script

Hi, I am executing a shell script which takes a string as a parameter. The scipt should validate the string and create the directoy with the name of specfied string. The following is the specified command and its parameter. test.sh "abc abc" The shell script is not able to identify... (4 Replies)
Discussion started by: raghu.amilineni
4 Replies

6. Shell Programming and Scripting

How to create incrementing counter

i am using SCO OpenServer 5 I wan to use bash and have a incrementing counter. e.g. #!/bin/bash counter=1 let "counter+=1" echo $counter It says that it does not recognise let So how should i do it? (5 Replies)
Discussion started by: khaos83_2000
5 Replies

7. Shell Programming and Scripting

how to find the path of a file when it is passed as ....filename(parameter) to script

hi unix guru's..................:confused: question is posted in the #3 permalink shown below. (3 Replies)
Discussion started by: yahoo!
3 Replies

8. Shell Programming and Scripting

Using the counter of a for loop in command line parameter

Say I have (in psuedocode) For i=1 to 10 tar cvfb /... 5*i /junk(i) end What I mean is that I want each successive for loop to have the block size parameter be 5 times the current counter. This isn't my actual code, just a stupid example...So the question is how do I descrive that parameter... (2 Replies)
Discussion started by: jeriryan87
2 Replies

9. UNIX for Dummies Questions & Answers

checking parameter values passed to script

Hi, I will pass 3 parameters for a script.I have to check the file name and create a new file name with time stamp. the parameters which i'm passing are /dir/stg/filename.txt /dir/path/head.txt /dir/path/tail.txt Now i have to check filename like : if it is a.txt i have to create... (2 Replies)
Discussion started by: ammu
2 Replies

10. Shell Programming and Scripting

manipulate a passed in parameter

I am passing a file name as a parameter to shell script the parameter is getfile.txt.gpg how do i process this parameter to get name like getfile.txt only and eleminate the .gpg text?? Thanks in advance (2 Replies)
Discussion started by: rudoraj
2 Replies
Login or Register to Ask a Question