1833031 Members
2506 Online
110049 Solutions
New Discussion

doubt on while loop

 
laiju.c.babu
Regular Advisor

doubt on while loop

Hi Gurus,

 

In a script i saw a while loop  having below condition

 

while [[ -a $LOCK_FILE ]]

 

do { ===== done }

 

could you please tell me what this while condition will do. This  LOCK_FILE is decalred as /tmp/audit.lock

 

Regards

 

Laiju.C.Babu
3 REPLIES 3
Matti_Kurkela
Honored Contributor

Re: doubt on while loop

The expression with double square brackets is a conditional expression that is parsed by the shell that is executing the script, so you should identify the shell that is used to run the script. Is it sh, ksh or something else?

 

For example, if the script is run using /sbin/sh or /usr/bin/sh in HP-UX, the sh-posix(1) man page lists all the possible conditions for a double-square-bracket conditional expression in a chapter titled Conditional Expressions.

 

In this case, -a <filename> means "the condition is true if <filename> exists". The test completely ignores the type of <filename>: it might actually be a directory, a symbolic link, a named pipe, or something else.


The loop will run as long as /tmp/audit.lock exists.

 

You might have been confused because in a single-square-bracket expression, the -a has a completely different meaning: it appears as <condition> -a <condition> and means that the expression is true only if both conditions are true.

MK
Dennis Handly
Acclaimed Contributor

Re: question on conditional expression

Be careful if replacing it with -e, that's only for sh, not ksh.

laiju.c.babu
Regular Advisor

Re: doubt on while loop

Hi MK,

 

Thanks for the reply, the shell on which this script is executing is /usr/bin/ksh.

 

Regards

Laiju.C.Babu