Find the answer to your Linux question:
Results 1 to 9 of 9
Code: if &#91; -d some_file &#93; ; then if &#40;&#40; $&#40;some_programme&#41; < 10 &#41;&#41; ; then do_something fi fi Is there a way to combine the 2 ifs into 1 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575

    Another bash question


    Code:
    if &#91; -d some_file &#93; ; then
        if &#40;&#40; $&#40;some_programme&#41; < 10 &#41;&#41; ; then
            do_something
        fi
    fi
    Is there a way to combine the 2 ifs into 1 ?

  2. #2
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    You should be able to use the && operator. Example:
    Code:
    if &#40;true && true&#41; ;  then echo Yah! ; fi

  3. #3
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    It doesn't work.Even if I put everything between double brackets
    it still gives an error message.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Feb 2004
    Location
    UK
    Posts
    131
    this should work you need to consider yor statements and if they work in this
    Very funny scotty - now beam down my clothes

  6. #5
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Code:
    if &#40; &#91; -d some_file &#93; &#41; && &#40; $&#40;some_programme&#41; < 10 &#41;  ; then
      do_something
    fi
    If it still doesn't work the way the if statement is constructed is not the problem..

  7. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    With the above construct bash thinks that 10 is a file.But if I put
    Code:
    if &#91; -d some_file &#93; && &#40;&#40; $&#40;some_programme&#41; < 10 &#41;&#41;  ; then
    it works.

    Thanks !

  8. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044

    Re: Another bash question

    Quote Originally Posted by Santa's little helper
    Code:
    if &#91; -d some_file &#93; ; then
        if &#40;&#40; $&#40;some_programme&#41; < 10 &#41;&#41; ; then
            do_something
        fi
    fi
    Is there a way to combine the 2 ifs into 1 ?
    What about using test's -a option?:
    Code:
    if &#91; -d some_file -a $&#40;some_programme&#41; < 10 &#93;
    then
        do_something
    fi
    It's easy when you know how!

  9. #8
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    It doesn't work ; it thinks 10 is a file.What does work is this:
    Code:
    if &#91; -d some_file -a \&#40; $&#40;some_programme&#41; -lt 10 \&#41; &#93; ; then
    I also tried it with the ash shell where it also works.
    Even better
    Code:
    if &#91; -d some_file -a \&#40; `some_programme` -lt 10 \&#41; &#93; ; then
    This also works with the Bourne shell.

  10. #9
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    It doesn't work ; it thinks 10 is a file.
    Of course, it's stdin redirection. Sorry.
    It doesn't actually think 10 is file - my code was (implicitly) telling it that it was! Should be fixable by quoting, but as you've got a solution, I'll leave that as an exercise for the reader.

Posting Permissions

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