This discussion is archived
5 Replies Latest reply: Jan 21, 2013 5:36 AM by Nik RSS

Shell script

Pavel Explorer
Currently Being Moderated
Hello!


Please explain me what mean code in shel-script (I don't understand sense of "-a" key ) :
if [ -e  /u01/1.sql  *-a* ! -e /u01/1.sql ]
does it contril the existence one of files ?


Thanks and regards,
Pavel
  • 1. Re: Shell script
    Nik Expert
    Currently Being Moderated
    Hi.

    man test
    ....
    condition1 -a condition2 True if both condition1 and condition2 are true. The -a binary
    primary is left associative and has higher precedence than the
    -o binary primary.
    ........

    But it's strange record:
    if [ -e /u01/1.sql -a ! -e /u01/1.sql ]
    becouse alredy false.
    -e /u01/1.sql file /u01/1.sql exist
    -a and
    ! -e /u01/1.sql file /u01/1.sql not exist

    Regards.
  • 2. Re: Shell script
    Dude! Guru
    Currently Being Moderated
    Check the following example:

    <pre>
    $ touch 1.sql 2.sql
    $ [ -e 1.sql -a ! -e 2.sql ] && echo true || echo false
    false

    $ rm -f 2.sql
    $ [ -e 1.sql -a ! -e 2.sql ] && echo true || echo false
    true

    $ rm -f 1.sql
    $ [ -e 1.sql -a ! -e 2.sql ] && echo true || echo false
    false
    </pre>

    As the above demonstrates. Both expressions have to return true (0) for the if-then clause to be true. Hence, the -a is a logical AND.

    You example as such does not make sense, because a file cannot exist and not exist at the same time. The if-then clause will always return false. Perhaps you have a spelling mistake.
  • 3. Re: Shell script
    Pavel Explorer
    Currently Being Moderated
    Hello Dude

    I'm sorry

    my condition should be
    if [ -e /u01/1.sql -a ! -e /u01/2.sql ]
    Regards,
    Pavel
  • 4. Re: Shell script
    Dude! Guru
    Currently Being Moderated
    The -a binary primary is left associative and has higher precedence than the -o binary primary.
    Aren't all binary operators left-associative, meaning that operations are performed from left to right? Does the statement apply to combining multiple operators, e.g. expr1 -a expr2 -o expr3, in which case -a is evaluated before -o?
  • 5. Re: Shell script
    Nik Expert
    Currently Being Moderated
    Hi.
    According man you can use
    expr1 -a expr2 -o expr3


    But some times more faster just test, not ask.


    Regards.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points