Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Finding latest file in dir but getting syntax errors

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
simpltyansh simpltyansh is offline
Registered User
 
Join Date: Mar 2014
Last Activity: 22 May 2017, 2:12 AM EDT
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Code Finding latest file in dir but getting syntax errors

I believe there are couple of syntax issues in my script, couldn't find them Linux
can someone help me with fixing it to make it work.


Code:
cd /abcde/
#get the latest filename excluding subdirs
filename=`ls -ltr | grep ^- | tail -1 | awk '{print $8}'`

#get system date and file timestamp and storing in variables
sysDate="$(date)"
fileDate="$(date -r $filename)"

#get day of sys and file dates
sysdateDay= `$(date +%d)`
filedateDay= `$(date -r $filename +%d)`

#get hour of sys and file dates
sysdateHour= `$(date +%H)`
filedateHour= `$(date -r $filename +%H)`

#find difference of days and hours in sys and file dates
daydiff= 'expr $day - $day2'
hourdiff= 'expr $hour - $hour2'

echo "Current date : $sysDate"
echo "File date    : $fileDate"

#checking if day is same but hour difference is more than 2 hrs
if [ $daydiff -eq "0" && $hourdiff -ge "2" ]
then
    echo "the newest file is more than 2 hours old...."

#checking if there is a day difference of 1 or more days
else if [ $daydiff -ge "1" ]
    echo  "the newest file is more than 24 hours old...."

#last file is less than 2 hours old
else
    echo "new files received within 2 hours"
fi

exit 0

Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 27 May 2017, 10:47 PM EDT
Location: NM
Posts: 11,069
Thanks: 526
Thanked 1,057 Times in 980 Posts
consider a shorter bit of arithmetic:

Code:
# the the age of the file using epoch seconds
fileage=$(( $(date +%s) - $(date -r min.h.bak +%s) ))
# age expressed as hours:
echo "hours = $(( $fileage / 3600 ))"
# age in days:
echo "days = $(( $fileage / 86400 ))"

You errors have a space between the variable name and equal symbol in an assign statement

Code:
var ="bad"
var= "bad"
var = "bad"
# no spaces either side of the equals symbol.
var="correct"

You have a logic problem using %H for hours - file going over midnight i.e., yesterday at 2300 and today at 0106 will give you a negative number

Last edited by jim mcnamara; 1 Week Ago at 07:02 AM..
The Following User Says Thank You to jim mcnamara For This Useful Post:
simpltyansh (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
giuliangiuseppe giuliangiuseppe is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 17 May 2017, 6:57 AM EDT
Posts: 77
Thanks: 45
Thanked 2 Times in 2 Posts
Can you send the error message?

Thanks

G
    #4  
Old Unix and Linux 1 Week Ago
simpltyansh simpltyansh is offline
Registered User
 
Join Date: Mar 2014
Last Activity: 22 May 2017, 2:12 AM EDT
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Unix or Linux Question

Quote:
Originally Posted by jim mcnamara View Post
consider a shorter bit of arithmetic:

Code:
# the the age of the file using epoch seconds
fileage=$(( $(date +%s) - $(date -r min.h.bak +%s) ))
# age expressed as hours:
echo "hours = $(( $fileage / 3600 ))"
# age in days:
echo "days = $(( $fileage / 86400 ))"

You errors have a space between the variable name and equal symbol in an assign statement

Code:
var ="bad"
var= "bad"
var = "bad"
# no spaces either side of the equals symbol.
var="correct"

You have a logic problem using %H for hours - file going over midnight i.e., yesterday at 2300 and today at 0106 will give you a negative number
Does min.h.bak mean the filename in your suggestion ?
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[Solved] Finding the latest file in a directory abhi_123 UNIX for Dummies Questions & Answers 5 11-16-2012 07:13 AM
Finding the Latest file in a Dir filter Shell Programming and Scripting 2 06-24-2011 11:54 AM
RSYNC syntax for pushing file with latest system date tfort73 Shell Programming and Scripting 4 08-12-2009 01:27 PM
finding latest file having timestamp on it..... kaushik25 Shell Programming and Scripting 1 08-06-2007 10:35 PM
finding latest file in Unix nick12 UNIX for Dummies Questions & Answers 2 03-05-2005 01:27 AM



All times are GMT -4. The time now is 04:49 AM.