5 Replies Latest reply: Jan 21, 2013 7:36 AM by Nik RSS

    Shell script

    Pavel
      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
          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!
            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
              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!
                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
                  Hi.
                  According man you can use
                  expr1 -a expr2 -o expr3


                  But some times more faster just test, not ask.


                  Regards.