Results 1 to 7 of 7

Thread: If there is terminal ouput then do something bash question

  1. #1
    Join Date
    Sep 2009
    Location
    California U.S.A.
    Beans
    398

    If there is terminal ouput then do something bash question

    Is there any way to run a command only if another command produces terminal output? I want to run

    Code:
    netstat --tcp --numeric |  cut -c 45-65 | sort -u | sed '/Foreign/d' | sed '/^$/d' | grep -e ':80' -e ':443'
    and if that produces output in the shell then run

    Code:
    echo 'http / https' && netstat --tcp --numeric |  cut -c 45-65 | sort -u | sed '/Foreign/d' | sed '/^$/d' | grep -e ':80' -e ':443'
    but also suppress the output from the original check.

    The script that I have so far is

    Code:
    #! /bin/bash
    if netstat --tcp --numeric |  cut -c 45-65 | sort -u | sed '/Foreign/d' | sed '/^$/d' | grep -e ':80' -e ':443' ; then
    echo 'http / https' && netstat --tcp --numeric |  cut -c 45-65 | sort -u | sed '/Foreign/d' | sed '/^$/d' | grep -e ':80' -e ':443'
    fi
    
    
    exit
    which produces

    Code:
    24.143.207.65:80     
    http / https
    24.143.207.65:80
    I am still new at this and know that there is something I am missing but I cannot seem to figure it out. I read a bash syntax guide that I found online but I still just don't get it. Any help would be greatly appreciated.

  2. #2
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: If there is terminal ouput then do something bash question

    Code:
    if [ "$(your command here)" ]; then
      echo something
    fi
    Last edited by sisco311; January 14th, 2011 at 07:51 PM.

  3. #3
    Join Date
    Sep 2009
    Location
    California U.S.A.
    Beans
    398

    Re: If there is terminal ouput then do something bash question

    Quote Originally Posted by sisco311 View Post
    Code:
    if [ $(your command here) ]; then
      echo something
    fi
    I tried

    Code:
    #! /bin/bash
    if [ $(netstat --tcp --numeric |  cut -c 45-65 | sort -u | sed '/Foreign/d' | sed '/^$/d' | grep -e ':80' -e ':443') ]; then
    echo 'http / https' && netstat --tcp --numeric |  cut -c 45-65 | sort -u | sed '/Foreign/d' | sed '/^$/d' | grep -e ':80' -e ':443'
    fi
    
    
    exit
    and it gave me

    Code:
    ./netcatweb.sh: line 2: [: 24.143.207.65:80: unary operator expected

  4. #4
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: If there is terminal ouput then do something bash question

    Sorry.

    You have to use quotes. I edited my first post.
    Last edited by sisco311; January 14th, 2011 at 07:58 PM.

  5. #5
    Join Date
    Sep 2009
    Location
    California U.S.A.
    Beans
    398

    Re: If there is terminal ouput then do something bash question

    Quote Originally Posted by sisco311 View Post
    Sorry.

    You have to use quotes. I edited my first post.
    Thank you this worked perfectly. The forums are definitely slow today for me too. Also, I noticed that you can use this over and over for different things. Marking thread as solved. Thanks again.

  6. #6
    Join Date
    Sep 2009
    Location
    California U.S.A.
    Beans
    398

    Re: If there is terminal ouput then do something bash question

    Quote Originally Posted by sisco311 View Post
    Sorry.

    You have to use quotes. I edited my first post.
    Thank you this worked perfectly. The forums are definitely slow today for me too. Also, I noticed that you can use this over and over for different things. Marking thread as solved. Thanks again.

  7. #7
    Join Date
    Sep 2009
    Location
    California U.S.A.
    Beans
    398

    Re: If there is terminal ouput then do something bash question

    Quote Originally Posted by sisco311 View Post
    Sorry.

    You have to use quotes. I edited my first post.
    Thank you this worked perfectly. Also, I noticed that you can use this over and over for different things. Marking thread as solved. Thanks again.

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
  •