Tuesday, October 15, 2013

Check if Two Files Are Empty and if It is a Monday

www.unixbabuforum.inI need help in creating an if statement which checks if 2 files are empty and check if it is a Monday. 
given below command is not working 
if [[! -s file1]]&&[[! -s file2]]&&[[$(date '+%a')="Mon"]] then 

echo "Files are empty on monday " |$T 
return 0 

fi 
Please help me to correct the statement

www.unixbabuforum.ineither put ";" before "then" or insert on new line 

if [[ ! -s file1 ]]&&[[ ! -s file2 ]]&&[[ $(date '+%a')="Mon" ]]; 
then 

echo "Files are empty on Monday " 
return 0 

fi 

NOTE: return must be in function.....
www.unixbabuforum.inThe syntax should be: 

if [ ! -s test1 ] && [ ! -s test2 ] && [ $(date +%a) = Mon ] 
then 
echo "Files are empty on Monday 
fi 

Note - this will return false is any one of the clauses fails to meet its test, so it will return false if there is data in either file.

www.unixbabuforum.in1) there are no spaces between the brackets (opening and closing ones. 
2) depending on what is before and after the bracket pairs, it's good or bad syntax, so always put a space before and after bracket pairs. 
3) if you had carefully read Marty's answer, you already had the answer (and this is the best one) 
4) NEVER use date +%a since it answers in the locale. For instance, on my CentOS, yesterday was not "Mon", but "lun" and on my AIX "Lun". Don't think that globalization means english! 
5) date +%w or date +%u will give you "1" for monday (look at the date man page to see the difference 
[[ $(date '+%u') = "1" ]] && [[ ! -s file1 ]] && [[! -s file2]] && echo "etc."

0 comments:

Post a Comment

 
Design by BABU | Dedicated to grandfather | welcome to BABU-UNIX-FORUM