Results 1 to 4 of 4

Thread: About BASH: Test with and/or/xor & pipes

  1. #1
    Join Date
    Feb 2010
    Location
    Argentina
    Beans
    19
    Distro
    Ubuntu 9.10 Karmic Koala

    About BASH: Test with and/or/xor & pipes

    Hi everybody.

    I'm trying to work something for testing based on a relative filename the ../ or ./ of a directory:

    example

    Code:
    test \(echo '../'| grep -q '^\.\{1,1\}'\) -a \(echo '../ | grep -q '^\.\{2,2\}'\)
    would give true

    Code:
    test \(echo './'| grep -q '^\.\{1,1\}'\) -a \(echo './ | grep -q '^\.\{2,2\}'\)
    and this one would give false

    For example if you issue

    Code:
    echo '../'| grep '^\.\{1,1\}'
    ../ (two dots, and the first dot matched by grep)

    if you issue

    Code:
    echo './'| grep '^\.\{2,2\}'
    will not match the two points at the beginning
    Other alternative way

    Code:
    [ echo '../'| grep -q '^\.\{1,1\} ] && [ echo '../'| grep -q '^\.\{2,2\} ]  && echo true
    errors are:

    Code:
    bash: [: missing `]'
    grep: [ ó ^[ desemparejados
    Notice I'm using the -q option to avoid output of grep. Logic would be applied based on exit status of grep of course.

    Anyone would be so kind to tell me what I'm doing wrong (i guess the problem is with the pipes).

    I also can stop whining, save the exit status on variables and later make a variable comparison (that is my last resort)?

    A B A&B
    0 0 0
    0 1 0
    1 0 0
    1 1 1

    http://www.gnu.org/software/bash/man...ell-Expansions

    I'm using this guides:
    http://tldp.org/LDP/Bash-Beginners-G...ct_07_01_02_01
    http://tldp.org/LDP/abs/html/ops.html

    I'm no programmer and just a beginner ...
    Not much experience in this.

    Thanks in advance

  2. #2
    dino99's Avatar
    dino99 is offline Ubuntu addict and loving it
    Join Date
    Jun 2006
    Location
    Nux Jam
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: About BASH: Test with and/or/xor & pipes


  3. #3
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: About BASH: Test with and/or/xor & pipes

    Hi,
    I'm not sure I've understood your question fully, but try this:
    Code:
    x="../"
    [[ "$x" =~ "." && "$x" =~ ".." ]] && echo true || echo false
    true
    x="./"
    [[ "$x" =~ "." && "$x" =~ ".." ]] && echo true || echo false
    false

  4. #4
    Join Date
    Feb 2010
    Location
    Argentina
    Beans
    19
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: About BASH: Test with and/or/xor & pipes

    Thanks! I understood it. Very clear example. Had to read some references

    I will read Mendel Cooper's guide to understand better these things. But first will finish with Machtelt Garrels one

    http://tldp.org/guides.html

    Still, I should make the piped commands save their contents in variables. Not a good thing to do everything in a command ...

    Here the version 3 of bash remarks:

    http://tldp.org/LDP/abs/html/bashver3.html
    Last edited by smeagol271006; August 8th, 2010 at 06:47 PM.

Tags for this Thread

Bookmarks

Posting Permissions

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