Files validation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Files validation
# 1  
Old 10-01-2012
Files validation

Hi,

I want a script to do the following:

1)I want to find all latest files with extension *.doc and write those files to a log file which i did.
my script: find *.doc -mtime -1 -ls > test.log

2) suppose if there is no such latest files extracts script should write "Server password expires" to log file(test.log)

your prompt help will be much appreciated please.
# 2  
Old 10-01-2012
Quote:
Originally Posted by sv0081493
Hi,

I want a script to do the following:

1)I want to find all latest files with extension *.doc and write those files to a log file which i did.
my script: find *.doc -mtime -1 -ls > test.log

2) suppose if there is no such latest files extracts script should write "Server password expires" to log file(test.log)

your prompt help will be much appreciated please.

Code:
find *.doc -mtime -1 -ls > test.log

if [ $? -ne 0 ]; then
  echo "Server password expires" >> test.log
fi

This User Gave Thanks to the_gripmaster For This Post:
# 3  
Old 10-01-2012
Find does not return an error when no files are found. Testing the rc ($?) will not help very much.

The log file size will be zero when nothing is found, test that instead:
Code:
find *.doc -mtime -1 -ls > test.log

if [ ! -s test.log ]; then
  echo "Server password expires" >> test.log
fi

This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 10-01-2012
Hi,

Thanks a lot for your prompt response.
I want to understand the concept here for below commands and its use.Could you please help
if [ $? -ne 0 ]; then
# 5  
Old 10-01-2012
Quote:
Originally Posted by jim mcnamara
Find does not return an error when no files are found. Testing the rc ($?) will not help very much.

The log file size will be zero when nothing is found, test that instead:
Code:
find *.doc -mtime -1 -ls > test.log

if [ ! -s test.log ]; then
  echo "Server password expires" >> test.log
fi

I am running Bash 4.x and find (find (GNU findutils) 4.4.2) does return exit code of 1 when the file is not found.

---------- Post updated at 09:10 PM ---------- Previous update was at 09:09 PM ----------

Quote:
Originally Posted by sv0081493
Hi,

Thanks a lot for your prompt response.
I want to understand the concept here for below commands and its use.Could you please help
if [ $? -ne 0 ]; then
$? contains the exit code of the previous run command. For a success, $? is 0 and for a fail $? is 1. However, there are some gotchas but that's the simple rule.
# 6  
Old 10-01-2012
Quote:
Originally Posted by the_gripmaster
I am running Bash 4.x and find (find (GNU findutils) 4.4.2) does return exit code of 1 when the file is not found.
Nope. From man find:
Code:
EXIT STATUS
       find  exits with status 0 if all files are processed successfully, greater than 0 if errors occur.   This is deliberately
       a very broad description, but if the return value is non-zero, you should not rely on the correctness of the  results  of
       find.

Code:
$ find --version
find (GNU findutils) 4.5.9

$ ls yup
ls: cannot access yup: No such file or directory

$ find . -name 'yup' && echo FOUND || echo NOT FOUND
FOUND

Does that mean that the file named yup exists?
This User Gave Thanks to elixir_sinari For This Post:
# 7  
Old 10-01-2012
Quote:
Originally Posted by elixir_sinari
Nope. From man find:
Code:
EXIT STATUS
       find  exits with status 0 if all files are processed successfully, greater than 0 if errors occur.   This is deliberately
       a very broad description, but if the return value is non-zero, you should not rely on the correctness of the  results  of
       find.

Code:
$ find --version
find (GNU findutils) 4.5.9

$ ls yup
ls: cannot access yup: No such file or directory

$ find . -name 'yup' && echo FOUND || echo NOT FOUND
FOUND

Does that mean that the file named yup exists?
Ok, I am wrong.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Validation using While and IF

I am learning Shell scripting on own. I am trying to do an assignment to get details from the user like username their individual marks ,DOB and send a report in mail with the Details calculated like total and average. validate_marks() { local Value=$1 if && then return 0 else... (1 Reply)
Discussion started by: JayashreeRobin
1 Replies

2. What is on Your Mind?

Date validation

Hi folks, I new to shell script . I want to know how to validate a String as valid date example: 20150712 ---> valid date 20160524-->valid 201605T12-->invalid date 12341234--->invalid date we need to valid string( yyyymmdd) to date in SunOS 5.10 please give some idea to validate... (9 Replies)
Discussion started by: srinadhreddy27
9 Replies

3. UNIX for Dummies Questions & Answers

Date column validation in files

HI, i have to do some file validations on which i am receving daily.could any one please let me know how we can able to do validations on below scenarios 1) On Date column Ex If i am receving date in format of YYYYMMDD---- 20141118 --- here i have to check the date value 20141142 if the... (7 Replies)
Discussion started by: bhaskar v
7 Replies

4. Shell Programming and Scripting

Value validation

Hi All I am trying to validate a value using if condition requirement is need to check whether its a valid numeric value the input contains ( space, #N/A and negative and positive decimal values and Zeros) if it contains the space, I need to display the error message as space ... (15 Replies)
Discussion started by: tsurendra
15 Replies

5. Shell Programming and Scripting

Validation of variable

Hey people, I got small little error which says I entered too many argument in the if else list. The stock contains a list of information. The 4th field consist of the quantity available, so I'm trying to alert the user that the stock is less than 3 and needed to restock. Is this the correct way... (7 Replies)
Discussion started by: aLHaNz
7 Replies

6. Shell Programming and Scripting

Name validation

Hi All, I need to write a small piece of code to check the following. name should contain (A-Z), spaces, hyphens & apostrophes I need to generate regular expressions for the same. Please help me out as i am not familiar with regular expressions. (1 Reply)
Discussion started by: lifzgud
1 Replies

7. Shell Programming and Scripting

Validation help

Hi, I am new to Unix shell scripting and need help to add some validation to an existing script. I've made a script that takes two argument (input) but I want the script to display an error message when nothing (null) is entered. So far I managed to validate the fist argument but fail to... (2 Replies)
Discussion started by: zen10
2 Replies

8. UNIX for Dummies Questions & Answers

Validation

I'm kinda new in shell scripting. How do i validate an input from a user to conform to requirement. For example, echo "Enter First Name: " read FName echo "Enter Date of Employment (dd/mm/yyyy): " read DoE If the user enters data that is alphanumeric, it accepts it. I hope i've... (1 Reply)
Discussion started by: Allenzo
1 Replies

9. Shell Programming and Scripting

Date Validation:

Hi I have below file with 3 rd column as date ....i want to make 3 column to mm/dd/yyyy . in the below file 2 row date is like 1/23/1994 so i want to append '0' to month i,e. 01/23/1994 and in the 3 row date is like 6/4/1994 ---so i want this to 06/04/1994 Source:... (1 Reply)
Discussion started by: satyam_sat
1 Replies

10. Shell Programming and Scripting

validation of data using filter (awk or other that works...) in csv files

Hello People I have the following file.csv: date,string,float,number,boolean 20080303,abc,1.5,123,Y 20080304,abc,1.2,345,N 20080229,nvh,1.4,098,Y 20080319,ugy,1.9,586,N 20080315,gyh,2.4,345,Y 20080316,erf,3.1,932,N I need to filter the date field where I have a data bigger than I... (1 Reply)
Discussion started by: Rafael.Buria
1 Replies
Login or Register to Ask a Question