G
G
gohellp2021-07-04 20:07:16
linux
gohellp, 2021-07-04 20:07:16

How and where does bash remove an array element?

I decided to make a script that will watch the output of the bot upon completion of it and look for "reboot" there in order to restart it and start all over again. I have not yet figured out how the script itself will be restarted from it, but not about that now (although if you suggest how to implement this, I will only be grateful). So far, I'm testing on this js script:

console.log("test");
console.log("test1");


The bash script itself:
read -p "Enter the programm's path with .js: " path
    cd $path
    echo "node $path/main.js"
    nodeLogs="$(node $path/main.js)"
        echo $nodeLogs
        IFS="\n" read -a nodeLogsArr <<< "$nodeLogs"
            echo "${nodeLogsArr[1]}"
            for element in "${nodeLogsArr[@]}"; do
                echo "$element"
            done
            for logs in ${nodeLogsArr[@]}; do
                if (($logs="reboot")); then
                    echo "U couldn't see it!!!"
                    #git pull
                else
                    echo "$logs"
                fi
            done


It turns out that the script, after translating the multiline string into array elements, puts the second element "test1" somewhere. I studied partitioning using IFS according to this guide . I did it, it seems, as it should, but it does not want to work ...

Script output:
$ ./test
Enter the programm's path with .js: .
node ./main.js
test test1

test
test

Maybe I'm trying to split the string incorrectly?

Thanks in advance for your reply

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xotkot, 2021-07-04
@gohellp

in a simple case:

$ x='test1 test2'
$ y=($x)
$ echo ${y[0]} И ${y[1]}
test1 И test2

if, for example, we have a newline separator, then:
$ x='test 1
test 2'
$ q="$IFS";IFS=$'\n';y=($x);IFS="$q"
$ echo ${y[0]} И ${y[1]}
test 1 И test 2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question