Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
I have about 2 or 3 questions. Using a for loop (for i in $(command);do echo $i;done) how can I get it to accept a full line as a var? ...
  1. #1
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    55

    Newbie looking for bash scripting help

    I have about 2 or 3 questions.

    Using a for loop (for i in $(command);do echo $i;done) how can I get it to accept a full line as a var?

    ie

    param1 param2 param3

    comes out as
    param1
    param2
    param3

    How can I get it to take the whole line as a var? (I will have multiple lines)

    Part 2: How can I take that same line, and left align the line and the right align personal output?

    ie

    p1 p2 p3<---- left align, right align ----------------->FAIL
    p1 p2 p3<---- left align, right align ----------------->PASS

    ?

    Thanks in advance

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    put param1 2 and 3 in quotes, and it will get passed as a whole line. You can demonstrate this with a simple script:

    Code:
    #!/bin/bash
    for i in $@; do
       echo $i
    done
    echo "NOW WITH QUOTES"
    for i in "$@"; do
       echo $i
    done
    Run the program like ./program one two three four five and see what happens.

  3. #3
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    As far as part two goes, if you're only doing PASS or FAIL, echo COLS - 6 spaces, then echo pass or fail. I don't know of an easier way really.
    So something like

    Code:
    #!/bin/bash
    for i in "$LINE"; do
       echo -n $LINE
       while (COUNT < COLS - 6); do
          echo -n ' '
          let COUNT+=1
       done
       echo $STATUS
    done

  4. #4
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    55
    Quote Originally Posted by likwid View Post
    put param1 2 and 3 in quotes, and it will get passed as a whole line. You can demonstrate this with a simple script:

    Code:
    #!/bin/bash
    for i in $@; do
       echo $i
    done
    echo "NOW WITH QUOTES"
    for i in "$@"; do
       echo $i
    done
    Run the program like ./program one two three four five and see what happens.
    Actually, my inputed text will be dynamic. I am pulling it from netstat.

  5. #5
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    55
    Quote Originally Posted by likwid View Post
    As far as part two goes, if you're only doing PASS or FAIL, echo COLS - 6 spaces, then echo pass or fail. I don't know of an easier way really.
    So something like

    Code:
    #!/bin/bash
    for i in "$LINE"; do
       echo -n $LINE
       while (COUNT < COLS - 6); do
          echo -n ' '
          let COUNT+=1
       done
       echo $STATUS
    done

    I keep trying the above, but get failures. I have also tried replacing COLS with COLUMNS without success.

    #!/bin/bash

    STATUS="FAIL"

    TEST="1 2 3 4 5"

    for i in "$TEST"; do
    echo -n $TEST
    while (COUNT < COLS - 6); do
    echo -n ' '
    let COUNT+=1
    done
    echo $STATUS
    done

    --------------------------------- RESULTS ------------------------

    # ./test.sh
    1 2 3 4 5./test.sh: line 9: COLUMNS: No such file or directory
    FAIL

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Just one qns for you, you want to parse netstat's output right? Show what you want to see from netstat's output.

  7. #7
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    55
    Quote Originally Posted by ghostdog74 View Post
    Just one qns for you, you want to parse netstat's output right? Show what you want to see from netstat's output.
    xxx.xxx.xxx.132:22 0.0.0.0:* LISTEN
    xxx.xxx.xxx.132:143 xxx.xxx.xxx.xxx:1407 ESTABLISHED
    xxx.xxx.xxx.133:22 xxx.xxx.xxx.xxx:1408 ESTABLISHED
    xxx.xxx.xxx.133:143 xxx.xxx.xxx.xxx:1407 ESTABLISHED

    I want most left aligned and LISTEN, ESTABLISHED, ETC right aligned. I want to color them based on the local IP (multiple IPs bound). The color code I can do, just the other stuff I am having issues with

  8. #8
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by jdh239 View Post
    xxx.xxx.xxx.132:22 0.0.0.0:* LISTEN
    xxx.xxx.xxx.132:143 xxx.xxx.xxx.xxx:1407 ESTABLISHED
    xxx.xxx.xxx.133:22 xxx.xxx.xxx.xxx:1408 ESTABLISHED
    xxx.xxx.xxx.133:143 xxx.xxx.xxx.xxx:1407 ESTABLISHED

    I want most left aligned and LISTEN, ESTABLISHED, ETC right aligned. I want to color them based on the local IP (multiple IPs bound). The color code I can do, just the other stuff I am having issues with
    how did you execute your netstat...with options? if yes, which options? netstat output is supposed to be aligned very nicely for you, as far as i know.....otherwise, i might have missed something ...

  9. #9
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    55
    Quote Originally Posted by ghostdog74 View Post
    how did you execute your netstat...with options? if yes, which options? netstat output is supposed to be aligned very nicely for you, as far as i know.....otherwise, i might have missed something ...
    I was able to accomplish my goal by using an array. Took me a while, but working now.

    Thanks.

  10. #10
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Yea sorry about that syntactical error up there (i've been doing a lot of c lately) it should be

    Code:
    while [ $COUNT -lt $(($COLS - 6)) ]; do
       echo whatyouwanthere
       let COUNT+=1
    done
    It was saying that because > isn't used for comparison in Bash, it's used for file redirection.

Page 1 of 2 1 2 LastLast

Posting Permissions

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