Problem with copying a date value to another variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with copying a date value to another variable
# 8  
Old 07-21-2010
Hi.

You have the closing backtick in the wrong place, and the echo is not required...
Code:
dte=`echo ls -ltr` | tail -1 | awk '{print $6 $7}' | date +%Y%m%d

Should be:
Code:
dte=`ls -ltr | tail -1 | awk '{print $6 $7}' | date +%Y%m%d`

If [B]

In Linux you can also say:

Code:
dte=$(ls -ltr --time-style=+%Y%m%d | tail -1 | awk '{print $6}')


Last edited by Scott; 07-21-2010 at 12:47 PM.. Reason: I was way off the mark first time!
This User Gave Thanks to Scott For This Post:
# 9  
Old 07-21-2010
Quote:
dte=`echo ls -ltr` | tail -1 | awk '{print $6 $7}' | date +%Y%m%d
echo ${dte}
bate=${dte}
echo bate value is ${bate}
cycle=20100702
if [[ ${bate} -gt ${cycle} ]]
then
echo VALIDATE
else
echo DO NOT VALIDATE
fi
Assuming this not a Linux with an extended format ls (in which case pludi's Linux solution is good), I think that the O/P is assuming that piping the date from a directory listing to the unix "date" command will convert the format of the piped input. It won't.

If the script is intended to find files modified after a particular date held in $cycle, it may be better to "touch" a temporary file with that date and use "find" with the "-type f -newer filename" options.
# 10  
Old 07-22-2010
Thanks Scottn - it worked out successfully now.
Thanks to Methyl too.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

2. Shell Programming and Scripting

Convert a date stored in a variable to epoch date

I am not able to pass date stored in a variable as an argument to date command. I get current date value for from_date and to_date #!/usr/bin/ksh set -x for s in server ; do ssh -T $s <<-EOF from_date="12-Jan-2015 12:02:09" to_date="24-Jan-2015 13:02:09" echo \$from_date echo... (7 Replies)
Discussion started by: raj48
7 Replies

3. Shell Programming and Scripting

Rsync copying within recent date range?

As many will point out, one of the benefits of using rsync (without --delete) is that it will sync files between source and destination, but not delete files in the destination which HAVE been deleted in the source. Well, I have kind of the opposite problem and I'm wondering if there are date... (2 Replies)
Discussion started by: WIOP33
2 Replies

4. Shell Programming and Scripting

Copying a file using variable

Hi this is the code i am using copying a file by using a variable i have a file test.txt which contains 5 lines #!/bin/bash #SCRIPT: method2.sh #PURPOSE: Process a file line by line with redirected while-read loop. FILENAME=test.txt count=0 foldername=OUTPUT mkdir $foldername ... (5 Replies)
Discussion started by: Rami Reddy
5 Replies

5. Shell Programming and Scripting

LINUX ---> Add one date to a date variable

Hi All, I am trying to add one day to the busdt variable i am extracting from a file (DynamicParam.env). I am deriving the busdt as below. Is there any function which I can use to derive as below. If busdt=20120910, then the new derived variable value should be 20120911 If... (2 Replies)
Discussion started by: dsfreddie
2 Replies

6. Shell Programming and Scripting

date output store in variable problem

When I run following command date Output1 => Thu Sep 9 03:26:52 IST 2010 When I store in a varibale as a=`date` echo $a output2 => Thu Sep 9 03:27:02 IST 2010 The differnece is, it is trimming the space when I am storing the output in varibale. Output1 = Thu Sep 9 03:26:52 IST 2010... (2 Replies)
Discussion started by: pravincpatil
2 Replies

7. UNIX for Dummies Questions & Answers

Appending the current date on Copying the file.

Hi I have the data file in the one directory and i have to copy the file in the another directory with the timestamp. EX: /ab/cd/a.dat i need to copy this file to the another directory /ef/gh/. while am copying i need to append the current timestamp in the file like... (3 Replies)
Discussion started by: bobprabhu
3 Replies

8. Shell Programming and Scripting

copying a file without changing date stamp.

Hi, I am using the below copy command, to copy the file sbn to sbn1, cp sbn sbn1 but its changing the date stamp of file sbn1, but i dont want to change the date stamp of sbn1. Could you please help me out in this. (3 Replies)
Discussion started by: shivanete
3 Replies

9. Shell Programming and Scripting

Copying files created after a specified date/time

I need to write a script that copies files from one directory to another that were created after "today 6:30". This script will be NOT be ran at the same time each day. any help is appreciated. (2 Replies)
Discussion started by: jm6601
2 Replies

10. UNIX for Dummies Questions & Answers

Copying files with the latest date

Hi All, I have a situation where I need to copy the files having the latest date. For example I have a file by name bas100e1_jun05. I need to copy it to bas100e1. But when a file by name bas100e1_jul05 is put in the same directory the script should copy the file having the latest month which... (34 Replies)
Discussion started by: shashi_kiran_v
34 Replies
Login or Register to Ask a Question