Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 29

Thread: Is there any way to grep a port range in netstat

  1. #11
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Is there any way to grep a port range in netstat

    examples of for loops
    Code:
    aaa="1 2 3"
    echo LOOP 1
    for a in $aaa; do echo a=$a; done;
    
    echo LOOP 2
    for a in {1..3}; do echo a=$a; done;
    
    echo LOOP 3
    for a in X{a..b}{1..2}; do echo a=$a; done;
    
    echo LOOP 4
    for (( a=0; a<4; a++ )); do echo a=$a; done;
    
    echo LOOP 5
    aaa=( 1 2 3 )
    for a in ${aaa[@]}; do echo a=$a; done;
    
    echo LOOP 6
    aaa=( 1 2 3 )
    for (( i=0; i<${#aaa[@]}; i++ )); do echo aaa[$i]=${aaa[i]}; done;
    
    echo LOOP 7
    for a in {1..3}; do
      for b in {1..3}; do
        echo a=$a b=$b
      done
    done
    you can end your script with exit anywhere and define the return code - 0 is treated as success, everything else is an error so you can use && and || to do stuff or use conditions to perform error-specific procedures.

    lets say you have
    prog1 && prog2
    this requires that prog1 ends with 0 so prog2 can be executed
    prog1 || prog2
    here prog2 will run only when prog1 ends with an error
    if you are not going to chain your script with anything or there is no need to end the script in the middle then you don't need exit

    offtopic problem - no help here. I know what &, && or sleep do but i lack deeper knowledge, i find it strange though. Does ps ux confirm that the process is still running?
    Last edited by Vaphell; January 17th, 2011 at 12:19 AM.

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

    Re: Is there any way to grep a port range in netstat

    examples of for loops
    Thanks for this. It will take me a while to figure out though.

    you can end your script with exit anywhere and define the return code - 0 is treated as success, everything else is an error so you can use && and || to do stuff or use conditions to perform error-specific procedures.

    lets say you have
    prog1 && prog2
    this requires that prog1 ends with 0 so prog2 can be executed
    prog1 || prog2
    here prog2 will run only when prog1 ends with an error
    This is one of the things I do get. "&&" and "&" are actually what got me started with this whole thing. I thought wow doing two things at once or one after the other is awesome. The "||" I have yet to use but I am sure that I will eventually. If I understand correctly the "||" is like an "if then else" without the "if" and the "then".

    if you are not going to chain your script with anything or there is no need to end the script in the middle then you don't need exit
    Could you please elaborate on this a little?

    offtopic problem - no help here. I know what &, && or sleep do but i lack deeper knowledge, i find it strange though. Does ps ux confirm that the process is still running?
    Well I didn't use "ps ux" but the process was definitely still running. It is alright though the second script really takes care of that. The weird thing is that the second script does not keep running like the first. I can't explain it but it works.

  3. #13
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Is there any way to grep a port range in netstat

    if you are not going to chain your script with anything or there is no need to end the script in the middle then you don't need exit
    Could you please elaborate on this a little?
    PHP Code:
    if <something bad>; then
      
    exit #error
    fi
    if <something else bad>; then
      
    exit #error
    fi
    if <something good>; then
      
    exit #success
    fi

    some
    bunch
    of
    code
    #if you got here with no problems, the script will exit with the code of the last command by itself (usually 0, yes?) 
    if you have exits nicely defined you can use your script in a && b || c chains or make use of the exit code in scripts using $?. eg
    Code:
    if [ "$?" == "666" ]
    $? is a special variable that keeps the code

    &&'s are braindead easy but to understand what exactly happens with && and || you need to know this - they are simply boolean logic operators (&&=AND, ||=OR) and the logic value of the whole chain is evaluated. code 0 = TRUE and code not-0 = FALSE
    a && b
    * if a is FALSE then the whole thing is FALSE => there is no need to check b (b command is skipped)
    * if a is TRUE then we need to check b because the value of the whole expression = value of b (b command is executed)
    in the sequence of ANDs first FALSE sets whole expression to FALSE

    a || b
    * if a is TRUE then the whole thing is TRUE no matter what (b is skipped)
    * if a is FALSE then the value = value of b (b is executed)
    in the sequence of ORs first TRUE sets whole expression to TRUE

    of course ANDs and ORs can be mixed for fancy hacks
    Last edited by Vaphell; January 17th, 2011 at 04:58 AM.

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

    Re: Is there any way to grep a port range in netstat

    So, what your saying is that exit is for separating two or more different blocks of code in the same script. Is that basically it?

  5. #15
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Is there any way to grep a port range in netstat

    no, exit ends the execution of the script, no questions asked. In my pseudocode example the script can end in 4 different places
    script enters condition #1 or #2 => ends with error with the exit code assigned, any code below that if is not executed
    script enters condition #3 => ends with success, any code below that if is not executed
    script executes everything => ends naturally with the exit code of the last command

    PHP Code:
    #!/bin/bash

    echo "some code"

    if (( $== 666 )); then
      
    echo SUCCESS
      
    exit 0
    fi

    echo "some more code"

    if (( $)); then
      
    echo ERROR $1
      
    exit $1
    fi

    echo "even more code" 
    run the script above with different integer parameters (666, non-zero, 0)
    Code:
    ./script 0
    echo $?
    ./script 1
    echo $?
    ./script 666
    echo $?
    666 triggers exit in the first if (success), any non-zero triggers exit in the second if (error), 0 skips ifs
    Last edited by Vaphell; January 17th, 2011 at 04:46 AM.

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

    Re: Is there any way to grep a port range in netstat

    Because it is "then exit". Got it. I don't see how I missed that one.

  7. #17
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Is there any way to grep a port range in netstat

    yes, everything between then and fi belongs to the condition and is executed only if condition is met. Proper indentation is good and helps to keep track visually of what happens.
    Last edited by Vaphell; January 17th, 2011 at 05:38 AM.

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

    Re: Is there any way to grep a port range in netstat

    Quote Originally Posted by Vaphell View Post
    yes, everything between then and fi belongs to the condition and is executed only if condition is met. Proper indentation is good and helps to keep track visually of what happens.
    Do you do the indentation by hand or is it an option that you set in your editor?

  9. #19
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Is there any way to grep a port range in netstat

    gedit, manual indentation though gedit can help you with that (it can start the new line with the indentation of the last line so it's easy to write blocks of code with unified intentation). Using tab and del from time to time never killed anybody and keeping the code tidy greatly reduces the time needed to write/debug.

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

    Re: Is there any way to grep a port range in netstat

    Quote Originally Posted by Vaphell View Post
    Using tab and del from time to time never killed anybody and keeping the code tidy greatly reduces the time needed to write/debug.
    Ha ha. Yeah thats true. I am still used to Writing small scipts. I am sure that when they start getting bigger this will be very useful. Based on all the things we have gone over the only conclusion I can come to is that this cannot be done ( using grep on a range of numbers I mean). I guess my only real option would be writing a really long script with all of the numbers in it. Do you think that an extremely long script would seriously affect the performance of running said script? I know that there is a way to output numbers in the terminal that you can predefine start and finish. I just do not remember what it is I will find it again though. So, I guess my next question is do you know how to add text to the beginning of multiple lines? For example if I had

    Code:
    21
    22
    23
    24
    and wanted to change it to

    Code:
     -e :21
     -e :22
     -e :23
     -e :24
    How is this done? Do you know?

Page 2 of 3 FirstFirst 123 LastLast

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
  •