1832872 Members
2557 Online
110048 Solutions
New Discussion

Re: IF condition

 
SOLVED
Go to solution
intp
Frequent Advisor

IF condition

Can someone help me , what the following IF condition is checking , i'm confused because it checks same file from same location. also wat is the significance of -o -s options.

if [ ! -f "$SOURCE/inven_file_cu.dat" -o -s "$SOURCE/inven_file_cu.dat" ]; then
echo 'Process the inventory data'

Thanks
7 REPLIES 7
Marvin Strong
Honored Contributor

Re: IF condition

man test will give you the answer quickly

-f file exists and is regular
-o or
-s file exists and is greater than zero bytes
Geoff Wild
Honored Contributor

Re: IF condition

See man test:

-o Binary OR operator (-a has higher precedence than -o).

-s file True if file exists and has a size greater than zero.

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Sandman!
Honored Contributor

Re: IF condition

! -f tests if file doesn't exist and is not a regular file
-o OR
-s tests if the size of the file is greater than zero

the condition is contradictory and would never evaluate to true unless the negation sign ! is removed.
Jonathan Fife
Honored Contributor

Re: IF condition

"The condition is contradictory and would never evaluate to true unless the negation sign ! is removed."

Actually, it tests true if the file doesn't exist, or if the file exists and has size. If it were an "and" expression instead of an "or", then it would never evaluate to true...
Decay is inherent in all compounded things. Strive on with diligence
Matti_Kurkela
Honored Contributor
Solution

Re: IF condition

-o means "OR": if at least one of the subconditions separated by "-o" is true, the entire condition is true

-f is true if a file exists _and_ it is a regular file

-s is true if a file exists _and_ its size is greater than zero

So, the condition would mean:
"If $SOURCE/inven_file_cu.dat is something other than a regular file, or if it is a file with greater-than-zero length, then process the inventory data".

"Something other than a regular file" would include a directory, a named pipe (created using "mknod p") or a device. If the target of the test is a symbolic link, the condition is evaluated according to what is on the other end of the link.

I have a feeling the creator of this condition might not have been aware of all the possible "not a regular file" choices... but without knowing more about the script it is impossible to be sure.
MK
Sandman!
Honored Contributor

Re: IF condition

Correct Jonathan...got confused by the test condition. If the primaries were connected by AND (-a) then the expression would never be true.

thanks!
intp
Frequent Advisor

Re: IF condition

Thanks All.