Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, Maybe I should just make a break and go through this again by myself, but there is no more coffee, so there is no break for me I want ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2005
    Posts
    1

    "[[ ..]]" with && and || instead of if clause


    Hi,

    Maybe I should just make a break and go through this again by myself, but there is no more coffee, so there is no break for me

    I want to check for a given dir, exit with a message if it exists, or create the given dir otherwise.

    What happens is, that the shell executes the && Branch and wont exit afterwards but executes the || branch, too. Obviously mkdir throws an ugly error when the directory exists.

    Dont get me wrong, I know how to make it work with the usual if construct, but I cant sleep well tonight, when I dont use a one-liner

    Code:
    [[ -d ${DIR_CUSTOMER} ]] && (echo "*** Information: Directory ${DIR_CUSTOMER} already exists!"; exit 1;) || (echo "*** Creating ${DIR_CUSTOMER}"; mkdir ${DIR_CUSTOMER};)
    ^^ can YOU detect the mistake i have produced?

  2. #2
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    From studying the manual(!) it seems that you (and me!) have misunderstood the construct.
    Code:
    The  control operators && and || denote AND lists and OR lists, respectively.    
          An AND list has the form
    
                  command1 && command2
    
           command2 is executed if, and only if, command1 returns an  exit  status
           of zero.
    
           An OR list has the form
    
                  command1 || command2
    
           command2  is  executed  if and only if command1 returns a non-zero exit
           status.  The return status of AND and OR lists is the  exit  status  of
           the last command executed in the list.
    Now if we take this literally, if your condition is true, the && list is executed, but according to the above, if the list returns false (ie exit 1), the || list will also be executed. Not how I'd expect it work, but sort of logical, in a perverse kind of way.

    Any comments?

  3. #3
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    ...
    but sort of logical, in a perverse kind of way.
    Yep , that's the right way to describe it.
    Some people get annoyed by these idiosyncrasies of Unix but I find them
    amusing.

    What I don't understand is why your quote has ?? instead of ||

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by Santa's little helper
    What I don't understand is why your quote has ?? instead of ||
    Oops. I cut'n'pasted and didn't spot that the paste had replaced the pipe symbols with "unknown char"s. Fixed now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •