• Help with bash script please

    From Graham J@nobody@nowhere.co.uk to alt.os.linux.mint on Tue Aug 4 13:18:01 2026
    From Newsgroup: alt.os.linux.mint

    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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Paul@nospam@needed.invalid to alt.os.linux.mint on Tue Aug 4 10:22:55 2026
    From Newsgroup: alt.os.linux.mint

    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
    -a-a-a echo-a $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
    -a-a-a echo-a $i
    -a-a-a 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

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Alan K.@alan@invalid.com to alt.os.linux.mint on Tue Aug 4 11:58:02 2026
    From Newsgroup: alt.os.linux.mint

    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.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Paul@nospam@needed.invalid to alt.os.linux.mint on Tue Aug 4 12:38:19 2026
    From Newsgroup: alt.os.linux.mint

    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
    -a-a-a-a-a echo-a $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
    -a-a-a-a-a echo-a $i
    -a-a-a-a-a 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
    -a-a-a echo "${list[i]}${list[i+1]}"
    done

    printing the pairs.-a-a 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

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Graham J@nobody@nowhere.co.uk to alt.os.linux.mint on Tue Aug 4 17:52:05 2026
    From Newsgroup: alt.os.linux.mint

    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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Graham J@nobody@nowhere.co.uk to alt.os.linux.mint on Tue Aug 4 17:55:03 2026
    From Newsgroup: alt.os.linux.mint

    Alan K. wrote:

    [snip]

    #!/bin/bash
    list="ann 1 bob 2 cat 3 dog 4"
    for i in $list; do
    -a-a-a-a-a echo-a $i
    -a-a-a-a-a 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
    -a-a-a echo "${list[i]}${list[i+1]}"
    done

    printing the pairs.-a-a 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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Graham J@nobody@nowhere.co.uk to alt.os.linux.mint on Tue Aug 4 22:42:54 2026
    From Newsgroup: alt.os.linux.mint

    Graham J wrote:

    [snip]


    Good ole Copilot:

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

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

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

    That looks worth a try.-a 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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Lawrence =?iso-8859-13?q?D=FFOliveiro?=@ldo@nz.invalid to alt.os.linux.mint on Wed Aug 5 04:48:37 2026
    From Newsgroup: alt.os.linux.mint

    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 rCLdocument.writerCY 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 rCLuntil the error
    messages stoppedrCY, I counted every single one to make sure the exact
    right number that was needed was present in each place. Trivia
    question: whatrCOs 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 rCLView SourcerCY in the browser was helpful, too.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Gordon@Gordon@leaf.net.nz to alt.os.linux.mint on Thu Aug 6 08:22:36 2026
    From Newsgroup: alt.os.linux.mint

    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
    -a-a-a-a echo "${list[i]}${list[i+1]}"
    done

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

    That looks worth a try.-a 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.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Alan K.@alan@invalid.com to alt.os.linux.mint on Thu Aug 6 08:43:02 2026
    From Newsgroup: alt.os.linux.mint

    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
    -a-a-a-a echo "${list[i]}${list[i+1]}"
    done

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

    That looks worth a try.-a 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.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Dr. Noah Bodie@noah@bodie.not to alt.os.linux.mint on Mon Aug 10 22:38:08 2026
    From Newsgroup: alt.os.linux.mint

    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.
    --- Synchronet 3.22a-Linux NewsLink 1.2