• Help with bash script please

    From Graham J@3:633/10 to All on Tuesday, August 04, 2026 13:18:01
    This is a snippet of my script:

    #!/bin/bash
    list="ann 1 bob 2 cat 3 dog 4"
    for i in $list; do
    echo $i
    done

    This outputs the strings from the list, one per line, thus:

    ann
    1
    bob
    2
    cat
    3
    dog
    4

    But I would like to output just the alternate values, thus

    ann
    bob
    cat
    dog

    So I want to add a line, a bit like this:

    #!/bin/bash
    list="ann 1 bob 2 cat 3 dog 4"
    for i in $list; do
    echo $i
    next i
    done

    How do I achieve the line "next i" , please?

    Ultimately I want to process the list entries in pairs, but I don't want
    to overcomplicate my question at present.

    --
    Graham J

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Paul@3:633/10 to All on Tuesday, August 04, 2026 10:22:55
    On Tue, 8/4/2026 8:18 AM, Graham J wrote:
    This is a snippet of my script:

    #!/bin/bash
    list="ann 1 bob 2 cat 3 dog 4"
    for i in $list; do
    ˙˙˙ echo˙ $i
    done

    This outputs the strings from the list, one per line, thus:

    ann
    1
    bob
    2
    cat
    3
    dog
    4

    But I would like to output just the alternate values, thus

    ann
    bob
    cat
    dog

    So I want to add a line, a bit like this:

    #!/bin/bash
    list="ann 1 bob 2 cat 3 dog 4"
    for i in $list; do
    ˙˙˙ echo˙ $i
    ˙˙˙ next i
    done

    How do I achieve the line "next i" , please?

    Ultimately I want to process the list entries in pairs, but I don't want to overcomplicate my question at present.


    I don't think this is achieving much, but at least I got
    the selected elements to print out. I used this for the starting point

    https://stackoverflow.com/q/8880603/loop-through-array-of-strings-bash

    ( also https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html )

    #!/bin/bash
    declare -a arr=("ann" "1" "bob" "2" "cat" "3" "dog" "4")
    for i in `seq 0 2 6`
    do
    echo "${arr[i]}"
    done

    $ ./test.sh
    ann
    bob
    cat
    dog

    Paul


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Alan K.@3:633/10 to All on Tuesday, August 04, 2026 11:58:02
    On 8/4/26 8:18 AM, Graham J wrote:
    This is a snippet of my script:

    #!/bin/bash
    list="ann 1 bob 2 cat 3 dog 4"
    for i in $list; do
    echo $i
    done

    This outputs the strings from the list, one per line, thus:

    ann
    1
    bob
    2
    cat
    3
    dog
    4

    But I would like to output just the alternate values, thus

    ann
    bob
    cat
    dog

    So I want to add a line, a bit like this:

    #!/bin/bash
    list="ann 1 bob 2 cat 3 dog 4"
    for i in $list; do
    echo $i
    next i
    done

    How do I achieve the line "next i" , please?

    Ultimately I want to process the list entries in pairs, but I don't want
    to overcomplicate my question at present.

    Good ole Copilot:

    list=(ann 1 bob 2 cat 3 dog 4)

    for ((i=0; i<${#list[@]}; i+=2)); do
    echo "${list[i]}${list[i+1]}"
    done

    printing the pairs. Just remove the ${list[i+1]} if you only want the ann,bob,cat,dog parts.

    --
    Mint 22.3, Thunderbird 153.0esr, Firefox 153.0.1
    Alan K.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Paul@3:633/10 to All on Tuesday, August 04, 2026 12:38:19
    On Tue, 8/4/2026 11:58 AM, Alan K. wrote:
    On 8/4/26 8:18 AM, Graham J wrote:
    This is a snippet of my script:

    #!/bin/bash
    list="ann 1 bob 2 cat 3 dog 4"
    for i in $list; do
    ˙˙˙˙˙ echo˙ $i
    done

    This outputs the strings from the list, one per line, thus:

    ann
    1
    bob
    2
    cat
    3
    dog
    4

    But I would like to output just the alternate values, thus

    ann
    bob
    cat
    dog

    So I want to add a line, a bit like this:

    #!/bin/bash
    list="ann 1 bob 2 cat 3 dog 4"
    for i in $list; do
    ˙˙˙˙˙ echo˙ $i
    ˙˙˙˙˙ next i
    done

    How do I achieve the line "next i" , please?

    Ultimately I want to process the list entries in pairs, but I don't want
    to overcomplicate my question at present.

    Good ole Copilot:

    list=(ann 1 bob 2 cat 3 dog 4)

    for ((i=0; i<${#list[@]}; i+=2)); do
    ˙˙˙ echo "${list[i]}${list[i+1]}"
    done

    printing the pairs.˙˙ Just remove the ${list[i+1]} if you only want the ann,bob,cat,dog parts.


    One of my thoughts about using bash this way,
    is the syntax is rapidly going to decay into a shit-storm :-)

    It's like trying to get ordinary array behavior out of gawk,
    it's just not going to happen, and any time you think you're
    going to "make it do that", it tells you where to get off.

    Or, like trying to do binary I/O for your Fortran class.

    There's always some things an environment you selected
    really cannot do with any grace.

    Or one of those problems, where you spend four hours
    figuring out "how many back-slashes will this require to work".
    And you're up to eight back-slashes and wondering what's for lunch.
    Sure, eventually you might stumble on the syntax, but
    you'll be a lot older and grayer. And if someone asks you
    "whats the deal with these eight back slashes", you tell
    them "I don't know, I just kept adding them until the
    error messages stopped".

    Paul


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Graham J@3:633/10 to All on Tuesday, August 04, 2026 17:52:05
    Paul wrote:

    [snip]

    How do I achieve the line "next i" , please?

    Ultimately I want to process the list entries in pairs, but I don't want to overcomplicate my question at present.


    I don't think this is achieving much, but at least I got
    the selected elements to print out. I used this for the starting point

    https://stackoverflow.com/q/8880603/loop-through-array-of-strings-bash

    ( also https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html )

    #!/bin/bash
    declare -a arr=("ann" "1" "bob" "2" "cat" "3" "dog" "4")
    for i in `seq 0 2 6`
    do
    echo "${arr[i]}"
    done

    $ ./test.sh
    ann
    bob
    cat
    dog

    Paul


    I tried using an array, with a script that looked very like yours. It
    worked as expected when I passed through the array just once, for a test.

    Ultimately I want to pass through the array indefinitely, with a time
    delay between each pass, so I enclosed the whole thing in a while loop,
    like this:

    while true
    do
    # your for ... do ... done loop

    echo "Wait a minute"
    sleep 60
    echo "slept"
    echo "now go round the loop"
    done

    This fails. As written it prints "Wait a minute" and hangs - the sleep
    60 never completes. But if I take out the for loop and replace it with
    an echo "some string" it behaves sensibly. Also it behaves sensibly if
    I take out the sleep 60 line. So I suspect something confusing with
    nested for loops, but have no idea. So I'm trying do use list=(.....)
    rather than array.



    --
    Graham J

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Graham J@3:633/10 to All on Tuesday, August 04, 2026 17:55:03
    Alan K. wrote:

    [snip]

    #!/bin/bash
    list="ann 1 bob 2 cat 3 dog 4"
    for i in $list; do
    ˙˙˙˙˙ echo˙ $i
    ˙˙˙˙˙ next i
    done

    How do I achieve the line "next i" , please?

    Ultimately I want to process the list entries in pairs, but I don't want
    to overcomplicate my question at present.

    Good ole Copilot:

    list=(ann 1 bob 2 cat 3 dog 4)

    for ((i=0; i<${#list[@]}; i+=2)); do
    ˙˙˙ echo "${list[i]}${list[i+1]}"
    done

    printing the pairs.˙˙ Just remove the ${list[i+1]} if you only want the ann,bob,cat,dog parts.

    That looks worth a try. It's very like what I tried using an array (see
    my reply to Paul in the ng) so I will test it this evening and let you know.



    --
    Graham J

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Graham J@3:633/10 to All on Tuesday, August 04, 2026 22:42:54
    Graham J wrote:

    [snip]


    Good ole Copilot:

    list=(ann 1 bob 2 cat 3 dog 4)

    for ((i=0; i<${#list[@]}; i+=2)); do
    ˙˙˙˙ echo "${list[i]}${list[i+1]}"
    done

    printing the pairs.˙˙ Just remove the ${list[i+1]} if you only want
    the ann,bob,cat,dog parts.

    That looks worth a try.˙ It's very like what I tried using an array (see
    my reply to Paul in the ng) so I will test it this evening and let you
    know.


    Tried, it works OK. Thanks.


    --
    Graham J

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Lawrence D?Oliveiro@3:633/10 to All on Wednesday, August 05, 2026 04:48:37
    On Tue, 4 Aug 2026 12:38:19 -0400, Paul wrote:

    Or one of those problems, where you spend four hours figuring out
    "how many back-slashes will this require to work". And you're up to
    eight back-slashes and wondering what's for lunch. Sure, eventually
    you might stumble on the syntax, but you'll be a lot older and
    grayer. And if someone asks you "whats the deal with these eight
    back slashes", you tell them "I don't know, I just kept adding them
    until the error messages stopped".

    Been there, done that, with a project many years ago for a now-defunct
    client (sheer coincidence). Wrote a web-based intranet app in Python, outputting a page with a lot of interaction controlled via JavaScript,
    with a lot of ?document.write? calls from the JavaScript (generated
    from the Python), which needed more escaping of HTML specials (amongst
    HTML also generated from the Python).

    I went back and counted, and the largest number of backslashes in a
    row was 9. No, I did not simply add backslashes ?until the error
    messages stopped?, I counted every single one to make sure the exact
    right number that was needed was present in each place. Trivia
    question: what?s the difference between a place where you need an even
    number of backslashes, and one where you need an odd number? There
    were places where I needed 1, 2, 3, 4, 5, 7 or 9 backslashes, but
    never 6 or 8. Or any greater number. How come?

    Of course, doing ?View Source? in the browser was helpful, too.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Gordon@3:633/10 to All on Thursday, August 06, 2026 08:22:36
    On 2026-08-04, Graham J <nobody@nowhere.co.uk> wrote:
    Graham J wrote:

    [snip]


    Good ole Copilot:

    list=(ann 1 bob 2 cat 3 dog 4)

    for ((i=0; i<${#list[@]}; i+=2)); do
    ˙˙˙˙ echo "${list[i]}${list[i+1]}"
    done

    printing the pairs.˙˙ Just remove the ${list[i+1]} if you only want
    the ann,bob,cat,dog parts.

    That looks worth a try.˙ It's very like what I tried using an array (see
    my reply to Paul in the ng) so I will test it this evening and let you
    know.


    Tried, it works OK. Thanks.


    AI can not be poor at everything, writing code seems to be the exception to
    the rule.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Alan K.@3:633/10 to All on Thursday, August 06, 2026 08:43:02
    On 8/6/26 4:22 AM, Gordon wrote:
    On 2026-08-04, Graham J <nobody@nowhere.co.uk> wrote:
    Graham J wrote:

    [snip]


    Good ole Copilot:

    list=(ann 1 bob 2 cat 3 dog 4)

    for ((i=0; i<${#list[@]}; i+=2)); do
    ˙˙˙˙ echo "${list[i]}${list[i+1]}"
    done

    printing the pairs.˙˙ Just remove the ${list[i+1]} if you only want
    the ann,bob,cat,dog parts.

    That looks worth a try.˙ It's very like what I tried using an array (see >>> my reply to Paul in the ng) so I will test it this evening and let you
    know.


    Tried, it works OK. Thanks.


    AI can not be poor at everything, writing code seems to be the exception to the rule.
    It makes it's mistakes there too but for the most part it's good. I use it for stuff
    like this, with my own diagnostic and testing of course.

    --
    Mint 22.3, Thunderbird 153.0esr, Firefox 153.0.1
    Alan K.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Dr. Noah Bodie@3:633/10 to All on Monday, August 10, 2026 22:38:08
    On 2026-08-06 05:22 AM, Gordon wrote:
    On 2026-08-04, Graham J <nobody@nowhere.co.uk> wrote:
    Graham J wrote:

    [snip]


    Good ole Copilot:

    list=(ann 1 bob 2 cat 3 dog 4)

    for ((i=0; i<${#list[@]}; i+=2)); do
    echo "${list[i]}${list[i+1]}"
    done

    printing the pairs. Just remove the ${list[i+1]} if you only want
    the ann,bob,cat,dog parts.

    That looks worth a try. It's very like what I tried using an array (see >>> my reply to Paul in the ng) so I will test it this evening and let you
    know.

    Tried, it works OK. Thanks.


    AI can not be poor at everything, writing code seems to be the exception to the rule.

    I've never used it myself but many people say that Claude is the best AI
    for writing code. I mostly use GPT for writing bash or php apps and so
    far I've gotten several very useful scripts.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)